Hbase 1.x版本和2.x版本,虽然进行了大量的重构和优化,一些API也已经能抛弃,单核心API没有改多少。
本处:HBASE 2.0.5版本
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","mycat01,mycat02,mycat03");
conf.set("hbase.zookeeper.property.clientPort", "2181");
Connect cnn=ConnectionFactory.createConnection(conf);
Admin admin=cnn.getAdmin();
Table mk=cnn.getTable(TableName.valueOf("test"));
public static void createNameSpace(Admin admin) throws IOException {
NamespaceDescriptor mkNameSpace = NamespaceDescriptor.create("mktest").build();
admin.createNamespace(mkNameSpace);
}
结果:
2.4.1 :036 > list_namespace
NAMESPACE
default
hbase
mktest 3 row(s)
Took 0.4493 seconds
public static void createOneColumnFamilyTable(Admin admin) throws IOException {
TableDescriptorBuilder mk = TableDescriptorBuilder.newBuilder(TableName.valueOf("mk1"));
ColumnFamilyDescriptorBuilder columnBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("user"));
ColumnFamilyDescriptor familyDescriptor = columnBuilder.build();
mk.setColumnFamily(familyDescriptor);
admin.createTable(mk.build());
}
结果如下
2.4.1 :042 > list 'mktest:.*'
TABLE mktest:mk1 1 row(s)
Took 0.0219 seconds => ["mktest:mk1"]
public static void createMultiPartColumnFamily(Admin admin) throws IOException {
TableDescriptorBuilder mk = TableDescriptorBuilder.newBuilder(TableName.valueOf("mktest:mk2"));
ColumnFamilyDescriptorBuilder columnBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("user"));
ColumnFamilyDescriptorBuilder columnBuilder2 = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("socre"));
List<ColumnFamilyDescriptor> columnFamilyDescriptors=new ArrayList<>();
columnFamilyDescriptors.add(columnBuilder.build());
columnFamilyDescriptors.add(columnBuilder2.build());
mk.setColumnFamilies(columnFamilyDescriptors);
admin.createTable(mk.build());
}
结果如下:
2.4.1 :043 > list 'mktest:.*'
TABLE
mktest:mk1
mktest:mk2
2 row(s)
Took 0.0448 seconds
=> ["mktest:mk1", "mktest:mk2"]
public static void listTables(Admin admin) throws IOException {
TableName[] tableNames = admin.listTableNames();
for (TableName tableName : tableNames) {
System.out.println(tableName);
}
}
结果如下
mktest:mk1
mktest:mk2
test_mk
test_mk2
public static void listTablesByNameSpace(Admin admin) throws IOException {
TableName[] tableNames = admin.listTableNamesByNamespace("mktest");
for (TableName tableName : tableNames) {
System.out.println(tableName);
}
}
结果如下
mktest:mk1
mktest:mk2
public static void scanWholeTable(Table table) throws IOException {
Scan scan=new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result rs : scanner) {
System.out.println("-------------------------------");
System.out.println("rowKey:"+Bytes.toString(rs.getRow()));
Cell[] cells = rs.rawCells();
for (Cell c : cells) {
String columnName = Bytes.toString(c.getQualifierArray(), c.getQualifierOffset(), c.getQualifierLength());
String columnValue=null;
if(columnName.equals("income")||columnName.equals("age")){
int result=Bytes.toInt(c.getValueArray());
columnValue=Integer.toString(result);
}else{
columnValue = Bytes.toString(c.getValueArray(), c.getValueOffset(), c.getValueLength());
}
System.out.println(columnName +":"+columnValue);
}
}
}
结果如下
-------------------------------
rowKey:rk001
name:cm
-------------------------------
rowKey:rk002
age:25
income:28
name:tom
sex:female
-------------------------------
rowKey:rk003
age:25
income:28
name:sam
sex:female
public static void scanTableBetween(Table table) throws IOException {
Scan scan=new Scan();
scan.withStartRow(Bytes.toBytes("rk002"));
// scan.withStopRow(Bytes.toBytes("rk004")); // 本处数据不够,没有测试---不包含结束行
ResultScanner scanner = table.getScanner(scan);
for (Result rs : scanner) {
System.out.println("-------------------------------");
System.out.println("rowKey:"+Bytes.toString(rs.getRow()));
Cell[] cells = rs.rawCells();
for (Cell c : cells) {
String columnName = Bytes.toString(c.getQualifierArray(), c.getQualifierOffset(), c.getQualifierLength());
String columnValue=null;
if(columnName.equals("income")||columnName.equals("age")){
int result=Bytes.toInt(c.getValueArray());
columnValue=Integer.toString(result);
}else{
columnValue = Bytes.toString(c.getValueArray(), c.getValueOffset(), c.getValueLength());
}
System.out.println(columnName +":"+columnValue);
}
}
}
public static void getRowData(Table table) throws IOException {
Get get=new Get(Bytes.toBytes("rk002"));
Result rs = table.get(get);
Cell[] cells = rs.rawCells();
for (Cell c : cells) {
String columnName = Bytes.toString(c.getQualifierArray(), c.getQualifierOffset(), c.getQualifierLength());
String columnValue=null;
if(columnName.equals("income")||columnName.equals("age")){
int result=Bytes.toInt(c.getValueArray());
columnValue=Integer.toString(result);
}else{
columnValue = Bytes.toString(c.getValueArray(), c.getValueOffset(), c.getValueLength());
}
System.out.println(columnName +":"+columnValue);
}
}
结果如下
age:25
income:28
name:tom
sex:female
public static void getRowDataList(Table table) throws IOException {
List<Get> gets=new ArrayList<>();
gets.add(new Get(Bytes.toBytes("rk001")));
gets.add(new Get(Bytes.toBytes("rk002")));
gets.add(new Get(Bytes.toBytes("rk003")));
gets.add(new Get(Bytes.toBytes("rk004")));
Result[] rs = table.get(gets);
for (Result r : rs) {
Cell[] cells = r.rawCells();
for (Cell c : cells) {
String columnName = Bytes.toString(c.getQualifierArray(), c.getQualifierOffset(), c.getQualifierLength());
String columnValue=null;
if(columnName.equals("income")||columnName.equals("age")){
int result=Bytes.toInt(c.getValueArray());
columnValue=Integer.toString(result);
}else{
columnValue = Bytes.toString(c.getValueArray(), c.getValueOffset(), c.getValueLength());
}
System.out.println(columnName +":"+columnValue);
}
}
}
结果如下
name:cm
age:25
income:28
name:tom
sex:female
age:25
income:28
name:sam
sex:female
public static void deleteRowData(Table table) throws IOException {
Delete delete=new Delete("rk004".getBytes());
table.delete(delete);
System.out.println("删除数据!rk004");
}
结果如下
删除数据!rk004
public static void putRowData(Table table) throws IOException {
Put put=new Put(Bytes.toBytes("rk002"));
put.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("name") , Bytes.toBytes("tom"));
put.addColumn(Bytes.toBytes("info2"), Bytes.toBytes("age"), Bytes.toBytes(24));
put.addColumn(Bytes.toBytes("info2"), Bytes.toBytes("sex"),Bytes.toBytes("female"));
put.addColumn(Bytes.toBytes("info2"), Bytes.toBytes("income"), Bytes.toBytes(2000.0));
Put put2=new Put(Bytes.toBytes("rk003"));
put2.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("name") , Bytes.toBytes("sam"));
put2.addColumn(Bytes.toBytes("info2"), Bytes.toBytes("age"), Bytes.toBytes(34));
put2.addColumn(Bytes.toBytes("info2"), Bytes.toBytes("sex"),Bytes.toBytes("female"));
put2.addColumn(Bytes.toBytes("info2"), Bytes.toBytes("income"), Bytes.toBytes(3400.0));
List<Put> puts=new ArrayList<>();
puts.add(put);
puts.add(put2);
table.put(puts);
table.close();
}
结果如下
2.4.1 :055 > scan 'test_mk2'
ROW COLUMN+CELL
rk001 column=info2:name, timestamp=1554120098856, value=cm
rk002 column=info2:age, timestamp=1554121338856, value=\x00\x00\x00\x18
rk002 column=info2:income, timestamp=1554121338856, value=@\x9F@\x00\x00\x00\x00\x00
rk002 column=info2:name, timestamp=1554121338856, value=tom
rk002 column=info2:sex, timestamp=1554121338856, value=female
rk003 column=info2:age, timestamp=1554121338856, value=\x00\x00\x00"
rk003 column=info2:income, timestamp=1554121338856, value=@\xAA\x90\x00\x00\x00\x00\x00
rk003 column=info2:name, timestamp=1554121338856, value=sam
rk003 column=info2:sex, timestamp=1554121338856, value=female
3 row(s)