zoukankan      html  css  js  c++  java
  • Hbase访问方式之Java API

    Hbase的访问方式
    1、Native Java API:最常规和高效的访问方式;
    2、HBase Shell:HBase的命令行工具,最简单的接口,适合HBase管理使用;
    3、Thrift Gateway:利用Thrift序列化技术,支持C++,PHP,Python等多种语言,适合其他异构系统在线访问HBase表数据;
    4、REST Gateway:支持REST 风格的Http API访问HBase, 解除了语言限制;
    5、MapReduce:直接使用MapReduce作业处理Hbase数据;
    6、使用Pig/hive处理Hbase数据。

    常用Java API的用法:

    1、加载配置

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. Configuration config = HBaseConfiguration.create();   
    2. //可以自定义配置,也可以从自定义配置文件中读取  
    3. /*config.set("hbase.zookeeper.property.clientPort", "4181"); 
    4. config.set("hbase.zookeeper.quorum", "hadoop.datanode5.com,hadoop.datanode2.com,hadoop.datanode3.com"); 
    5. config.set("hbase.master", "hadoop.datanode3.com\:600000");*/  

    2、表的创建、表信息修改、表删除

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. HBaseAdmin admin = new HBaseAdmin(config);  
    2. //创建表  
    3. HTableDescriptor htd = new HTableDescriptor(tableName);  
    4. htd.addFamily(new HColumnDescriptor("cf1"));  
    5. htd.addFamily(new HColumnDescriptor("cf2"));  
    6. admin.createTable(htd);  
    7. //修改表信息  
    8. admin.disableTable(tableName);  
    9. // modifying existing ColumnFamily  
    10. admin.modifyColumn(tableName, new HColumnDescriptor("cf1"));    
    11. admin.enableTable(tableName);   
    12. //删除表  
    13. admin.disableTable(Bytes.toBytes(tableName));  
    14. admin.deleteTable(Bytes.toBytes(tableName));  

    3、添加记录

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. /** 在多次使用时,建议用HTablePool 
    2.   HTable table = new HTable(config, tableName);  
    3.   => 
    4.   HTablePool pool = new HTablePool(config, 1000); 
    5.   HTableInterface table = pool.getTable(tableName);*/  
    6. HTable table = new HTable(config, tableName);  
    7.   
    8. /** 
    9.  * 在插入操作时,默认不适用任何缓存 
    10.  * 可自定义使用缓存,以及缓存大小 
    11.  * 每个任务最后需要手工调用 flushCommits(); 
    12.  */  
    13. /*table.setAutoFlush(false); 
    14. table.setWriteBufferSize(1024);*/  
    15.   
    16. Put put1 = new Put(Bytes.toBytes(rowKey));  
    17. if (ts == 0) {  
    18.     put1.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));  
    19. else {  
    20.        //自定义版本时,从自定义的版本号,类型为long  
    21.     put1.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), ts,Bytes.toBytes(value));  
    22. }  
    23. table.put(put1);  
    24. //table.flushCommits();  

    4、查询,根据Rowkey查询

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. Get get1 = new Get(Bytes.toBytes(rowKey));  
    2. Result result = table.get(get1);  
    3. System.out.println("get result:" + Bytes.toString(result.getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier))));  
    4. Result[] result = table.get(List<Get>);//查询指定Rowkey的多条记录  

    5、查询,指定条件和rowkey区间查询

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. Scan scan = new Scan();  
    2. //默认缓存大小为1,设置成一个合理的值,可以减少scan过程中next()的时间开销,代价是客户端的内存  
    3. scan.setCaching(500);  
    4. scan.setCacheBlocks(false);  
    5.   
    6. //根据startRowKey、endRowKey查询  
    7. //Scan scan = new Scan(Bytes.toBytes("startRowKey"), Bytes.toBytes("endRowKey"));  
    8.   
    9. //rowKey之外的过滤条件,在List中可以add;  
    10. /**List<Filter> filters = new ArrayList<Filter>(); 
    11. Filter filter = new SingleColumnValueFilter("familyName".getBytes(),  
    12.         "qualifierName".getBytes(),  
    13.         CompareOp.EQUAL, 
    14.         Bytes.toBytes("value")); 
    15. filters.add(filter); 
    16. scan.setFilter(new FilterList(filters));*/  
    17.   
    18. ResultScanner scanner = table.getScanner(scan);  
    19.   
    20. System.out.println("scan result list:");  
    21.           
    22. for (Result result : scanner) {  
    23.     System.out.println(Bytes.toString(result.getRow()));  
    24.     System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("data"), Bytes.toBytes("data1"))));  
    25.     System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("data"), Bytes.toBytes("data2"))));  
    26. }  
    27. scanner.close();  

    参考:

    1、http://www.taobaotest.com/blogs/1605

    2、http://abloz.com/hbase/book.html#data_model_operations(官网示例)

  • 相关阅读:
    IntelliJ IDEA 安装和破解教程
    IntelliJ IDEA 快捷键
    分布式事务框架-seata初识
    Spring超详细总结
    spring注解之@Import注解的三种使用方式
    双亲委托模型
    日志管理-log4j与slf4j的使用
    最详细的自定义Spring Boot Starter开发教程
    深入JVM(二)JVM概述
    一篇长文说 git 基础
  • 原文地址:https://www.cnblogs.com/bluecoder/p/3824268.html
Copyright © 2011-2022 走看看