本文主要是hbase的表操作、数据操作、数据查询过滤等,如果对JDBC或ADO有了解,容易理解HBASE API。

hbase版本是2.0。

1、为了方便先贴helper的部分代码(文末git上有完整的测试代码),主要是为了复用Connection。

  1. public class HBaseHelper implements Closeable {
  2.  
  3. private Configuration configuration = null;
  4. private Connection connection = null;
  5. private Admin admin = null;
  6.  
  7. private HBaseHelper(Configuration configuration) throws IOException {
  8. this.configuration = configuration;
  9. this.connection = ConnectionFactory.createConnection(this.configuration);
  10. admin = this.connection.getAdmin();
  11. }
  12.  
  13. public static HBaseHelper getHBaseHelper(Configuration configuration) throws IOException {
  14. return new HBaseHelper(configuration);
  15. }
  16.  
  17. @Override
  18. public void close() throws IOException {
  19. admin.close();
  20. connection.close();
  21. }
  22.  
  23. public Connection getConnection() {
  24. return connection;
  25. }
  26.  
  27. public Configuration getConfiguration() {
  28. return configuration;
  29. }
    ... ...

初始化,用来初始化hbase配置,连接hbase,获取本文中的hbase辅助操作类HbaseHelper。

  1. //初始化
  2. private void setUp() throws IOException{
  3. conf = HBaseConfiguration.create();
  4. conf.set("hbase.master","192.168.31.10");
  5. //The port the HBase Master should bind to.
  6. // conf.set("hbase.master.port","16000");
  7.  
  8. //The port for the HBase Master web UI. Set to -1 if you do not want a UI instance run.
  9. // conf.set("hbase.master.info.port","16010");
  10.  
  11. //The port the HBase RegionServer binds to.
  12. // conf.set("hbase.regionserver.port","16020");
  13.  
  14. //The port for the HBase RegionServer web UI Set to -1 if you do not want the RegionServer UI to run.
  15. // conf.set("hbase.regionserver.info.port","16030");
  16.  
  17. conf.set("hbase.zookeeper.quorum","192.168.31.10");
  18.  
  19. //Property from ZooKeeper’s config zoo.cfg. The port at which the clients will connect.
  20. // HBase数据库使用的端口
  21. //conf.set("hbase.zookeeper.property.clientPort", "2181");
  22.  
  23. //单机
  24. conf.set("hbase.rootdir","file:///opt/hbase_data");
  25. conf.set("hbase.zookeeper.property.dataDir","/opt/hbase_data/zookeeper");
  26.  
  27. helper = HBaseHelper.getHBaseHelper(conf);
  28. }

2、命名空间、表创建、删除、exist等

  1. public void createNamespace(String namespace) {
  2. try {
  3. NamespaceDescriptor nd = NamespaceDescriptor.create(namespace).build();
  4. admin.createNamespace(nd);
  5. } catch (Exception e) {
  6. System.err.println("Error: " + e.getMessage());
  7. }
  8. }
  9.  
  10. public void dropNamespace(String namespace, boolean force) {
  11. try {
  12. if (force) {
  13. TableName[] tableNames = admin.listTableNamesByNamespace(namespace);
  14. for (TableName name : tableNames) {
  15. admin.disableTable(name);
  16. admin.deleteTable(name);
  17. }
  18. }
  19. } catch (Exception e) {
  20. // ignore
  21. }
  22. try {
  23. admin.deleteNamespace(namespace);
  24. } catch (IOException e) {
  25. System.err.println("Error: " + e.getMessage());
  26. }
  27. }
  28.  
  29. public boolean existsTable(String table)
  30. throws IOException {
  31. return existsTable(TableName.valueOf(table));
  32. }
  33.  
  34. public boolean existsTable(TableName table)
  35. throws IOException {
  36. return admin.tableExists(table);
  37. }
  38.  
  39. public void createTable(String table, String... colfams)
  40. throws IOException {
  41. createTable(TableName.valueOf(table), 1, null, colfams);
  42. }
  43.  
  44. public void createTable(TableName table, String... colfams)
  45. throws IOException {
  46. createTable(table, 1, null, colfams);
  47. }
  48.  
  49. public void createTable(String table, int maxVersions, String... colfams)
  50. throws IOException {
  51. createTable(TableName.valueOf(table), maxVersions, null, colfams);
  52. }
  53.  
  54. public void createTable(TableName table, int maxVersions, String... colfams)
  55. throws IOException {
  56. createTable(table, maxVersions, null, colfams);
  57. }
  58.  
  59. public void createTable(String table, byte[][] splitKeys, String... colfams)
  60. throws IOException {
  61. createTable(TableName.valueOf(table), 1, splitKeys, colfams);
  62. }
  63.  
  64. public void createTable(TableName table, int maxVersions, byte[][] splitKeys,
  65. String... colfams)
  66. throws IOException {
  67. //表描述器构造器
  68. TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(table);
  69.  
  70. //列族描述构造器
  71. ColumnFamilyDescriptorBuilder cfDescBuilder;
  72.  
  73. //列族描述器
  74. ColumnFamilyDescriptor cfDesc;
  75.  
  76. for (String cf : colfams) {
  77. cfDescBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cf));
  78. cfDescBuilder.setMaxVersions(maxVersions);
  79. cfDesc = cfDescBuilder.build();
  80. tableDescriptorBuilder.setColumnFamily(cfDesc);
  81. }
  82. //获得表描述器
  83. TableDescriptor tableDescriptor = tableDescriptorBuilder.build();
  84.  
  85. if (splitKeys != null) {
  86. admin.createTable(tableDescriptor, splitKeys);
  87. } else {
  88. admin.createTable(tableDescriptor);
  89. }
  90. }

  91. //禁用表
  92. public void disableTable(String table) throws IOException {
  93. disableTable(TableName.valueOf(table));
  94. }
  95.  
  96. public void disableTable(TableName table) throws IOException {
  97. admin.disableTable(table);
  98. }
  99.  
  100. public void dropTable(String table) throws IOException {
  101. dropTable(TableName.valueOf(table));
  102. }

  103. //删除前,先禁用表
  104. public void dropTable(TableName table) throws IOException {
  105. if (existsTable(table)) {
  106. if (admin.isTableEnabled(table)) disableTable(table);
  107. admin.deleteTable(table);
  108. }
  109. }

