啟動(dòng)docker容器
docker run -d -h myhbase -p 2181:2181 -p 8080:8080 -p 8085:8085 -p 9090:9090 -p 9095:9095 -p 16000:16000 -p 16010:16010 -p 16020:16020 -p 16030:16030 --name hbase1.3 harisekhon/hbase:1.3
配置hosts文件
xx.xx.xx.xx myhabse
1. 引入hbase依賴
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.1</version>
</dependency>
如果測試HADOOP_HOME的錯(cuò)誤請下載hadoop-common,并配置hadoop.home.dir
https://blog.csdn.net/liu16659/article/details/84069297
java.io.IOException: HADOOP_HOME or hadoop.home.dir are not set.
2.編寫測試類
public class HbaseClientTest {
public static Configuration conf;
@Before
public void beforeInit() {
System.setProperty("hadoop.home.dir","D:\\program files\\hadoop-common-2.2.0-bin-master");
//使用HbaseConfiguration的單例方法實(shí)例化
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","XXX");
conf.set("hbase.zookeeper.property.clientPort","2181");
}
/**
* 表是否存在
*/
@Test
public void isTableExist() throws IOException {
String tableName = "student";
//在Hbase中管理、訪問表需要先創(chuàng)建HBaseAdmin對象
Connection connection = ConnectionFactory.createConnection(conf);
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
System.out.println(admin.tableExists(tableName));
}
/**
* 創(chuàng)建表
* @throws IOException
*/
@Test
public void createTable() throws IOException {
Connection connection = ConnectionFactory.createConnection(conf);
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
String tableName = "student";
//創(chuàng)建表屬性對象,表名需要轉(zhuǎn)字節(jié)
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
List<String> columnFamily = new ArrayList<>();
columnFamily.add("info");
//創(chuàng)建多個(gè)列族
for(String cf : columnFamily) {
descriptor.addFamily(new HColumnDescriptor(cf));
}
//根據(jù)對表的配置,創(chuàng)建表
admin.createTable(descriptor);
System.out.println("表" + tableName + "創(chuàng)建成功!");
}
/**
* 刪除表
*/
@Test
public void dropTable() throws IOException {
String tableName = "student";
Connection connection = ConnectionFactory.createConnection(conf);
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
/**
* 向表中插入數(shù)據(jù)
*/
@Test
public void addRowData() throws IOException {
String tableName = "student";
//創(chuàng)建HTable對象
HTable hTable = new HTable(conf,tableName);
//向表中插入數(shù)據(jù)
Put put = new Put(Bytes.toBytes("1001"));
//向Put對象中組裝數(shù)據(jù)
put.add(Bytes.toBytes("info"),Bytes.toBytes("sex"),Bytes.toBytes("male"));
hTable.put(put);
hTable.close();
System.out.println("插入數(shù)據(jù)成功");
}
/**
* 刪除多行數(shù)據(jù)
*/
@Test
public void deleteMultiRow() throws IOException {
String tableName = "student";
HTable hTable = new HTable(conf,tableName);
List<String> rows = new ArrayList<>();
rows.add("1001");
List<Delete> deleteList = new ArrayList<>();
for(String row : rows) {
Delete delete = new Delete(Bytes.toBytes(row));
deleteList.add(delete);
}
hTable.delete(deleteList);
hTable.close();
}
/**
* 獲取所有數(shù)據(jù)
*/
@Test
public void getAllRows() throws IOException {
String tableName = "student";
HTable hTable = new HTable(conf,tableName);
//得到用于掃描的region對象
Scan scan = new Scan();
//使用HTable得到resultscanner實(shí)現(xiàn)類的對象
ResultScanner resultScanner = hTable.getScanner(scan);
for(Result result :resultScanner) {
Cell[] cells = result.rawCells();
for(Cell cell : cells) {
//得到rowkey
System.out.println("行鍵: " + Bytes.toString(CellUtil.cloneRow(cell)));
//得到列族
System.out.println("列族: " + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("列: "+ Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("值: " + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
}
/**
* 獲取某一行數(shù)據(jù)
*/
@Test
public void getRow() throws IOException {
String tableName = "student";
HTable hTable = new HTable(conf,tableName);
Get get = new Get(Bytes.toBytes("1001"));
//get.setMaxVersions(); // 顯示所有版本
//get.setTimeStamp();// 顯示指定時(shí)間戳的版本
Result result = hTable.get(get);
for(Cell cell : result.rawCells()) {
System.out.println("行鍵: " + Bytes.toString(result.getRow()));
System.out.println("列族: " + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("列: "+ Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("值: " + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("時(shí)間戳: " + cell.getTimestamp());
}
}
/**
* 獲取某一行指定"列族:列"的數(shù)據(jù)
*/
@Test
public void getRowQualifier() throws IOException {
String tableName = "student";
HTable hTable = new HTable(conf,tableName);
Get get = new Get(Bytes.toBytes("1001"));
get.addColumn(Bytes.toBytes("info"),Bytes.toBytes("sex"));
Result result = hTable.get(get);
for(Cell cell : result.rawCells()) {
System.out.println("行鍵: " + Bytes.toString(result.getRow()));
System.out.println("列族: " + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("列: "+ Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("值: " + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
}