hbase版本:2.3.5
junit
junit
4.11
org.apache.hbase
hbase-client
2.3.5
org.apache.hbase
hbase-common
2.3.5
private Connection connection = null;
private Table table = null;
@Before
public void init() throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","192.168.153.135");
conf.set("hbase.zookeeper.property.clientPort","2181");
connection = ConnectionFactory.createConnection(conf);
}
@Test
public void testConnection(){
System.out.println(connection);
}
效果图:
@After
public void close() throws IOException {
if (connection!=null)
connection.close();
}
@Test
public void createNamespace() throws IOException {
Admin admin = connection.getAdmin();
NamespaceDescriptor.Builder builder = NamespaceDescriptor.create("bigdata888");
NamespaceDescriptor namespaceDescriptor = builder.build();
admin.createNamespace(namespaceDescriptor);
}
@Test
public void createTable() throws IOException {
Admin admin = connection.getAdmin();
TableName tableName = TableName.valueOf("bigdata888:test001");
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);
ColumnFamilyDescriptor columnFamilyDescriptor1 = ColumnFamilyDescriptorBuilder.of("baseinfo");
ColumnFamilyDescriptor columnFamilyDescriptor2 = ColumnFamilyDescriptorBuilder.of("schoolinfo");
tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor1);
tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor2);
TableDescriptor descriptor = tableDescriptorBuilder.build();
admin.createTable(descriptor);
}
@Test
public void insertValue() throws IOException {
table = connection.getTable(TableName.valueOf("bigdata888:test001"));
Put rowkey1 = new Put(Bytes.toBytes("rowkey1"));
rowkey1.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("name"),Bytes.toBytes("zhangsan"));
rowkey1.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("gender"),Bytes.toBytes("male"));
rowkey1.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("pwd"),Bytes.toBytes("111"));
rowkey1.addColumn(Bytes.toBytes("schoolinfo"),Bytes.toBytes("name"),Bytes.toBytes("njdx"));
rowkey1.addColumn(Bytes.toBytes("schoolinfo"),Bytes.toBytes("location"),Bytes.toBytes("nj"));
table.put(rowkey1);
}
@Test
public void insertValue2() throws IOException {
table = connection.getTable(TableName.valueOf("bigdata888:test001"));
Put rowkey2 = new Put(Bytes.toBytes("rowkey2"));
rowkey2.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("name"),Bytes.toBytes("lisi"));
rowkey2.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("gender"),Bytes.toBytes("female"));
rowkey2.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("pwd"),Bytes.toBytes("222"));
rowkey2.addColumn(Bytes.toBytes("schoolinfo"),Bytes.toBytes("name"),Bytes.toBytes("bjdx"));
rowkey2.addColumn(Bytes.toBytes("schoolinfo"),Bytes.toBytes("location"),Bytes.toBytes("bj"));
Put rowkey3 = new Put(Bytes.toBytes("rowkey3"));
rowkey3.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("name"),Bytes.toBytes("wangwu"));
rowkey3.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("gender"),Bytes.toBytes("male"));
rowkey3.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("pwd"),Bytes.toBytes("333"));
rowkey3.addColumn(Bytes.toBytes("schoolinfo"),Bytes.toBytes("name"),Bytes.toBytes("qhdx"));
rowkey3.addColumn(Bytes.toBytes("schoolinfo"),Bytes.toBytes("location"),Bytes.toBytes("bj"));
ArrayList list = new ArrayList();
list.add(rowkey2);
list.add(rowkey3);
table.put(list);
}
@Test
public void scanValue() throws IOException {
table = connection.getTable(TableName.valueOf("bigdata888:test001"));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result :
scanner) {
byte[] stuname = result.getValue(Bytes.toBytes("baseinfo"), Bytes.toBytes("name"));
byte[] gender = result.getValue(Bytes.toBytes("baseinfo"), Bytes.toBytes("gender"));
byte[] pwd = result.getValue(Bytes.toBytes("baseinfo"), Bytes.toBytes("pwd"));
byte[] schoolname = result.getValue(Bytes.toBytes("schoolinfo"), Bytes.toBytes("name"));
byte[] location = result.getValue(Bytes.toBytes("schoolinfo"), Bytes.toBytes("location"));
System.out.println("-----------------------------------------");
System.out.print(Bytes.toString(stuname)+"\t");
System.out.print(Bytes.toString(gender)+"\t");
System.out.print(Bytes.toString(pwd)+"\t");
System.out.print(Bytes.toString(schoolname)+"\t");
System.out.println(Bytes.toString(location)+"\t");
}
}
效果图:
@Test
public void getValue() throws IOException {
table = connection.getTable(TableName.valueOf("bigdata888:test001"));
Get zhangsan = new Get(Bytes.toBytes("rowkey2"));
Result result = table.get(zhangsan);
byte[] name = result.getValue(Bytes.toBytes("baseinfo"), Bytes.toBytes("name"));
System.out.println(Bytes.toString(name));
}
效果图:
@Test
public void del() throws IOException {
table = connection.getTable(TableName.valueOf("bigdata888:test001"));
// Delete wangwu = new Delete(Bytes.toBytes("rowkey3"));
// lisi.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("name")); // 删除具体列
// table.delete(wangwu);
Delete delwangwu = new Delete(Bytes.toBytes("rowkey3")); // 删除整行
table.delete(delwangwu);
}