样例:

  1. //插入testtable表数据
  2. private void initTestTable() throws IOException{
  3. String tableNameString = "testtable";
  4. if(helper.existsTable(tableNameString)){
  5. helper.disableTable(tableNameString);
  6. helper.dropTable(tableNameString);
  7. }
  8.  
  9. helper.createTable(tableNameString,"info","ex","memo");
  10. helper.put(tableNameString,"row1","info","username","admin");
  11. helper.put(tableNameString,"row1","ex","addr","北京大道");
  12. helper.put(tableNameString,"row1","memo","detail","超级用户,地址:北京大道");
  13.  
  14. helper.put(tableNameString,"row2","info","username","guest");
  15. helper.put(tableNameString,"row2","ex","addr","全国各地");
  16. helper.put(tableNameString,"row2","memo","detail","游客,地址:全国到处都是");
  17.  
  18. helper.close();
  19. }

2、插入(或是更新)数据

  1. public void put(String table, String row, String fam, String qual,
  2. String val) throws IOException {
  3. put(TableName.valueOf(table), row, fam, qual, val);
  4. }
  5.  
  6. //插入或更新单行
  7. public void put(TableName table, String row, String fam, String qual,
  8. String val) throws IOException {
  9. Table tbl = connection.getTable(table);
  10. Put put = new Put(Bytes.toBytes(row));
  11. put.addColumn(Bytes.toBytes(fam), Bytes.toBytes(qual), Bytes.toBytes(val));
  12. tbl.put(put);
  13. tbl.close();
  14. }
  15.  
  16. public void put(String table, String row, String fam, String qual, long ts,
  17. String val) throws IOException {
  18. put(TableName.valueOf(table), row, fam, qual, ts, val);
  19. }
  20.  
  21. //带时间戳插入或更新单行
  22. public void put(TableName table, String row, String fam, String qual, long ts,
  23. String val) throws IOException {
  24. Table tbl = connection.getTable(table);
  25. Put put = new Put(Bytes.toBytes(row));
  26. put.addColumn(Bytes.toBytes(fam), Bytes.toBytes(qual), ts,
  27. Bytes.toBytes(val));
  28. tbl.put(put);
  29. tbl.close();
  30. }
  31.  
  32. //插入或者更新一个rowKey数据,一个Put里有一个rowKey,可能有多个列族和列名
  33. public void put(String tableNameString, Put put) throws IOException {
  34. TableName tableName = TableName.valueOf(tableNameString);
  35. Table table = connection.getTable(tableName);
  36. if (put != null && put.size() > 0) {
  37. table.put(put);
  38. }
  39. table.close();
  40. }

