HbaseCURD操作java API

来源:互联网 发布:证券公司 知乎 编辑:程序博客网 时间:2024/06/15 22:28

public class HbaseCURD {

private Configuration conf = null;
private Connection con = null;

/**由于是测试类,没有main方法,要提前加载一些配置项。所以用@Before *  * @throws IOException */@Beforepublic void init() throws IOException {    conf = HBaseConfiguration.create();    conf.set("hbase.zookeeper.quorum", "worker05:2181,worker06:2181,worker07:2181");}

/**
* 在Hbase中添加people表并创建两个列簇:info、data
* 并设置创建的版本为3(也就是最新版本)
* @throws IOException
*/
@Test
public void testCreateTable() throws IOException {
HBaseAdmin admin = new HBaseAdmin(conf);
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(“people”));
HColumnDescriptor hcdInfo = new HColumnDescriptor(“info”);
HColumnDescriptor hcdData = new HColumnDescriptor(“data”);
hcdInfo.setMaxVersions(3);
htd.addFamily(hcdInfo);
htd.addFamily(hcdData);
admin.createTable(htd);
//用完关闭
admin.close();
}


/**插入数据 * @throws IOException **/

@Test
public void testPut(String tableName) throws IOException {
HTable table = new HTable(conf, tableName);
Put put = new Put(Bytes.toBytes(“row001”));
put.addColumn(Bytes.toBytes(“info”), Bytes.toBytes(“name”), Bytes.toBytes(“lingxin”));
put.addColumn(Bytes.toBytes(“info”), Bytes.toBytes(“age”), Bytes.toBytes(“57”));
put.addColumn(Bytes.toBytes(“info”), Bytes.toBytes(“money”), Bytes.toBytes(30000));
table.put(put);
table.close();
}


/**测试插入100万条数据**/@Testpublic void testPutAll() throws IOException {    HTable table = new HTable(conf, "people");        List<Put> puts = new ArrayList<Put>(10000);    for(int i = 1; i < 1000000; i++) {        Put put = new Put(Bytes.toBytes("row" + i));        put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("lingxin" + i));        put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("57"));        put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("money"), Bytes.toBytes(30000));        puts.add(put);        //注意!!!不能一次写一条。即会造成资源的浪费和磁盘的损坏、更会可能使Hbase负载增加而挂掉        //每隔10000,写一次        if(i % 10000 == 0) {            table.put(puts);            puts = new ArrayList<Put>(10000);        }    }    table.put(puts);    table.close();    //以下方式不可取    /*      for(int i=1; i<1000000; i++) {                Put put = new Put(Bytes.toBytes("row"+i));                put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("lingxin"+i));                put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("57"));                put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("money"), Bytes.toBytes(30000));                puts.add(put);            }            table.put(puts);            table.close();*/}

/**查看某个cell的值**/@Testpublic void testGet() throws IOException {    Table table = con.getTable(TableName.valueOf("people"));    Get get = new Get(Bytes.toBytes("row9999"));    Result resut = table.get(get);    String r = Bytes.toString(resut.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));    System.out.println(r);    table.close();}/**查看某个rowkey范围的数据,按字典顺序排序**/@Testpublic void testScan() throws IOException {    Table table = con.getTable(TableName.valueOf("people"));    Scan scan = new Scan(Bytes.toBytes("row010000"), Bytes.toBytes("row110"));    ResultScanner scanner = table.getScanner(scan);    for(Result result : scanner) {        String r = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));        System.out.println(r);    }    table.close();}

/**组合使用扫描器缓存和批量大小**/@Testpublic void testScanWithCacheAndBatch(int caching, int batch) throws IOException {    Table table = con.getTable(TableName.valueOf("people"));    Scan scan = new Scan(Bytes.toBytes("row010000"), Bytes.toBytes("row110"));    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!    //根据提供表的特性,(宽表,还是窄表,设置不同的caching和batch值)    scan.setCaching(caching);//设置每次扫描的打印行数      scan.setBatch(batch);//设置每次扫描的打印列数    //!!!!!!!!!!!!!!!!!!!!!!!!!!    ResultScanner scanner = table.getScanner(scan);    for(Result result : scanner) {        String r = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));        System.out.println(r);    }    table.close();}@Testpublic void testDel() throws IOException {    Table table = con.getTable(TableName.valueOf("people"));//  HTable table = new HTable(conf, "people");      Delete delete = new Delete(Bytes.toBytes("row9999"));    table.delete(delete);    table.close();}

}

1 0