1.hbase與hive哪些版本兼容?
hive0.90與hbase0.92是兼容的,早期的hive版本與hbase0.89/0.90兼容,不需要自己編譯。
hive1.x與hbase0.98.x或則更低版本是兼容的,不需要自己編譯。
hive2.x與hbase1.x及比hbase1.x更高版本兼容,不需要自己編譯。
重點注意:hive 1.x 與 hbase 1.x整合時,需要自己編譯
2.連接和使用Hive:
1.第一種方式:
/usr/bin/hive 為連接打開Hive操作界面的快捷方式
因此只需要直接執行命令 hive 即能連接打開Hive操作界面
2.第二種方式:
默認啟動Hive時,就會同時啟動了hiveserver2進程,jps查看到有RunJar進程即表示hiveserver2進程已啟動
/usr/bin/beeline 為外部Linux連接訪問當前Linux下的hive的快捷方式,所以此時只需要直接執行命令 beeline -u jdbc:hive2://node1:10000 -n root
2.show databases;
默認只有一個 數據庫: default
3.create database rimengshe;
use rimengshe;
create table nagisa(name string,num1 int,num2 int) row format delimited fields terminated by ',';
show tables;
3.連接和使用hbase
1./usr/bin/hbase 為連接打開Hbase操作界面的快捷方式
因此只需要直接執行命令 hbase shell 即能連接打開Hive操作界面
2.list 查看所有表
創建用戶表:create 'user','base_info'
4.hive 整合 hbase:
1.(一般無需操作拷貝jar包這一步)
因為 /opt/cloudera/parcels/CDH-6.1.0-1.cdh6.1.0.p0.770702/jars 目錄下已經存在 hive-hbase-handler-2.1.1-cdh6.1.0.jar,因此下面的拷貝jar包操作並不需要進行
把 hive-hbase-handler.jar 從 hive 的lib目錄下 拷貝到 hbase 的lib目錄下
如果沒有 hive-hbase-handler.jar 可以拷貝 hive-hbase-handler-2.1.1-cdh6.0.0.jar
hive 的lib目錄:
CDH6.0.0:cd /opt/cloudera/parcels/CDH-6.0.0-1.cdh6.0.0.p0.537114/lib/hive/lib
CDH6.1.0:cd /opt/cloudera/parcels/CDH-6.1.0-1.cdh6.1.0.p0.770702/lib/hive/lib
hbase 的lib目錄:
CDH6.0.0:cd /opt/cloudera/parcels/CDH-6.0.0-1.cdh6.0.0.p0.537114/lib/hbase/lib
CDH6.1.0:cd /opt/cloudera/parcels/CDH-6.1.0-1.cdh6.1.0.p0.770702/lib/hbase/lib
執行拷貝命令:cp /opt/cloudera/parcels/CDH-6.0.0-1.cdh6.0.0.p0.537114/lib/hive/lib/hive-hbase-handler.jar /opt/cloudera/parcels/CDH-6.0.0-1.cdh6.0.0.p0.537114/lib/hbase/lib
2.修改 hive 的conf目錄下 hive-site.xml文件中<configuration> 中的內容
<property>
<name>hive.zookeeper.quorum</name>
<value>node1,node2,node3</value>
</property>
<property>
<name>hive.server2.enable.doAs</name>
<value>false</value>
</property>
修改操作如下:
1.可通過Hive -> 操作 -> 下載客戶端配置 的方式查看hive-site.xml文件內容
1.可得知 hive.zookeeper.quorum 配置的內容,默認配置即為 node1,node2,node3 即可。
2.可得知 hive.server2.enable.doAs 默認為 true,推薦修改為false,否則在使用官方推薦的hiveserver2/beeline的方式操作時,
在利用HQL語句創建HBase時可能會出現異常
2.可通過Hive -> 配置 -> 搜索欄中搜索 hive.server2.enable.doAs ,默認為勾選,取消勾選即可,即能修改配置為 false。
再當我們通過Hive -> 操作 -> 下載客戶端配置 的方式查看hive-site.xml文件內容,即可查看到hive.server2.enable.doAs已為false
3.重啟 hive、hbase
4.保證hiveserver2進程啟動中,使用命令 beeline -u jdbc:hive2://node1:10000 -n root 進行連接
5.HIVE執行創建表語句:hbase表 映射 hive表,寫入的數據存儲在 hbase表中,"hbase.mapred.output.outputtable"可指定數據寫入到hbase表中
1.create database rimengshe;
2.use rimengshe;
3.第一種映射方式:
1.創建hive表的同時也會創建出hbase表
# Hive中的表名test_tb;key字段映射hbase表中的rowkey;value字段映射cf1列簇下的val字段
CREATE TABLE ushio(key int, value string)
# 指定存儲處理器
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
# 聲明列簇,列名
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,cf1:val")
# hbase.table.name聲明HBase表名,為可選屬性默認與Hive的表名相同
# hbase.mapred.output.outputtable指定插入數據時寫入的表,如果以后需要往該表插入數據就需要指定該值
TBLPROPERTIES ("hbase.table.name" = "ushio", "hbase.mapred.output.outputtable" = "ushio");
2.hbase表中添加數據:put '表名','rowkey值','列簇名:列名','列值'
put 'ushio','98','cf1:val','val_98'
put 'ushio','99','cf1:val','val_99'
put 'ushio','100','cf1:val','val_100'
3.hive表中添加數據:(會運行yarn)INSERT INTO table_name (field1, field2,...fieldN ) VALUES (value1, value2,...valueN );
insert into ushio values(2,'ushio');
4.hbase表 查詢表中的所有數據:scan '表名'
scan 'ushio'
5.hive表 查詢表中的所有數據:
select * from ushio;
4.第二種映射方式:
Hive映射“HBase中已經存在的”表,即使用HQL創建hive外部表,映射HBase已存在的表,進行關聯操作。
5.JAVA 操作 Hive
1.依賴
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-metastore</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
2.實現代碼
import java.sql.*;
public class select
{
private static String Driver = "org.apache.hive.jdbc.HiveDriver";
//jdbc:hive2://(主)節點IP:10000/數據庫名
private static String URL = "jdbc:hive2://192.168.88.100:10000/default";
private static String name = "";
private static String password = "";
public static void main(String[] args) throws SQLException
{
try
{
Class.forName(Driver);
Connection connection = DriverManager.getConnection(URL, name, password);
Statement statement = connection.createStatement();
String sql = "select * from test_tb";
ResultSet res = statement.executeQuery(sql);
while (res.next())
{
System.out.println(res.getInt(1) + "\t==> " + res.getString(2));
}
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
}