2.1、批量插入,根据实际的业务来组装数据,最终就是利用API放入put列表

  1. //批量插入数据,list里每个map就是一条数据,并且按照rowKey columnFamily columnName columnValue放入map的key和value
  2. public void bulkInsert(String tableNameString, List<Map<String, Object>> list) throws IOException {
  3. Table table = connection.getTable(TableName.valueOf(tableNameString));
  4. List<Put> puts = new ArrayList<Put>();
  5. if (list != null && list.size() > 0) {
  6. for (Map<String, Object> map : list) {
  7. Put put = new Put(Bytes.toBytes(map.get("rowKey").toString()));
  8. put.addColumn(Bytes.toBytes(map.get("columnFamily").toString()),
  9. Bytes.toBytes(map.get("columnName").toString()),
  10. Bytes.toBytes(map.get("columnValue").toString()));
  11. puts.add(put);
  12. }
  13. }
  14. table.put(puts);
  15. table.close();
  16. }
  17.  
  18. //批量插入,外部组装put放入list
  19. public void bulkInsert2(String tableNameString, List<Put> puts) throws IOException {
  20. Table table = connection.getTable(TableName.valueOf(tableNameString));
  21. if (puts != null && puts.size() > 0) {
  22. table.put(puts);
  23. }
  24. table.close();
  25. }

