几个常用HBase API说明
(2017-09-12 09:44:31)
标签:
大数据教育文化 |
说明:小编的旧版本HBase0.94,小编的新版本HBase1.2.6
一、常用新旧版本HBase API对照:
1. 创建HBaseAdmin 实例
旧:HBaseAdmin admin = new HBaseAdmin(conf);
新:Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
2. HTableDescriptor
旧:HTableDescriptor desc = new HTableDescriptor(Bytes.toBytes("testtable"));
新:HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("GoodsOrders"));
3. admin.deleteTable
旧:admin.deleteTable(Bytes.toBytes("testtable"));
新:admin.deleteTable(TableName.valueOf("testtable"));
4. admin.disableTable
旧:admin.disableTable(Bytes.toBytes("testtable"));
新:admin.disableTable(TableName.valueOf("testtable"));
5. admin.isTableAvailable
旧:admin.isTableAvailable(Bytes.toBytes("testtable"));
新:admin.isTableAvailable(TableName.valueOf("testtable"));
6. admin.isTableAvailable
旧:admin.isTableEnabled(Bytes.toBytes("testtable"));
新:admin.isTableEnabled(TableName.valueOf("testtable"));
7. Table取值
旧: HTable table = new HTable(conf, "testtable");
新:Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("testtable"));
8. Result取值
旧:for (KeyValue kv : result.raw()) {Bytes.toString(kv.getRow()));}
新:for(Cell cell:result.rawCells()){new String(CellUtil.cloneRow(cell)));}
二、常用HBase API功能:
1. 读取一条记录的几中方法
(1)for (KeyValue kv : result.raw()) {
System.out.print("行健: "+ Bytes.toString(kv.getRow()));
System.out.print("列簇: "+ Bytes.toString(kv.getFamily()));
System.out.print(" 列: "+ Bytes.toString(kv.getQualifier()));
System.out.print(" 值: "+ Bytes.toString(kv.getValue()));
System.out.println("时间戳: "+kv.getTimestamp());
(2)for(Cell cell:result.rawCells()){
System.out.print("行健: "+new String(CellUtil.cloneRow(cell)));
System.out.print("列簇: "+new String(CellUtil.cloneFamily(cell)));
System.out.print(" 列: "+new String(CellUtil.cloneQualifier(cell)));
System.out.print(" 值: "+new String(CellUtil.cloneValue(cell)));
System.out.println("时间戳: "+cell.getTimestamp());
}
(3)for (Cell cell : cells) {
System.out.print(new String(cell.getRowArray()).substring(cell.getRowOffset(),
cell.getRowOffset() + cell.getRowLength()));
System.out.print(new String(cell.getFamilyArray()).substring(cell.getFamilyOffset(),
cell.getFamilyOffset()+ cell.getFamilyLength()));
System.out.print(new String(cell.getFamilyArray()).substring(
cell.getQualifierOffset(),cell.getQualifierOffset() + cell.getQualifierLength()));
System.out.println(new String(new String(cell.getValueArray()) + " "+ cell.getValueOffset() + " "+ cell.getValueLength()));
System.out.print(cell.getTimestamp());
}
2. 建表的方法
Configuration conf = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("tablename"));
HColumnDescriptor coldef = new HColumnDescriptor(Bytes.toBytes("family1"));
desc.addFamily(coldef);
admin.createTable(desc);
boolean avail = admin.isTableAvailable(TableName.valueOf("tablename "));
System.out.println(avail);
3. 列举指定表结构
HTableDescriptor htd = admin.getTableDescriptor(TableName.valueOf("tablename "));
System.out.println(htd);
4. 修改表结构
HTableDescriptor htd1 = admin.getTableDescriptor(name);
HColumnDescriptor coldef = new HColumnDescriptor(Bytes.toBytes("colfamily"));
htd1.addFamily(coldef);
htd1.setMaxFileSize(1024 * 1024 * 1024L);
admin.disableTable(name);
admin.modifyTable(name, htd1);
admin.enableTable(name);
5. 批处理
List batch = new ArrayList();
batch.add(put);
batch.add(get1);
batch.add(delete);
batch.add(get2);
Object[] results = new Object[batch.size()];
table.batch(batch, results);
6. 删除操作
Admin admin = connection.getAdmin();
TableName tablename = TableName.valueOf("GoodsOrders");
if(admin.tableExists(tablename)) {
admin.disableTable(tablename);
HTableDescriptor newtd = admin.getTableDescriptor(tablename);
newtd.removeFamily(Bytes.toBytes("books"));
HColumnDescriptor newhcd = new HColumnDescriptor("addfamilytest");
newhcd.setMaxVersions(10);
newhcd.setKeepDeletedCells(KeepDeletedCells.TRUE);
newtd.addFamily(newhcd);
admin.modifyTable(Bytes.toBytes("rd_ns:itable"),newtd);
admin.modifyTable(tablename,newtd);
admin.enableTable(tablename);
admin.close();
7. Put读数据
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("testtable7"));
Put put = new Put(Bytes.toBytes("row1"));
put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),Bytes.toBytes("val1"));
put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual2"),Bytes.toBytes("val2"));
table.put(put);
List puts = new ArrayList(); // co PutListExample-1-CreateList Create a list that holds the Put instances.
Put put1 = new Put(Bytes.toBytes("row1"));
put1.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),Bytes.toBytes("val1"));
put1.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),
Bytes.toBytes("val1"));
puts.add(put1); // co PutListExample-2-AddPut1 Add put to list.
Put put2 = new Put(Bytes.toBytes("row2"));
put2.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),Bytes.toBytes("val2"));
puts.add(put2);
table.put(puts);
8. Get读数据
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("testtable1"));
Get get = new Get(Bytes.toBytes("row1"));
get.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"));
Result result = table.get(get);
byte[] val = result.getValue(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"));
System.out.println("Value: " + Bytes.toString(val));
List gets = new ArrayList();
9. Scan读数据
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes(beginRowKey));
scan.setStopRow(Bytes.toBytes(endRowKey));
scan.setMaxVersions(1);
scan.setCaching(20);
scan.setBatch(10);
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf(tableName));
ResultScanner rs = table.getScanner(scan);
10. Filter读数据
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("testtable"));
Filter filter1 = new FamilyFilter(CompareFilter.CompareOp.LESS,
new BinaryComparator(Bytes.toBytes("colfam3")));
Scan scan = new Scan();
scan.setFilter(filter1);
ResultScanner scanner = table.getScanner(scan);
11. MapReduce On HBase举例
public static class Map extends
public static class Reduce extends