大數據存儲: HBase API,DDL,DML
4.1 環境準備
新建項目后在pom.xml中添加依賴:
<dependency>
<groupId>org.Apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.0.5</version>
</dependency>
4.2 DDL
創建HBase_DDL類
4.2.1 判斷表是否存在
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.NamespaceExistException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import JAVA.io.IOException;
public class HBase_DDL {
//TODO 判斷表是否存在
public static boolean isTableExist(String tableName) throws IOException {
//1.創建配置信息并配置
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
//2.獲取與HBase的連接
Connection connection = ConnectionFactory.createConnection(configuration);
//3.獲取DDL操作對象
Admin admin = connection.getAdmin();
//4.判斷表是否存在操作
boolean exists = admin.tableExists(TableName.valueOf(tableName));
//5.關閉連接
admin.close();
connection.close();
//6.返回結果
return exists;
}
}
4.2.2 創建表
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.NamespaceExistException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class HBase_DDL {
//TODO 創建表
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("需要創建的表已存在!");
return;
}
//3.創建配置信息并配置
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
//4.獲取與HBase的連接
Connection connection = ConnectionFactory.createConnection(configuration);
//5.獲取DDL操作對象
Admin admin = connection.getAdmin();
//6.創建表描述器構造器
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
//7.循環添加列族信息
for (String cf : cfs) {
ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cf));
tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());
}
//8.執行創建表的操作
admin.createTable(tableDescriptorBuilder.build());
//9.關閉資源
admin.close();
connection.close();
}
}
4.2.3 刪除表
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.NamespaceExistException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class HBase_DDL {
//TODO 刪除表
public static void dropTable(String tableName) throws IOException {
//1.判斷表是否存在
if (!isTableExist(tableName)) {
System.out.println("需要刪除的表不存在!");
return;
}
//2.創建配置信息并配置
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
//3.獲取與HBase的連接
Connection connection = ConnectionFactory.createConnection(configuration);
//4.獲取DDL操作對象
Admin admin = connection.getAdmin();
//5.使表下線
TableName name = TableName.valueOf(tableName);
admin.disableTable(name);
//6.執行刪除表操作
admin.deleteTable(name);
//7.關閉資源
admin.close();
connection.close();
}
}
4.2.4 創建命名空間
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.NamespaceExistException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class HBase_DDL {
//TODO 創建命名空間
public static void createNameSpace(String ns) throws IOException {
//1.創建配置信息并配置
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
//2.獲取與HBase的連接
Connection connection = ConnectionFactory.createConnection(configuration);
//3.獲取DDL操作對象
Admin admin = connection.getAdmin();
//4.創建命名空間描述器
NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(ns).build();
//5.執行創建命名空間操作
try {
admin.createNamespace(namespaceDescriptor);
} catch (NamespaceExistException e) {
System.out.println("命名空間已存在!");
} catch (Exception e) {
e.printStackTrace();
}
//6.關閉連接
admin.close();
connection.close();
}
}
4.3 DML
創建類HBase_DML
4.3.1 插入數據
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class HBase_DML {
//TODO 插入數據
public static void putData(String tableName, String rowKey, String cf, String cn, String value) throws IOException {
//1.獲取配置信息并設置連接參數
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
//2.獲取連接
Connection connection = ConnectionFactory.createConnection(configuration);
//3.獲取表的連接
Table table = connection.getTable(TableName.valueOf(tableName));
//4.創建Put對象
Put put = new Put(Bytes.toBytes(rowKey));
//5.放入數據
put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));
//6.執行插入數據操作
table.put(put);
//7.關閉連接
table.close();
connection.close();
}
}
4.3.2 單條數據查詢
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class HBase_DML {
//TODO 單條數據查詢(GET)
public static void getDate(String tableName, String rowKey, String cf, String cn) throws IOException {
//1.獲取配置信息并設置連接參數
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
//2.獲取連接
Connection connection = ConnectionFactory.createConnection(configuration);
//3.獲取表的連接
Table table = connection.getTable(TableName.valueOf(tableName));
//4.創建Get對象
Get get = new Get(Bytes.toBytes(rowKey));
// 指定列族查詢
// get.addFamily(Bytes.toBytes(cf));
// 指定列族:列查詢
// get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
//5.查詢數據
Result result = table.get(get);
//6.解析result
for (Cell cell : result.rawCells()) {
System.out.println("CF:" + Bytes.toString(cell.getFamilyArray()) +
",CN:" + Bytes.toString(cell.getQualifierArray()) +
",Value:" + Bytes.toString(cell.getValueArray()));
}
//7.關閉連接
table.close();
connection.close();
}
}
4.3.3 掃描數據
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class HBase_DML {
//TODO 掃描數據(Scan)
public static void scanTable(String tableName) throws IOException {
//1.獲取配置信息并設置連接參數
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
//2.獲取連接
Connection connection = ConnectionFactory.createConnection(configuration);
//3.獲取表的連接
Table table = connection.getTable(TableName.valueOf(tableName));
//4.創建Scan對象
Scan scan = new Scan();
//5.掃描數據
ResultScanner results = table.getScanner(scan);
//6.解析results
for (Result result : results) {
for (Cell cell : result.rawCells()) {
System.out.println("CF:" + Bytes.toString(cell.getFamilyArray()) +
",CN:" + Bytes.toString(cell.getQualifierArray()) +
",Value:" + Bytes.toString(cell.getValueArray()));
}
}
//7.關閉資源
table.close();
connection.close();
}
}
4.3.4 刪除數據
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class HBase_DML {
//TODO 刪除數據
public static void deletaData(String tableName, String rowKey, String cf, String cn) throws IOException {
//1.獲取配置信息并設置連接參數
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
//2.獲取連接
Connection connection = ConnectionFactory.createConnection(configuration);
//3.獲取表的連接
Table table = connection.getTable(TableName.valueOf(tableName));
//4.創建Delete對象
Delete delete = new Delete(Bytes.toBytes(rowKey));
// 指定列族刪除數據
// delete.addFamily(Bytes.toBytes(cf));
// 指定列族:列刪除數據(所有版本)
// delete.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
// 指定列族:列刪除數據(指定版本)
// delete.addColumns(Bytes.toBytes(cf), Bytes.toBytes(cn));
//5.執行刪除數據操作
table.delete(delete);
//6.關閉資源
table.close();
connection.close();
}
}
你的贊,我都當成喜歡。
專注分享大數據技術&智能技術&基礎&實戰,干貨,資料。
關注本號,讓更多人了解技術,讓技術造福更多人。歡迎轉發傳播,感謝您的關注,謝謝。