样例:

  1. //批量插入
  2. private void bulkInsertTestTable() throws IOException{
  3. String tableNameString = "testtable";
  4. if(!helper.existsTable(tableNameString)){
  5. helper.createTable(tableNameString,"info","ex","memo");
  6. }
  7.  
  8. System.out.println(".........批量插入数据start.........");
  9. List<Map<String,Object>> mapList = new ArrayList<>();
  10. for(int i=1;i<201;i++){
  11. Map<String,Object> map = new HashMap<>();
  12. map.put("rowKey","testKey"+i);
  13. map.put("columnFamily","info");
  14. map.put("columnName","username");
  15. map.put("columnValue","guest"+i);
  16.  
  17. map.put("rowKey","testKey"+i);
  18. map.put("columnFamily","ex");
  19. map.put("columnName","addr");
  20. map.put("columnValue","北京路"+i+"号");
  21.  
  22. map.put("rowKey","testKey"+i);
  23. map.put("columnFamily","memo");
  24. map.put("columnName","detail");
  25. map.put("columnValue","联合国地球村北京路第"+i+"号");
  26.  
  27. mapList.add(map);
  28. }
  29.  
  30. helper.bulkInsert(tableNameString,mapList);
  31.  
  32. System.out.println(".........批量插入数据end.........");
  33. }
  34.  
  35. //批量插入2
  36. private void insertByRowKey(String table,String rowKey) throws IOException{
  37. Put put = new Put(Bytes.toBytes(rowKey));
  38.  
  39. String columnFamily ;
  40. String columnName ;
  41. String columnValue ;
  42. for(int i=0;i<10;i++){
  43. columnFamily = "info";
  44. columnName = "username"+i;
  45. columnValue = "user111";
  46. put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(columnName),Bytes.toBytes(columnValue));
  47.  
  48. columnFamily = "ex";
  49. columnName = "addr"+i;
  50. columnValue = "street 111";
  51. put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(columnName),Bytes.toBytes(columnValue));
  52.  
  53. columnFamily = "memo";
  54. columnName = "detail"+i;
  55. columnValue = "sssss zzz 111222 ";
  56. put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(columnName),Bytes.toBytes(columnValue));
  57. }
  58. System.out.println("----> put size:"+put.size());
  59.  
  60. helper.put(table,put);
  61.  
  62. }
  63.  
  64. private void bulkInsertTestTable2(String tableNameString) throws IOException{
  65. // String tableNameString = "testtable";
  66. if(!helper.existsTable(tableNameString)){
  67. helper.createTable(tableNameString,"info","ex","memo");
  68. }
  69.  
  70. List<Put> puts = new ArrayList<>();
  71. for(int i=0;i<10;i++){
  72. String rowKey = "rowKey"+i;
  73. Put put = new Put(Bytes.toBytes(rowKey));
  74.  
  75. String columnFamily = "info";
  76. String columnName = "username2";
  77. String columnValue = "user"+i;
  78. put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(columnName),Bytes.toBytes(columnValue));
  79.  
  80. columnFamily = "ex";
  81. columnName = "addr2";
  82. columnValue = "street "+i;
  83. put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(columnName),Bytes.toBytes(columnValue));
  84.  
  85. columnFamily = "memo";
  86. columnName = "detail2";
  87. columnValue = "aazzdd "+i;
  88. put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(columnName),Bytes.toBytes(columnValue));
  89.  
  90. System.out.println("put size:"+put.size());
  91. puts.add(put);
  92. }
  93. helper.bulkInsert2(tableNameString,puts);
  94. }

3、删除数据,由于hbase数据是三个维度的,所以删除数据有多种操作

  1. //根据rowKey删除所有行数据
  2. public void deleteByKey(String tableNameString,String rowKey) throws IOException{
  3. Table table = connection.getTable(TableName.valueOf(tableNameString));
  4. Delete delete = new Delete(Bytes.toBytes(rowKey));
  5.  
  6. table.delete(delete);
  7. table.close();
  8. }
  9.  
  10. //根据rowKey和列族删除所有行数据
  11. public void deleteByKeyAndFamily(String tableNameString,String rowKey,String columnFamily) throws IOException{
  12. Table table = connection.getTable(TableName.valueOf(tableNameString));
  13. Delete delete = new Delete(Bytes.toBytes(rowKey));
  14. delete.addFamily(Bytes.toBytes(columnFamily));
  15.  
  16. table.delete(delete);
  17. table.close();
  18. }
  19.  
  20. //根据rowKey、列族删除多个列的数据
  21. public void deleteByKeyAndFC(String tableNameString,String rowKey,
  22. String columnFamily,List<String> columnNames) throws IOException{
  23. Table table = connection.getTable(TableName.valueOf(tableNameString));
  24. Delete delete = new Delete(Bytes.toBytes(rowKey));
  25. for(String columnName:columnNames){
  26. delete.addColumns(Bytes.toBytes(columnFamily),Bytes.toBytes(columnName));
  27. }
  28. table.delete(delete);
  29. table.close();
  30. }

4、基本的查询,唯一要注意的是cell里的value必须按位移和长度来取

  1. //根据rowkey,获取所有列族和列数据
  2. public List<Cell> getRowByKey(String tableNameString,String rowKey) throws IOException{
  3. Table table = connection.getTable(TableName.valueOf(tableNameString));
  4.  
  5. Get get = new Get(Bytes.toBytes(rowKey));
  6. Result result = table.get(get);
  7. // Cell[] cells = result.rawCells();
  8. List<Cell> list = result.listCells();
  9. table.close();
  10. return list;
  11. }
  12.  
  1. //从Cell取Array要加上位移和长度,不然数据不正确
  2. public void dumpResult(Result result) {
  3. for (Cell cell : result.rawCells()) {
  4. System.out.println("Cell: " + cell +
  5. ", Value: " + Bytes.toString(cell.getValueArray(),
  6. cell.getValueOffset(), cell.getValueLength()));
  7. }
  8. }

