Java连接Hbase

创建工程添加依赖

<dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-client</artifactId>
      <version>2.3.5</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-server</artifactId>
      <version>2.3.5</version>
    </dependency>

初始化连接Hbase

public void init() throws IOException {
        System.out.println("执行初始化init()方法");
        config = HBaseConfiguration.create();
        config.set(HConstants.HBASE_DIR,"hdfs://192.168.140.33:9000/hbase");
        config.set(HConstants.ZOOKEEPER_QUORUM,"192.168.140.33");
        config.set(HConstants.CLIENT_PORT_STR,"2181");
        conn=ConnectionFactory.createConnection(config);
        admin = conn.getAdmin();
    }

创建命名空间

public void createNameSpace() throws IOException {
        NamespaceDescriptor myhbase= NamespaceDescriptor.create("myhbase").build();

        try {
            admin.createNamespace(myhbase);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

创建和删除库中的表

public void createTable() throws IOException {
        //创建表的描述类
       TableName tableName = TableName.valueOf("myhbase:student");

        HTableDescriptor desc = new HTableDescriptor(tableName);

        //创建列族描述类
        HColumnDescriptor family1 = new HColumnDescriptor("info1");
        HColumnDescriptor family2 = new HColumnDescriptor("info2");
        desc.addFamily(family1);
        desc.addFamily(family2);
        admin.createTable(desc);
         //删除表格
        admin.disableTable(TableName.valueOf("myhabase:student"));
        admin.deleteTable(TableName.valueOf("myhbase:student"));

获取所有命名空间

public void getAllNmaeSpace() throws IOException {
        String[] nps=admin.listNamespaces();
        String s = Arrays.toString(nps);
        System.out.println(s);
        }

插入数据

public void insertData() throws IOException {
        Table table = conn.getTable(TableName.valueOf("myhbase:student"));
        Put put = new Put(Bytes.toBytes( "student1")) ;
        put.addColumn("info1".getBytes(),"name".getBytes(),"zs".getBytes()) ;
        put.addColumn("info2".getBytes(),"school".getBytes(),"bjdx".getBytes());
        table.put(put);//插入数据

        Put put1 = new Put(Bytes.toBytes( "student2")) ;
        put1.addColumn("info1".getBytes (),"name".getBytes (),"ls".getBytes ()) ;
        put1.addColumn("info2".getBytes (),"school".getBytes(),"qhdx".getBytes());


        Put put2 = new Put(Bytes.toBytes( "student3")) ;
        put2.addColumn("info1".getBytes (),"name".getBytes (),"ww".getBytes ()) ;
        put2.addColumn("info2".getBytes (),"school".getBytes(),"njdx".getBytes());

        List<Put> list =new ArrayList<>();
        list.add(put1);
        list.add(put2);
        table.put(list);//插入数据集合

    }

查询表数据

public void queryData() throws IOException {
        Table table =conn.getTable(TableName.valueOf("myhbase:student"));
        Get get =new Get(Bytes.toBytes("student1"));
        Result result=table.get(get);
        byte[] value=result.getValue(Bytes.toBytes("info1"),Bytes.toBytes("name"));
        System.out.println("姓名"+Bytes.toString(value));
        value=result.getValue(Bytes.toBytes("info2"),Bytes.toBytes("school"));
        System.out.println("学校:"+Bytes.toString(value));
    }

扫描表中数据

public void scanData() throws IOException {
        Table table=conn.getTable(TableName.valueOf("myhbase:student"));
        Scan scan=new Scan();
        ResultScanner scanner=table.getScanner(scan);
        for (Result result:scanner
             ) {
            byte[] value=result.getValue(Bytes.toBytes("info1"),Bytes.toBytes("name"));
            System.out.println("姓名"+Bytes.toString(value));
            value=result.getValue(Bytes.toBytes("info2"),Bytes.toBytes("school"));
            System.out.println("学校:"+Bytes.toString(value));
        }
        scanner.close();
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值