新建項目後在 pom.xml 中添加依賴:
<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>
代碼如下
package test;
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 java.io.IOException;
/**
* DDL:
* 1.判斷表是否存在
* 2.創建name_space
* 3.表的基本操作(增刪)
* 4.刪除表
*
* DML:
* 5.插入數據(插入和修改)
* 6.查詢數據(get)
* 7.scan查詢數據
* 8.刪除數據
*/
public class TestAPI {
private static Connection connection = null;
private static Admin admin = null;
static {
try {
//1.獲取配置文件信息
// HBaseConfiguration configuration = new HBaseConfiguration();
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "hadoop102:2181,hadoop103:2181,hadoop104:2181");
//2.獲取管理員對象
// HBaseAdmin admin = new HBaseAdmin(configuration);
connection = ConnectionFactory.createConnection(configuration);
//創建Admin對象
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
//1.判斷表是否存在
public static boolean isTableExist(String tableName) throws IOException {
//注意這裏的Table創建方式
boolean exists = admin.tableExists(TableName.valueOf(tableName));
// //關閉連接
// admin.close();
return exists;
}
//2.創建表
public static void createTable(String tableName,String... cfs) throws IOException {
//1.判斷是否存在列族信息
if(cfs.length <= 0){
System.out.println("請設置列族信息!");
return;
}
//2.判斷表是否存在
if(isTableExist(tableName)){
System.out.println(tableName+"表已存在!");
return;
}
//3.創建表的描述器
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
//4.循環添加列族信息
for (String cf : cfs) {
//5.創建列族描述器
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
//6.添加具體表是否存在
descriptor.addFamily(hColumnDescriptor);
}
//7.創建表
admin.createTable(descriptor);
}
//3.刪除表
public static void dropTable(String tableName) throws IOException {
//1.判斷表是否存在
if(!isTableExist(tableName)){
System.out.println(tableName+"表不存在!");
return;
}
//2.使表下線
admin.disableTable(TableName.valueOf(tableName));
//3.刪除表
admin.deleteTable(TableName.valueOf(tableName));
}
//4.創建命名空間
public static void createNameSpace(String ns){
//1.創建命名空間描述器
NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(ns).build();
//2.創建命名空間
try {
admin.createNamespace(namespaceDescriptor);
}catch (NamespaceExistException e){
System.out.println(ns+"命名空間已存在!");
}
catch (IOException e) {
e.printStackTrace();
}
}
//5.向表插入數據
public static void putData(String tableName,String rowKey,String cf,String cn,String value) throws IOException {
//1.獲取表對象
Table table = connection.getTable(TableName.valueOf(tableName));
//2.獲取Put對象
Put put = new Put(Bytes.toBytes(rowKey));
//3.給Put對象賦值
put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));
//4.插入數據
table.put(put);
//5.關閉表資源
table.close();
}
//6.獲取數據(get)
public static void getData(String tableName, String rowKey,String cf,String cn) throws IOException {
//1.獲取表對象
Table table = connection.getTable(TableName.valueOf(tableName));
//2.創建Get對象
Get get = new Get(Bytes.toBytes(rowKey));
//2.1指定獲取的列族和列名
get.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn));
get.setMaxVersions();
//3.獲取數據
Result result = table.get(get);
//4.解析result並打印
for (Cell cell : result.rawCells()) {
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("時間戳:" + cell.getTimestamp());
}
}
//7.獲取數據(scan)
public static void scanTable(String tableName) throws IOException {
//1.獲取表對象
Table table = connection.getTable(TableName.valueOf(tableName));
//2.構建scan對象
Scan scan = new Scan();
//3.掃描表
ResultScanner scanner = table.getScanner(scan);
//4.解析result
for (Result result : scanner) {
//5.解析result並打印
for (Cell cell : result.rawCells()) {
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("時間戳:" + cell.getTimestamp());
}
}
//6.關閉表連接
table.close();
}
//8.刪除數據
public static void deleteData(String tableName, String rowKey,String cf,String cn) throws IOException {
//1.獲取表連接
Table table = connection.getTable(TableName.valueOf(tableName));
//2.構建刪除對象
Delete delete = new Delete(Bytes.toBytes(rowKey));
// delete.addColumns(Bytes.toBytes(cf), Bytes.toBytes(cn));
delete.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
//3.執行刪除操作
table.delete(delete);
//4.關閉連接
table.close();
}
//關閉資源
public static void close(){
//關閉連接
if(admin != null){
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(connection != null){
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
//1.測試表是否存在
System.out.println(isTableExist("stu6"));
//2.測試創建表操作
createTable("stu6","info1","info2");
//3.刪除表測試
dropTable("stu6");
//4.創建命名空間測試
createNameSpace("nihao");
//5.插入數據測試
putData("stu1", "1001", "info1", "name", "jingjing");
System.out.println(isTableExist("stu6"));
//6.獲取數據(get)
getData("stu1","1001","info2","sex");
//7.獲取數據(scan)
scanTable("stu1");
//8.測試刪除
deleteData("stu1", "1001", "info1", "name");
//關閉資源
close();
}
}