5、过滤,这个是HBASE查询的重要部分

5.1、根据rowKey来过滤

  1. //根据rowKey过滤数据,rowKey可以使用正则表达式
  2. //返回rowKey和Cells的键值对
  3. public Map<String,List<Cell>> filterByRowKeyRegex(String tableNameString,String rowKey,CompareOperator operator) throws IOException{
  4. Table table = connection.getTable(TableName.valueOf(tableNameString));
  5. Scan scan = new Scan();
  6. //使用正则
  7. RowFilter filter = new RowFilter(operator,new RegexStringComparator(rowKey));
  8.  
  9. //包含子串匹配,不区分大小写。
  10. // RowFilter filter = new RowFilter(operator,new SubstringComparator(rowKey));
  11.  
  12. scan.setFilter(filter);
  13.  
  14. ResultScanner scanner = table.getScanner(scan);
  15. Map<String,List<Cell>> map = new HashMap<>();
  16. for(Result result:scanner){
  17. map.put(Bytes.toString(result.getRow()),result.listCells());
  18. }
  19. table.close();
  20. return map;
  21. }

5.2、根据列值、列值正则等方式过滤

  1. //根据列族,列名,列值(支持正则)查找数据
  2. //返回值:如果查询到值,会返回所有匹配的rowKey下的各列族、列名的所有数据(即使查询的时候这些列族和列名并不匹配)
  3. public Map<String,List<Cell>> filterByValueRegex(String tableNameString,String family,String colName,
  4. String value,CompareOperator operator) throws IOException{
  5. Table table = connection.getTable(TableName.valueOf(tableNameString));
  6. Scan scan = new Scan();
  7.  
  8. //正则匹配
  9. SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes(family),
  10. Bytes.toBytes(colName),operator,new RegexStringComparator(value));
  11.  
  12. //完全匹配
  13. // SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes(family),
  14. // Bytes.toBytes(colName),operator,Bytes.toBytes(value));
  15.  
  16. //SingleColumnValueExcludeFilter排除列值
  17.  
  18. //要过滤的列必须存在,如果不存在,那么这些列不存在的数据也会返回。如果不想让这些数据返回,设置setFilterIfMissing为true
  19. filter.setFilterIfMissing(true);
  20. scan.setFilter(filter);
  21.  
  22. ResultScanner scanner = table.getScanner(scan);
  23. Map<String,List<Cell>> map = new HashMap<>();
  24. for(Result result:scanner){
  25. map.put(Bytes.toString(result.getRow()),result.listCells());
  26. }
  27. return map;
  28. }

5.3、根据列名前缀、列名正则、多个列名等过滤

  1. //根据列名前缀过滤数据
  2. public Map<String,List<Cell>> filterByColumnPrefix(String tableNameString,String prefix) throws IOException{
  3. Table table = connection.getTable(TableName.valueOf(tableNameString));
  4.  
  5. //列名前缀匹配
  6. ColumnPrefixFilter filter = new ColumnPrefixFilter(Bytes.toBytes(prefix));
  7.  
  8. //QualifierFilter 用于列名多样性匹配过滤
  9. // QualifierFilter filter = new QualifierFilter(CompareOperator.EQUAL,new SubstringComparator(prefix));
  10.  
  11. //多个列名前缀匹配
  12. // MultipleColumnPrefixFilter multiFilter = new MultipleColumnPrefixFilter(new byte[][]{});
  13.  
  14. Scan scan = new Scan();
  15. scan.setFilter(filter);
  16.  
  17. ResultScanner scanner = table.getScanner(scan);
  18. Map<String,List<Cell>> map = new HashMap<>();
  19. for(Result result:scanner){
  20. map.put(Bytes.toString(result.getRow()),result.listCells());
  21. }
  22. return map;
  23. }

