2018-07-13 hbase

hive与hbase
如果hive相当与hadop中的传统关系数据数据库,那么hbase就是hadoop中的nosql,非关系型数据库

安装

环境hadoop,zookeeper
1.解压hbase
2.编辑hbase_env.sh
修改java_home
修改 HBASE_MANAGES_ZK = FALSE(不使用自带zk)
3.添加hadoop
将hadoop /etc/hadoop下的hdfs-site.xml和core-site.xml拷入hbase/conf下
4.新建hbase_site.xml


        
        
                hbase.rootdir
                hdfs://ip1/hbase
        
        
        
                hbase.cluster.distributed
                true
        
        
        
                hbase.zookeeper.quorum
                ip1:2181,ip2:2181,ip3:2181
        
    

5.编辑regionservers和backup-masters
regionservers :添加分布式机器
backup-masters:添加备用主机
6.拷贝到其它集群
7.启动
启动zk,启动hdfs,启动start-hbase.sh

浏览器访问:ip:16010

注:启动master
hbase-daemon.sh start master

java实现增删改差

导入hbaseclient

public class HbaseDemo {

    private Configuration conf = null;
    
    @Before
    public void init(){
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "weekend05,weekend06,weekend07");
    }
    
    @Test
    public void testDrop() throws Exception{
        HBaseAdmin admin = new HBaseAdmin(conf);
        admin.disableTable("account");
        admin.deleteTable("account");
        admin.close();
    }
    
    @Test
    public void testPut() throws Exception{
        HTable table = new HTable(conf, "person_info");
        Put p = new Put(Bytes.toBytes("person_rk_bj_zhang_000002"));
        p.add("base_info".getBytes(), "name".getBytes(), "zhangwuji".getBytes());
        table.put(p);
        table.close();
    }
    

    @Test
    public void testDel() throws Exception{
        HTable table = new HTable(conf, "user");
        Delete del = new Delete(Bytes.toBytes("rk0001"));
        del.deleteColumn(Bytes.toBytes("data"), Bytes.toBytes("pic"));
        table.delete(del);
        table.close();
    }

    @Test
    public void testGet() throws Exception{
        HTable table = new HTable(conf, "person_info");
        Get get = new Get(Bytes.toBytes("person_rk_bj_zhang_000001"));
        get.setMaxVersions(5);
        Result result = table.get(get);
        
        List cells = result.listCells();
    
        for(Cell c:cells){
        }
        
        //result.getValue(family, qualifier);  可以从result中直接取出一个特定的value
        
        //遍历出result中所有的键值对
        List kvs = result.list();
        //kv  ---> f1:title:superise....      f1:author:zhangsan    f1:content:asdfasldgkjsldg
        for(KeyValue kv : kvs){
            String family = new String(kv.getFamily());
            System.out.println(family);
            String qualifier = new String(kv.getQualifier());
            System.out.println(qualifier);
            System.out.println(new String(kv.getValue()));
            
        }
        table.close();
    }

你可能感兴趣的:(2018-07-13 hbase)