流程
- 先建立conn
- 连接table
- 创建get或put
- 执行相关操作
建立连接
import org.apache.hadoop.hbase.client.*;
Connection conn = null;
Configuration conf = HBaseConfiguration.create();
conf.setStrings("hbase.zookeeper.quorum", "192.168.0.1,192.168.0.2");
conf.setStrings("hbase.zookeeper.property.clientPort", "2181");
conf.setStrings("hbase.client.retries.number", "3");
conf.setStrings("hbase.rpc.timeout", "10000");
conf.setStrings("hbase.client.operation.timeout", "30000");
conn = ConnectionFactory.createConnection(conf);
获取table
Table table = null;
table = connection.getTable(TableName.valueOf("table"));
获取rowkey
String rowkey = null;
写入数据
Put put = new Put(Bytes.toBytes(rowkey));
// option
put.addColumn(Bytes.toBytes("family"), Bytes.toBytes("column"), Bytes.toBytes("value"));
table.put(put);
读取数据
Get get = new Get(Bytes.toBytes(rowkey));
Result result = table.get(get);
for (Map.Entry<byte[], byte[]> entry: result.getFamilyMap(Bytes.toBytes("family")).entrySet()) {
System.out.println(Bytes.toBytes(entry.getKey()));
}
删除数据
Delete delete = new Delete(Bytes.toBytes(rowkey));
// option
delete.addColumn(Bytes.toBytes("family"), Bytes.toBytes("column"));
table.delete(delete);
Reference:
http://www.corejavaguru.com/bigdata/hbase-tutorial/hbase-java-client-api-examples