5.4、过滤器集合,多个过滤器同时按通过策略来过滤

  1. //根据列名范围以及列名前缀过滤数据
  2. public Map<String,List<Cell>> filterByPrefixAndRange(String tableNameString,String colPrefix,
  3. String minCol,String maxCol) throws IOException{
  4. Table table = connection.getTable(TableName.valueOf(tableNameString));
  5.  
  6. //列名前缀匹配
  7. ColumnPrefixFilter filter = new ColumnPrefixFilter(Bytes.toBytes(colPrefix));
  8.  
  9. //列名范围扫描,上下限范围包括
  10. ColumnRangeFilter rangeFilter = new ColumnRangeFilter(Bytes.toBytes(minCol),true,
  11. Bytes.toBytes(maxCol),true);
  12.  
  13. FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
  14. filterList.addFilter(filter);
  15. filterList.addFilter(rangeFilter);
  16.  
  17. Scan scan = new Scan();
  18. scan.setFilter(filterList);
  19.  
  20. ResultScanner scanner = table.getScanner(scan);
  21. Map<String,List<Cell>> map = new HashMap<>();
  22. for(Result result:scanner){
  23. map.put(Bytes.toString(result.getRow()),result.listCells());
  24. }
  25. return map;
  26. }

6、过滤器介绍

6.1、比较操作,如等于、大于、小于

  1. public enum CompareOperator {
  2. // Keeps same names as the enums over in filter's CompareOp intentionally.
  3. // The convertion of operator to protobuf representation is via a name comparison.
  4. /** less than */
  5. LESS,
  6. /** less than or equal to */
  7. LESS_OR_EQUAL,
  8. /** equals */
  9. EQUAL,
  10. /** not equal */
  11. NOT_EQUAL,
  12. /** greater than or equal to */
  13. GREATER_OR_EQUAL,
  14. /** greater than */
  15. GREATER,
  16. /** no operation */
  17. NO_OP,
  18. }

6.2、比较器,主要是继承ByteArrayComparable的类

  1. RegexStringComparator 支持正则表达式的值比较
  2.  
  3. Scan scan = new Scan();
  4. RegexStringComparator comp = new RegexStringComparator("you."); // 以 you 开头的字符串
  5. SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("family"), Bytes.toBytes("qualifier"), CompareOp.EQUAL, comp);
  6. scan.setFilter(filter);
  1. SubStringComparator 用于判断一个子串是否存在于值中,并且不区分大小写。
  2.  
  3. Scan scan = new Scan();
  4. SubstringComparator comp = new SubstringComparator("substr"); // 查找包含的字符串
  5. SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("family"), Bytes.toBytes("qualifier"), CompareOp.EQUAL, comp);
  6. scan.setFilter(filter);
  1. BinaryComparator 二进制比较器,不用反序列化直接进行字节比较,比较高效。
  2.  
  3. Scan scan = new Scan();
  4. BinaryComparator comp = new BinaryComparator(Bytes.toBytes("my hbase"));
  5. ValueFilter filter = new ValueFilter(CompareOp.EQUAL, comp);
  6. scan.setFilter(filter);
  1. BinaryPrefixComparator 前缀二进制比较器。只比较前缀是否相同。
  2.  
  3. Scan scan = new Scan();
  4. BinaryPrefixComparator comp = new BinaryPrefixComparator(Bytes.toBytes("test")); //
  5. SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("family"), Bytes.toBytes("qualifier"), CompareOp.EQUAL, comp);
  6. scan.setFilter(filter);

注意:BitComparator、RegexStringComparator、SubStringComparator只能与EQUAL和NOT_EQUAL搭配使用,因为这些比较器的compareTo()方法匹配时返回0,不匹配的时候返回1,如果和LESS或GREATER搭配就会出错。

