Java操做hbase总结

用过之后,总得写个总结,否则,就忘喽。java

1、寻找操做的jar包。服务器

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

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

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

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

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

      百度分享,http://pan.baidu.com/s/1jICqdgy,能够下载。对象

3、java操做Hbase。blog

     构造函数:继承

     

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

     一、如何建立一个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);    
    }

         二、删除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     }

 

       三、查询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     }

     四、查询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     }
相关文章
相关标签/搜索