java api 访问 hbase

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

1、创建表

public class HBaseTest {

    Configuration config = null;
    private Connection connection = null;
    private Table table = null;

    @Before
    public void init() throws Exception {
        config = HBaseConfiguration.create();// 配置
        config.set("hbase.zookeeper.quorum", "192.168.25.127,192.168.25.129,192.168.25.130");// zookeeper地址
        config.set("hbase.zookeeper.property.clientPort", "2181");// zookeeper端口
        connection = ConnectionFactory.createConnection(config);
        table = connection.getTable(TableName.valueOf("user1"));
    }
    /**
     * 创建表
     * @throws Exception
     */
    @Test
    public void testCreateTable() throws Exception{
        //创建表管理类
        HBaseAdmin admin = new HBaseAdmin(config);
        //创建表描述类
        TableName tableName = TableName.valueOf("user2");
        HTableDescriptor descriptor = new HTableDescriptor(tableName);
        //创建列族描述类
        HColumnDescriptor info1 = new HColumnDescriptor("info1");
        //列族加入表中
        descriptor.addFamily(info1);
        HColumnDescriptor info2 = new HColumnDescriptor("info2");
        descriptor.addFamily(info2);
        //创建表
        admin.createTable(descriptor);
    }
}

如果执行之后发现程序卡住不动,或者过了很久之后出现下面的异常

org.apache.hadoop.hbase.client.RetriesExhaustedException: Failed after attempts=35, exceptions:
...
Caused by: org.apache.hadoop.hbase.MasterNotRunningException: com.google.protobuf.ServiceException: java.net.UnknownHostException: unknown host: mini1
...
Caused by: com.google.protobuf.ServiceException: java.net.UnknownHostException: unknown host: mini1
...

首先确保关闭了hadoop的安全模式,然后linux下的ip地址跟主机名必须对应,最后windows下的ip地址跟主机名也要对应,我这在linux下/etc/hosts文件中

但是当时出现windows下的hosts文件没有配置后三个映射导致出现上面的异常。

执行之后去hbase集群查看
user2表已经创建

hbase(main):002:0> list
TABLE                                                                                                                                                                             
user1                                                                                                                                                                             
user2                                                                                                                                                                             
2 row(s) in 0.0130 seconds

=> ["user1", "user2"]

2、删除表
删除表跟shell命令一样,也是要先disable表之后才能删除

@Test
    public void testDeleteTable() throws Exception{
        HBaseAdmin admin = new HBaseAdmin(config);
        admin.disableTable("user2");
        admin.deleteTable("user2");
    }

3、单条插入(修改)

/**
     * 向表中插入数据
     * 单条插入(包括修改)
     * @throws Exception
     */
    @Test
    public void testPut() throws Exception{
        //rowkey
        Put put = new Put(Bytes.toBytes("1234"));
        //列族,列,值
        put.add(Bytes.toBytes("info1"), Bytes.toBytes("gender"), Bytes.toBytes("1"));
        put.add(Bytes.toBytes("info2"), Bytes.toBytes("name"), Bytes.toBytes("wangwu"));
        table.put(put);
        //提交
        table.flushCommits();
    }

4、批量插入数据
Table有2个重载的方法,一个是table.put(Put put)也就是单条插入,一个是table.put(list)list泛型是Put,这就是批量插入。

/**
     * 向表中插入数据
     * 多条插入,使用list
     * @throws Exception
     */
    @Test
    public void testPut2() throws Exception{
        //可以通过将自动刷新设置为false来激活缓冲区
        table.setAutoFlushTo(false);
        //设置数据将被写入的缓冲区大小
        table.setWriteBufferSize(534534534);
        List<Put> putList = new ArrayList<>();
        for (int i=20;i<=30;i++){
            //rowkey
            Put put = new Put(Bytes.toBytes("jbm_"+i));
            //列族,列,值
            put.add(Bytes.toBytes("info1"), Bytes.toBytes("age"), Bytes.toBytes(i));
            put.add(Bytes.toBytes("info1"), Bytes.toBytes("name"), Bytes.toBytes("lucy"+i));
            putList.add(put);
        }
        table.put(putList);
        //提交
        table.flushCommits();
    }

5、修改数据

/**
     * 修改数据
     * @throws Exception
     */
    @Test
    public void testUpdate() throws Exception{
        Put put = new Put(Bytes.toBytes("1234"));
        put.add(Bytes.toBytes("info2"), Bytes.toBytes("name"), Bytes.toBytes("tom"));
        table.put(put);
        table.flushCommits();
    }

6、删除整行数据
删除rowkey为1234整行数据

/**
     * 删除数据
     * @throws Exception
     */
    @Test
    public void testDeleteData() throws Exception{
        Delete delete = new Delete(Bytes.toBytes("1234"));
        table.delete(delete);
        table.flushCommits();
    }

7、单条查询

/**
     * 单条查询
     * @throws Exception
     */
    @Test
    public void testGetSingle() throws Exception{
        //rowkey
        Get get = new Get(Bytes.toBytes("12345"));
        Result result = table.get(get);
        //列族,列名
        byte[] name = result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("name"));
        byte[] age = result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("age"));
        System.out.println(Bytes.toString(name));
        System.out.println(Bytes.toString(age));
    }