基于字符串的比较器比基于字节的比较器更慢,也更消耗资源。

6.3、过滤器,部分介绍

  1. 行键过滤器
  2. RowFilter 对某一行的过滤。
  3.  
  4. Scan scan = new Scan();
  5. RowFilter filter = new RowFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("row1")));
  6. scan.setFilter(filter);
  1. 列族过滤器
  2. FamilyFilter 用于过滤列族(也可以在Scan 过程中通过设定某些列族来实现该功能)
  3.  
  4. Scan scan = new Scan();
  5. FamilyFilter filter = new FamilyFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("info"))); // 列族为 info
  6. scan.setFilter(filter);
  1. 列名过滤器
    QualifierFilter 列名全匹配
  2.  
  3. Scan scan = new Scan();
  4. QualifierFilter filter = new QualifierFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("username"))); // 列名为 username
  5. scan.setFilter(filter);
  6.  
  7. ColumnPrefixFilter 用于列名(Qualifier)前缀过滤,即包含某个前缀的所有列名。
  8.  
  9. Scan scan = new Scan();
  10. ColumnPrefixFilter filter = new ColumnPrefixFilter(Bytes.toBytes("addr")); // 前缀为 addr
  11. scan.setFilter(filter);
  12.  
  13. MultipleColumnPrefixFilter
  14. MultipleColumnPrefixFilter ColumnPrefixFilter 的行为类似,但可以指定多个列名(Qualifier)前缀。
  15.  
  16. Scan scan = new Scan();
  17. byte[][] prefixes = new byte[][]{Bytes.toBytes("my-prefix-1"), Bytes.toBytes("my-prefix-2")};
  18. MultipleColumnPrefixFilter filter = new MultipleColumnPrefixFilter(prefixes);
  19. scan.setFilter(filter);
  20.  
  21. ColumnRangeFilter 列名范围过滤器可以进行高效的列名内部扫描。关键字:已排序
  22.  
  23. Scan scan = new Scan();
  24. ColumnRangeFilter filter = new ColumnRangeFilter(Bytes.toBytes("minColumn"), true, Bytes.toBytes("maxColumn"), false);
  25. scan.setFilter(filter);
  26.  
  27. DependentColumnFilter 尝试找到该列所在的每一行,并返回该行具有相同时间戳的全部键值对。
  28.  
  29. Scan scan = new Scan();
  30. DependentColumnFilter filter = new DependentColumnFilter(Bytes.toBytes("family"), Bytes.toBytes("qualifier"));
  31. scan.setFilter(filter);
  1. 列值过滤器
  2. SingleColumnValueFilter 列值比较
  3.  
  4. 列族 info 下的列 username的列值和字符串 "admin" 相等的数据 :
  5. Scan scan = new Scan();
  6. SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("username"), CompareOp.EQUAL, Bytes.toBytes("admin"));
  7. scan.setFilter(filter);

6.4、代码:

https://github.com/asker124143222/hbaseHello

https://github.com/asker124143222/hbaseDemo

