Java操作hbase总结

用过以后,总得写个总结,不然,就忘喽。

一、寻找操作的jar包。

java操作hbase,首先要考虑到使用hbase的jar包。

因为咱装的是CDH5,比较方便,使用SecureCRT工具,远程连接到你安装的那台服务器上。

jar包的存放位置在/opt/cloudera/parcels/CDH/lib/hbase,找到,下载下来。

在当前路径下,有一个lib包,里面是支持hbase的hadoop的jar包,根据需求,可以下载下来。

二、找一个API文档当成手册,哪里不会查哪里

      百度分享,http://pan.baidu.com/s/1jICqdgy,可以下载。

三、java操作Hbase。

     构造函数:

     

public static Configuration configuration;
    static{
        configuration = HBaseConfiguration.create();
        configuration.set("hbase.master","ip1:60000");
        configuration.set("hbase.zookeeper.quorum", "ip1:2181,ip2:2181") ;
    }

     1、如何创建一个hbase表并put数据。

public static void creaTable(String tablename) throws Exception{
        HBaseAdmin admin = new HBaseAdmin(configuration);
        if(admin.tableExists(tablename)){
            admin.disableTable(tablename);
            admin.deleteTable(tablename);
            System.out.println("开始创建表!");
        }
        System.out.println("新的表正在创建中!!!");
        HTableDescriptor tableDescriptor = new HTableDescriptor(tablename);
        tableDescriptor.addFamily(new HColumnDescriptor("cf1"));
        admin.createTable(tableDescriptor);
        
        Put put = new Put("123".getBytes());
        put.add("cf1".getBytes(), "colum1".getBytes(), "value1".getBytes()) ;
        put.add("cf1".getBytes(), "colum2".getBytes(), "value2".getBytes()) ;
        put.add("cf1".getBytes(), "colum3".getBytes(), "value3".getBytes()) ;
        
        Put put2 = new Put("234".getBytes()) ;
        put2.add("cf1".getBytes(), "colum1".getBytes(), "value1".getBytes()) ;
        put2.add("cf1".getBytes(), "colum2".getBytes(), "value2".getBytes()) ;
        put2.add("cf1".getBytes(), "colum3".getBytes(), "value3".getBytes()) ;
        
        HTable table = new HTable(configuration, tablename);
        table.put(put);
        table.put(put2);    
    }

         2、删除hbase中的table里面的rowkey

1 public static void deleteRow(String tableName,String rowKey) throws Exception{
2         HTable hTable = new HTable(configuration,tableName);
3         Delete delete = new Delete(rowKey.getBytes());
4         List<Delete> list = new ArrayList<Delete>();
5         list.add(delete);
6         hTable.delete(list);
7     }

       3、查询row = rowKey的数据

 1 /**
 2      * 查询row = rowKey的数据
 3      * @param tableName
 4      * @param rowKey
 5      * @throws Exception
 6      */
 7     public static void getRow(String tableName,String rowKey) throws Exception{
 8         HTable hTable = new HTable(configuration, tableName);
 9         Get get = new Get(rowKey.getBytes());
10         Result result = hTable.get(get);
11         for(KeyValue value:result.raw()){
12             System.out.println("cf:"+new String(value.getFamily())+new String(value.getQualifier())+"="+new String(value.getValue()));
13         }
14     }

     4、查询rowkey在startRow和endRow之间的数据,及rowkey的范围查询

   Put、Delete与Get对象都是Row的子类,从该继承关系中我们就可以了解到Get、Delete与Pu对象本身就只能进行单行的操作,

     HBase客户端还提供了一套能够进行全表扫描的API,方便用户能够快速对整张表进行扫描,以获取想要的结果---scan、

 1 /**
 2      * 查询rowkey在startRow和endRow之间的数据
 3      * @param tablename
 4      * @param startRow
 5      * @param endRow
 6      * @throws Exception 
 7      */
 8     public static void getBetweenRow(String tableName,String startRow,String stopRow) throws Exception{
 9         HTable table = new HTable(configuration, tableName);
10         Scan scan = new Scan();
11         scan.addColumn("cf1".getBytes(), "colum1".getBytes());
12         scan.addColumn("cf1".getBytes(), "colum2".getBytes());
13         scan.addColumn("cf1".getBytes(), "colum3".getBytes());
14         
15         scan.setStartRow(startRow.getBytes());
16         scan.setStopRow(stopRow.getBytes());
17         
18         ResultScanner scanner = table.getScanner(scan);
19         
20         for(Result result:scanner){
21             for(KeyValue value:result.raw()){
22                 System.out.println("cf:"+new String(value.getFamily())+new String(value.getQualifier())+"="+new String(value.getValue()));
23             }
24         }
25     }

  

     

原文地址:https://www.cnblogs.com/invban/p/5667701.html