hbase版本:2.3.5
1、创建maven工程,引入pom依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.3.5</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
<version>2.3.5</version>
</dependency>
</dependencies>
2、编写测试类,与hbase创建连接
2.1 初始化配置
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);
}
2.2 测试
@Test
public void testConnection(){
System.out.println(connection);
}
效果图:
2.3 关闭连接
@After
public void close() throws IOException {
if (connection!=null)
connection.close();
}
3、创建表空间
@Test
public void createNamespace() throws IOException {
Admin admin = connection.getAdmin();
NamespaceDescriptor.Builder builder = NamespaceDescriptor.create("bigdata888");
NamespaceDescriptor namespaceDescriptor = builder.build();
admin.createNamespace(namespaceDescriptor);
}
4.创建表
@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);
}
5、插入数据
5.1 插入单行数据
@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);
}
5.2 插入多行数据
@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<Put> list = new ArrayList<Put>();
list.add(rowkey2);
list.add(rowkey3);
table.put(list);
}
6、扫描表
@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");
}
}
效果图:
7、查询单行数据
@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));
}
效果图:
8、删除数据
@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);
}