hbase 基本的JavaApi 数据操作及数据过滤(filter)的更多相关文章

  1. for循环中进行联网请求数据、for循环中进行异步数据操作,数据排序错乱问题解决;

    for循环中进行联网请求数据,由于网络请求是异步的,第一个网络请求还没有回调,第二次第三次以及后续的网络请求又已经发出去了,有可能后续的网络请求会先回调:这时我们接收到的数据的排序就会错乱:怎么才能让 ...

  2. MySQL的数据库,数据表,数据的操作

    数据库简介 概念 什么是数据库?简单来说,数据库就是存储数据的"仓库", 但是,光有数据还不行,还要管理数据的工具,我们称之为数据库管理系统! 数据库系统 = 数据库管理系统 + ...

  3. coreData数据操作

    // 1. 建立模型文件// 2. 建立CoreDataStack// 3. 设置AppDelegate 接着 // // CoreDataStack.swift // CoreDataStackDe ...

  4. SQL语言-----数据操作

    数据操作 增加数据,insert into 标准格式 insert into 表名 (字段的列表)value(数据列表): 使用set insert into 表名 set 字段1=值,2.....: ...

  5. MySQL表操作及数据操作

    表操作 表相当于一个文件,其形式与现实中的表格相同.表中的每条记录都有相应的字段,字段就类似于表格的表头. 表操作详细: #对表进行操作(文件) #首先要切换到指定库(即文件夹)下:use db1; ...

  6. JAVA IO操作:数据操作流:DataOutputStream和DataInputStream

    掌握DataOutputStream和DataInputStream的作用. 可以使用DataOutputStream和DataInputStream写入和读取数据. 在IO包中提供了两个与平台无关的 ...

  7. 笔记-mongodb数据操作

    笔记-mongodb数据操作 1.      数据操作 1.1.    插入 db.COLLECTION_NAME.insert(document) 案例: db.inventory.insertOn ...

  8. 使用Hive或Impala执行SQL语句,对存储在HBase中的数据操作

    CSSDesk body { background-color: #2574b0; } /*! zybuluo */ article,aside,details,figcaption,figure,f ...

  9. HBase伪分布式安装(HDFS)+ZooKeeper安装+HBase数据操作+HBase架构体系

    HBase1.2.2伪分布式安装(HDFS)+ZooKeeper-3.4.8安装配置+HBase表和数据操作+HBase的架构体系+单例安装,记录了在Ubuntu下对HBase1.2.2的实践操作,H ...

随机推荐

  1. 1071: [SCOI2007]组队

    1071: [SCOI2007]组队 https://lydsy.com/JudgeOnline/problem.php?id=1071 分析: dp+单调性. A*(hi–minH)+B*(si–m ...

  2. ELKStack入门篇(五)之实用架构解析

    (1)用户通过nginx或haproxy访问ELK日志统计平台,IP地址为keepalived的vip地址. (2)nginx将请求转发到kibana (3)kibana到elasticsearch获 ...

  3. tidb测试环境安装,离线部署

    1.环境以及规划 机器:centos7.5 ; 文件系统为ext4:内存16g:cpu8核,共三个节点: ip hostname roles --- tidb tipd tikv --- tidb t ...

  4. JS的发布订阅模式

    JS的发布订阅模式 这里要说明一下什么是发布-订阅模式 发布-订阅模式里面包含了三个模块,发布者,订阅者和处理中心.这里处理中心相当于报刊办事大厅.发布者相当与某个杂志负责人,他来中心这注册一个的杂志 ...

  5. dsp6657的helloworld例程测试-第一篇

    环境搭建可以参考http://blog.sina.com.cn/s/blog_ed2e19900102xi2j.html 1. 先从mcsdk导入工程,helloworld例程 2. 提示有错误,估计 ...

  6. Vue视图

    1. 基本模板语法 1.1 插值 文本 数据绑定最常见的形式就是使用“Mustache”语法 (双大括号) 的文本插值 v-text 指令也可以用于数据绑定,如果要更新部分的 textContent ...

  7. SOAPUI参数中xml中CDATA包含问题

    <![CDATA[ <Request> <CardNo>000002629518</CardNo> <SecrityNo/> <BankTr ...

  8. linux下,将一个目录中的图片文件合成为gif图片

    # {} 为文件所在目录位置 # {} 为gif图片位置 convert -delay -depth -layers optimize -quality -loop {} {}

  9. PHP基础知识试题

    转载于:http://www.php.cn/toutiao-415599.html 1.PHP中传值与传引用的区别,什么时候传值,什么时候传引用? 按值传递:函数范围内对值任何改变在函数外部都会被忽略 ...

  10. JavaScript 中函数的参数

    functionName(parameter1, parameter2, parameter3) { // 要执行的代码…… } 参数规则 JavaScript 函数定义时形参没有指定数据类型. Ja ...