一、导入maven依赖
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>2.2.6</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.2.6</version>
</dependency>
二、编写Java代码
package com.qujiuge;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
public class HbaseDemo {
private HBaseAdmin admin;
private Connection conn;
@Before
public void init() throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.159.9");
conf.set("hbase.zookeeper.property.clientPort", "2181");
conn = ConnectionFactory.createConnection(conf);
admin = (HBaseAdmin) conn.getAdmin();
}
@After
public void destroy() throws IOException {
admin.close();
conn.close();
}
@Test
public void createTable() throws IOException {
TableName tableName = TableName.valueOf("person");
TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName);
ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder("info".getBytes()).build();
builder.setColumnFamily(family);
TableDescriptor table = builder.build();
admin.createTable(table);
}
@Test
public void deleteTable() throws IOException {
TableName tableName = TableName.valueOf("person");
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
@Test
public void list() throws IOException {
TableName[] tableNames = admin.listTableNames();
for (TableName tableName : tableNames) {
System.out.println(tableName);
}
}
@Test
public void isTableExists() throws IOException {
TableName tableName = TableName.valueOf("student");
System.out.println(admin.tableExists(tableName));
}
@Test
public void insertMany() throws IOException {
TableName tableName = TableName.valueOf("student");
Table table = conn.getTable(tableName);
ArrayList<Put> puts = new ArrayList<>();
Put put = new Put(Bytes.toBytes("1003"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("Alice"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("sex"), Bytes.toBytes("female"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("18"));
puts.add(put);
table.put(puts);
admin.flush(tableName);
}
@Test
public void deleteRecords() throws IOException {
TableName tableName = TableName.valueOf("student");
Table table = conn.getTable(TableName.valueOf("student"));
Delete delete = new Delete(Bytes.toBytes("1003"));
table.delete(delete);
admin.flush(tableName);
}
@Test
public void updateRecord() throws IOException {
TableName tableName = TableName.valueOf("student");
Table table = conn.getTable(TableName.valueOf("student"));
Put put = new Put(Bytes.toBytes("1002"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("Smith"));
table.put(put);
admin.flush(tableName);
}
@Test
public void findAll() throws IOException {
Scan scan = new Scan();
Table table = conn.getTable(TableName.valueOf("student"));
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.print("行键:" + Bytes.toString(CellUtil.cloneRow(cell)) + "\t");
System.out.print("列族:" + Bytes.toString(CellUtil.cloneFamily(cell)) + "\t");
System.out.print("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)) + "\t");
System.out.print("值:" + Bytes.toString(CellUtil.cloneValue(cell)) + "\n");
}
}
}
@Test
public void findOne() throws IOException {
Table table = conn.getTable(TableName.valueOf("student"));
Get get = new Get(Bytes.toBytes("1001"));
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
System.out.print("行键:" + Bytes.toString(CellUtil.cloneRow(cell)) + "\t");
System.out.print("列族:" + Bytes.toString(CellUtil.cloneFamily(cell)) + "\t");
System.out.print("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)) + "\t");
System.out.print("值:" + Bytes.toString(CellUtil.cloneValue(cell)) + "\t");
System.out.println("时间戳:" + cell.getTimestamp());
}
}
@Test
public void findOneQualifier() throws IOException {
Table table = conn.getTable(TableName.valueOf("student"));
Get get = new Get(Bytes.toBytes("1001"));
get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"));
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
System.out.print("行键:" + Bytes.toString(CellUtil.cloneRow(cell)) + "\t");
System.out.print("列族:" + Bytes.toString(CellUtil.cloneFamily(cell)) + "\t");
System.out.print("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)) + "\t");
System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
}