大數(shù)據(jù)存儲: HBase API,DDL,DML
4.1 環(huán)境準(zhǔn)備
新建項目后在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
創(chuàng)建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.創(chuàng)建配置信息并配置
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.關(guān)閉連接
admin.close();
connection.close();
//6.返回結(jié)果
return exists;
}
}
4.2.2 創(chuàng)建表
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 創(chuàng)建表
public static void createTable(String tableName, String... cfs) throws IOException {
//1.判斷是否存在列族信息
if (cfs.length <= 0) {
System.out.println("請設(shè)置列族信息!");
return;
}
//2.判斷表是否存在
if (isTableExist(tableName)) {
System.out.println("需要創(chuàng)建的表已存在!");
return;
}
//3.創(chuàng)建配置信息并配置
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.創(chuàng)建表描述器構(gòu)造器
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
//7.循環(huán)添加列族信息
for (String cf : cfs) {
ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cf));
tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());
}
//8.執(zhí)行創(chuàng)建表的操作
admin.createTable(tableDescriptorBuilder.build());
//9.關(guān)閉資源
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.創(chuàng)建配置信息并配置
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.執(zhí)行刪除表操作
admin.deleteTable(name);
//7.關(guān)閉資源
admin.close();
connection.close();
}
}
4.2.4 創(chuàng)建命名空間
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 創(chuàng)建命名空間
public static void createNameSpace(String ns) throws IOException {
//1.創(chuàng)建配置信息并配置
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.創(chuàng)建命名空間描述器
NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(ns).build();
//5.執(zhí)行創(chuàng)建命名空間操作
try {
admin.createNamespace(namespaceDescriptor);
} catch (NamespaceExistException e) {
System.out.println("命名空間已存在!");
} catch (Exception e) {
e.printStackTrace();
}
//6.關(guān)閉連接
admin.close();
connection.close();
}
}
4.3 DML
創(chuàng)建類HBase_DML
4.3.1 插入數(shù)據(jù)
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 插入數(shù)據(jù)
public static void putData(String tableName, String rowKey, String cf, String cn, String value) throws IOException {
//1.獲取配置信息并設(shè)置連接參數(shù)
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.創(chuàng)建Put對象
Put put = new Put(Bytes.toBytes(rowKey));
//5.放入數(shù)據(jù)
put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));
//6.執(zhí)行插入數(shù)據(jù)操作
table.put(put);
//7.關(guān)閉連接
table.close();
connection.close();
}
}
4.3.2 單條數(shù)據(jù)查詢
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 單條數(shù)據(jù)查詢(GET)
public static void getDate(String tableName, String rowKey, String cf, String cn) throws IOException {
//1.獲取配置信息并設(shè)置連接參數(shù)
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.創(chuàng)建Get對象
Get get = new Get(Bytes.toBytes(rowKey));
// 指定列族查詢
// get.addFamily(Bytes.toBytes(cf));
// 指定列族:列查詢
// get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
//5.查詢數(shù)據(jù)
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.關(guān)閉連接
table.close();
connection.close();
}
}
4.3.3 掃描數(shù)據(jù)
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 掃描數(shù)據(jù)(Scan)
public static void scanTable(String tableName) throws IOException {
//1.獲取配置信息并設(shè)置連接參數(shù)
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.創(chuàng)建Scan對象
Scan scan = new Scan();
//5.掃描數(shù)據(jù)
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.關(guān)閉資源
table.close();
connection.close();
}
}
4.3.4 刪除數(shù)據(jù)
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 刪除數(shù)據(jù)
public static void deletaData(String tableName, String rowKey, String cf, String cn) throws IOException {
//1.獲取配置信息并設(shè)置連接參數(shù)
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.創(chuàng)建Delete對象
Delete delete = new Delete(Bytes.toBytes(rowKey));
// 指定列族刪除數(shù)據(jù)
// delete.addFamily(Bytes.toBytes(cf));
// 指定列族:列刪除數(shù)據(jù)(所有版本)
// delete.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
// 指定列族:列刪除數(shù)據(jù)(指定版本)
// delete.addColumns(Bytes.toBytes(cf), Bytes.toBytes(cn));
//5.執(zhí)行刪除數(shù)據(jù)操作
table.delete(delete);
//6.關(guān)閉資源
table.close();
connection.close();
}
}
你的贊,我都當(dāng)成喜歡。
專注分享大數(shù)據(jù)技術(shù)&智能技術(shù)&基礎(chǔ)&實戰(zhàn),干貨,資料。
關(guān)注本號,讓更多人了解技術(shù),讓技術(shù)造福更多人。歡迎轉(zhuǎn)發(fā)傳播,感謝您的關(guān)注,謝謝。