8、多条查询
这里叫做扫描更适合吧,先用全表扫描,和命令行的scan ‘表名’一样

/**
     * 多条查询
     * 全表扫描
     * @throws Exception
     */
    @Test
    public void testGetMany() throws Exception{

        Scan scan = new Scan();
        //字典序   类似于分页
        scan.setStartRow(Bytes.toBytes("jbm_20"));
        scan.setStopRow(Bytes.toBytes("jbm_30"));
        ResultScanner resultScanner = table.getScanner(scan);
        for (Result result : resultScanner) {
            //Single row result of a Get or Scan query. Result
            //Result 一次获取一个rowkey对应的记录
            //列族,列名
            byte[] name = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name"));
            byte[] age = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("age"));
            System.out.print(Bytes.toString(name)+",");
            System.out.print(Bytes.toInt(age));
            System.out.println();
        }

    }

9、Hbase过滤器
1)列值过滤器
SingleColumnValueFilter
过滤列值的相等、不等、范围等

/**
     * 全表扫描过滤器
     * 列值过滤器
     * @throws Exception
     */
    @Test
    public void testFilter() throws Exception{

        Scan scan = new Scan();
        //列值过滤器
        SingleColumnValueFilter columnValueFilter = new SingleColumnValueFilter(Bytes.toBytes("info1"), 
                Bytes.toBytes("name"), CompareOp.EQUAL, Bytes.toBytes("lisi"));
        //设置过滤器
        scan.setFilter(columnValueFilter);
        //获取结果集
        ResultScanner resultScanner = table.getScanner(scan);
        for (Result result : resultScanner) {
            byte[] name = result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("name"));
            byte[] age = result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("age"));
            System.out.print(Bytes.toString(name)+",");
            System.out.print(Bytes.toString(age));
            System.out.println();
        }
    }

2)rowkey过滤器
RowFilter 通过正则,过滤rowKey值。

/**
     * 全表扫描过滤器
     * rowkey过滤
     * @throws Exception
     */
    @Test
    public void testRowkeyFilter() throws Exception{

        Scan scan = new Scan();
        //rowkey过滤器
        //匹配以jbm开头的
        RowFilter filter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator("^jbm"));
        //设置过滤器
        scan.setFilter(filter);
        //获取结果集
        ResultScanner resultScanner = table.getScanner(scan);
        for (Result result : resultScanner) {
            byte[] name = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name"));
            byte[] age = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("age"));
            System.out.print(Bytes.toString(name)+",");
            System.out.print(Bytes.toInt(age));
            System.out.println();
        }
    }

3)列名前缀过滤器
ColumnPrefixFilter列名前缀过滤

/**
     * 全表扫描过滤器
     * 列名前缀过滤
     * @throws Exception
     */
    @Test
    public void testColumnPrefixFilter() throws Exception{

        Scan scan = new Scan();
        //列名前缀过滤器 列名前缀为na(注:不是指值的前缀)
        ColumnPrefixFilter filter = new ColumnPrefixFilter(Bytes.toBytes("na"));
        //设置过滤器
        scan.setFilter(filter);
        //获取结果集
        ResultScanner resultScanner = table.getScanner(scan);
        for (Result result : resultScanner) {
            byte[] name = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name"));
            byte[] age = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("age"));
            if(name!=null){
                System.out.print(Bytes.toString(name)+" ");
            }
            if(age!=null){
                System.out.print(age);
            }
            System.out.println();
        }
    }

4)过滤器集合

/**
     * 全表扫描过滤器
     * 过滤器集合
     * @throws Exception
     */
    @Test
    public void testFilterList() throws Exception{
        Scan scan = new Scan();
        //过滤器集合:MUST_PASS_ALL(and),MUST_PASS_ONE(or)
        FilterList filterList = new FilterList(Operator.MUST_PASS_ALL);
        //ROWKEY过滤器
        RowFilter rowFilter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator("^jbm"));
        //列值过滤器     age大于25
        SingleColumnValueFilter columnValueFilter = new SingleColumnValueFilter(Bytes.toBytes("info1"), 
                Bytes.toBytes("age"), CompareOp.GREATER, Bytes.toBytes(25));
        filterList.addFilter(columnValueFilter);
        filterList.addFilter(rowFilter);
        //设置过滤器
        scan.setFilter(filterList);
        //获取结果集
        ResultScanner resultScanner = table.getScanner(scan);
        for (Result result : resultScanner) {
            byte[] name = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name"));
            byte[] age = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("age"));
            if(name!=null){
                System.out.print(Bytes.toString(name)+" ");
            }
            if(age!=null){
                System.out.print(Bytes.toInt(age)+" ");
            }
            System.out.println();
        }
    }


更多的过滤器用法
https://blog.csdn.net/m0_37739193/article/details/73615016
参考:
作者:豆约翰
链接:https://www.jianshu.com/p/aeb2ce94d5f5
來源:简书

猜你喜欢

转载自blog.csdn.net/wth_97/article/details/86420801