HBase通過JavaAPI和HIVE集成
1、Maven導入依賴包: hbase-clinet 版本與server上的hbase相同
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-hbase-handler</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
2、編寫demo
//Configuration同Hadoop
//Connection打開hbase鏈接
//Admin類管理命名空間和表等元數據
//connection管理數據
//H【Table】|【Column】Descriptor table描述類/列族描述類/列描述類/
public class HbaseJavaApi {
static Connection connect;
public static void main(String args[]) {
try {
createTable();
} catch (IOException e) {
e.printStackTrace();
}
try {
listTable();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void createTable() throws IOException {
//賬戶類
Admin admin=getAdmin();
//表描述類
HTableDescriptor tableDescriptor=new HTableDescriptor(TableName.valueOf("demo6"));
//列描述類
HColumnDescriptor columnDescriptor=new HColumnDescriptor("info1");
//掛載列到表上
tableDescriptor.addFamily(columnDescriptor);
//客戶端執行添加表
admin.createTable(tableDescriptor);
}
public static void listTable() throws IOException {
Admin admin=getAdmin();
//客戶端執行list,獲取表名數組
TableName[] tableNames = admin.listTableNames();
for (TableName name : tableNames) {
System.out.println(name);
}
connect.close();
}
public static void insertColumn() throws IOException {
//獲取表
Table table = connect.getTable(TableName.valueOf("zookeeper"));
//構建PUT,屬性為行鍵
Put put=new Put("xixi".getBytes());
//添加列值信息,列族,列鍵,cell值
put.addColumn("cf01".getBytes(),"name".getBytes(),"jason".getBytes());
//客戶端執行put
table.put(put);
//構建get,屬性為行鍵
Get get=new Get("rk01".getBytes());
//添加要查詢的,列族,列鍵
get.addColumn("cf01".getBytes(),"name".getBytes());
//獲取get結果
Result result = table.get(get);
//從結果中取得特定信息,指定列族列鍵的cell
result.getValue("cf01".getBytes(),"name".getBytes());
//構建scan
Scan scan=new Scan();
ResultScanner resultScanner = table.getScanner(scan);
for (Result result1 : resultScanner) {
//從結果中取得特定信息,指定列族列鍵的cell
result1.getValue("cf01".getBytes(),"name".getBytes());
}
}
public static Admin getAdmin() throws IOException {
if (connect==null) {
//開啟鏈接,指定zookeeper鏈接信息
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum.", "localhost");
conf.set("hbase.zookeeper.property.clientPort", "2181");
connect = ConnectionFactory.createConnection(conf);
}
//打開客戶端
Admin admin=connect.getAdmin();
return admin;
}
}
3、發布工程,包括依賴包
4、shell 運行jar
上傳jar包到服務器,命令執行
java -cp HBase-JAVAAPI.jar com.kgc.study.HBaseJavaApi
HBase by Phoenix
安裝phoenix
每個regionserver上都安裝上phoenix
雙引號大小寫,單引號
HBase by Hive
建表語句
CREATE external TABLE tbl_name_hive_new(
rowkey string,
val_cf1_col1 string,
val_cf1_col2 string,
val_cf2_col1 string,
val_cf2_col2 string
)
stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
with serderproperties("hbase.columns.mappring"=":key,cf1:col1,cf1:col2,cf2:col1,cf2:col2")
tblproperties("hbase.table.name"="tbl_name_hbase_exists");
--hbase.columns.mapping列鍵的順序與表中字段順序一致
--無論外部內部表,數據都存在hbase維護的庫中
--外部表建表要求hbase表已存在
--內部表建表要求hbase表不存在(此時內部表也是誤刪安全的)
--數據會在雙方實時映射(因為操作的都是hbase上的同一個文件)
注:hbase插入的數據會先在內存緩沖區,因此可能會出現hbase更新數據后未實時更新到hive。如果發生,在hbase端使用compact tbl_name命令
- hive讀取的是hbase文件
- hive導入已有的hbase可選用外部表,也可選用內部表
- hive創建新的hbase表用內部表
- hive寫入可以實時映射到hbase
- hbase可以寫入實時映射到hive