Hive的基本操作和數據類型


Hive的基本操作

 1.啟動Hive
	  bin/hive
 2.查看數據庫
 	hive>show databases;
 3. 打開默認數據庫
 	hive>use default;
 4.顯示default數據庫中的所有表
    hive>show tables; 
 5.創建一張表
 	hive> create table student(id int, name string) ;
 6.顯示數據庫中的所有表
 	hive>show tables;
 7.查看表結構
 	hive>desc student;
 8.向表中插入數據
	hive> insert into student values(1000,"ss");
9.查詢表中數據
	hive>select * from student;
10.退出Hive
	hive>quit;

將本地文件數據導入到hive中

需求:將本地/opt/module/datas/student.txt這個目錄下的數據,
導入到hive的student(id int, name string)表中。

student.txt:文件內容使用\t進行分割
1001  zhangshan
1002  lishi
1003  zhaoliu

$ bin/hive
hive>show databases;
hive>use default;
hive>show tables;
hive> drop table student;

創建 student 表, 並聲明文件分隔符’\t’
hive> create table student(id int, name string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';

加載/opt/module/datas/student.txt 文件到 student 數據庫表中。
hive> load data local inpath '/opt/module/datas/student.txt' into table student;

Hive 查詢結果
hive> select * from student;

中間出現的問題

Caused  by:  java.lang.RuntimeException:  Unable  to  instantiate
org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient

原因是,Metastore 默認存儲在自帶的 derby 數據庫中,推薦使用 MySQL 存儲 Metastore;

Hive 元數據配置到 MySql

前提,確認MySqlServer安裝成功

1.拷貝Mysql驅動到hive/lib包下
mysql-connector-java-5.1.27-bin.jar

2.配置 Metastore 到 到 MySql
	配置hive-site.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
	<property>
		<name>javax.jdo.option.ConnectionURL</name>
		<value>jdbc:mysql://hadoop102:3306/metastore?createDatabaseIfNotExist=true</value>
		<description>JDBC connect string for a JDBC metastore</description>
	</property>
	<property>
		<name>javax.jdo.option.ConnectionDriverName</name>
		<value>com.mysql.jdbc.Driver</value>
		<description>Driver class name for a JDBC metastore</description>
	</property>
	<property>
		<name>javax.jdo.option.ConnectionUserName</name>
		<value>root</value>
		<description>username to use against metastore database</description>
	</property>
	<property>
		<name>javax.jdo.option.ConnectionPassword</name>
		<value>000000</value>
		<description>password to use against metastore database</description>
	</property>
	<property>
		<name>hive.cli.print.current.db</name>
		<value>true</value>
		<description>Whether  to  include  the  current  database  in  the  Hive
		prompt.</description>
	</property>
	<property>
		<name>hive.cli.print.header</name>
		<value>false</value>
		<description>Whether to print the names of the columns in query output.</description>
	</property>
</configuration>

配置完畢后,如果啟動 hive 異常,可以重新啟動虛擬機。(重啟后,別忘了啟動 hadoop集群)

Hive 常用的交互命令

“-e”不進入 hive 的交互窗口執行 sql 語句
	$ bin/hive -e "select id from student;"

“-f”執行腳本中 sql 語句
	$ bin/hive -f /opt/module/datas/hivef.sql

執行文件中的 sql 語句並將結果寫入文件中
$  bin/hive  -f  /opt/module/datas/hivef.sql  > /opt/module/datas/hive_result.txt

Hive 其他命令操作

1)退出 hive 窗口: 
	hive(default)>exit;
	hive(default)>quit; 
	exit:先隱性提交數據,再退出;
	quit:不提交數據,退出;
	
2)在 hive cli 命令窗口中如何查看 hdfs 文件系統
	hive(default)>dfs -ls /;
	
3)在 hive cli 命令窗口中如何查看 hdfs 本地系統
	hive(default)>! ls /opt/module/datas;
	
4)查看在 hive 中輸入的所有歷史命令
	(1)進入到當前用戶的根目錄/root 或/home/upuptop
	(2)查看. hivehistory 文件
	[upuptop@hadoop102 ~]$ cat .hivehistory

Hive 常見屬性配置

Hive  數據倉庫位置配置
	1)Default 數據倉庫的最原始位置是在 hdfs 上的:/user/hive/warehouse 路徑下
	2)在倉庫目錄下,沒有對默認的數據庫 default 創建文件夾。如果某張表屬於 default	數據庫,		
	直接在數據倉庫目錄下創建一個文件夾。
	3)修改 default 數據倉庫原始位置(將 hive-default.xml.template 如下配置信息拷貝到	hive-site.xml 文件中)
		<property>
			<name>hive.metastore.warehouse.dir</name>
			<value>/user/hive/warehouse</value>
			<description>location of default database for the warehouse</description>
		</property>
	配置同組用戶有執行權限
	bin/hdfs dfs -chmod g+w /user/hive/warehouse

查詢后信息顯示配置
	在 hive-site.xml 文件中添加如下配置信息,就可以實現顯示當前數據庫,以及查詢	表的頭信息配置
	<property>
		<name>hive.cli.print.header</name>
		<value>true</value>
		</property>
		<property>
		<name>hive.cli.print.current.db</name>
		<value>true</value>
	</property>

Hive 運行日志信息配置
	Hive 的 log 默認存放在/tmp/uupuptop/hive.log 目錄下(當前用戶名下)
  修改 hive 的 log 存放日志到/opt/module/hive/logs
	(1)修改/opt/module/hive/conf/hive-log4j.properties.template 文件名稱為hive-log4j.properties
	$ pwd
	opt/module/hive/conf
	$ mv hive-log4j.properties.template hive-log4j.properties
	(2)在 hive-log4j.properties 文件中修改 log 存放位置
	$ hive.log.dir=/opt/module/hive/logs

參數配置方式
	1)查看當前所有的配置信息
		hive>set;
	2)參數的配置三種方式
		(1)配置文件方式
				默認配置文件:hive-default.xml
				用戶自定義配置文件:hive-site.xml
				注意:用戶自定義配置會覆蓋默認配置。另外,Hive 也會讀入 Hadoop 的配置,
				因為 Hive 是作為 Hadoop 的客戶端啟動的,Hive 的配置會覆蓋 Hadoop 的配置。
				配置文件的設定對本機啟動的所有 Hive 進程都有效。
		(2)命令行參數方式
			啟動 Hive 時,可以在命令行添加-hiveconf param=value 來設定參數。
			例如:
			[upuptop@hadoop103 hive]$ bin/hive -hiveconf mapred.reduce.tasks=10;
			注意:僅對本次 hive 啟動有效
			查看參數設置:
			hive (default)> set mapred.reduce.tasks;
		
		(3)參數聲明方式
			可以在 HQL 中使用 SET 關鍵字設定參數
			例如:
				hive (default)> set mapred.reduce.tasks=100;
				注意:僅對本次 hive 啟動有效。
		查看參數設置: hive (default)> set mapred.reduce.tasks
	上述三種設定方式的優先級依次遞增。即配置文件<命令行參數<參數聲明。注意某些系統級的參數,
	例如 log4j 相關的設定,必須用前兩種方式設定,因為那些參數的讀取在會
	話建立以前已經完成了。
	

Hive 數據類型

基本數據類型

Hive 數據類型 Java 數據類型 長度 例子
TINYINT byte 1byte有符號整數 20
SMALINT short 2byte有符號整數 20
INT int 4byte 有符號整數 20
BIGINT long 8byte 有符號整數 20
BOOLEAN boolean 布爾類型,true 或者false TRUE FALSE
FLOAT float 單精度浮點數 3.14159
DOUBLE double 雙精度浮點數 3.14159
STRING string 字符系列。可以指定字符集。可以使用單引號或者雙引號。 ‘now is the time’ “forall good men”
TIMESTAMP 時間類型
BINARY 字節數組

對於 Hive 的 String 類型相當於數據庫的 varchar 類型,該類型是一個可變的字符串,不過它不能聲明其中最多能存儲多少個字符,理論上它可以存儲 2GB 的字符數。

集合數據類型

數據類型 描述 語法示例
STRUCT 和 c 語言中的 struct 類似,都可以通過“點”符號訪問元素內容。例如,如果某個列的數據類型是 STRUCT{first STRING, lastSTRING},那么第 1 個元素可以通過字段.first 來引用。 struct()
MAP MAP 是一組鍵-值對元組集合,使用數組表示法可以訪問數據。例如,如果某個列的數據類型是 MAP,其中鍵->值對是’first’->’John’和’last’->’Doe’,那么可以通過字段名[‘last’]獲取最后一個元素 map()
ARRAY 數組是一組具有相同類型和名稱的變量的集合。這些變量稱為數組的元素,每個數組元素都有一個編號,編號從零開始。例如,數組值為[‘John’, ‘Doe’],那么第 2 個元素可以通過數組名[1]進行引用。 Array()

Hive 有三種復雜數據類型 ARRAY、MAP 和 STRUCT。ARRAY 和 MAP 與 Java 中的 Array 和 Map 類似,而 STRUCT 與 C 語言中的 Struct 類似,它封裝了一個命名字段集合,復雜數據類型允許任意層次的嵌套。

案例實操

1)假設某表有如下一行,我們用 JSON 格式來表示其數據結構。在 Hive 下訪問的格式為

{
	"name": "songsong",
	"friends": ["bingbing", "lili"], //列表 Array,
	"children": { //鍵值 Map,
		"xiao song": 18,
		"xiaoxiao song": 19
	}
	"address": { //結構 Struct,
		"street": "hui long guan",
		"city": "beijing"
	}
}

2)基於上述數據結構,我們在 Hive 里創建對應的表,並導入數據。
創建本地測試文件 test.txt

songsong,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long guan_beijing
yangyang,caicai_susu,xiao yang:18_xiaoxiao yang:19,chao yang_beijing

注意,MAP,STRUCT 和 ARRAY 里的元素間關系都可以用同一個字符表示,這里用“_”。

3)Hive 上創建測試表 test

create table test(
	name string,
	friends array<string>,
	children map<string, int>,
	address struct<street:string, city:string>
) row format delimited fields terminated by ','
collection items terminated by '_'
map keys terminated by ':'
lines terminated by '\n';
字段解釋:
row format delimited fields terminated by ',' -- 列分隔符
collection items terminated by '_' --MAP STRUCT 和 ARRAY 的分隔符(數據分割符號)
map keys terminated by ':'  -- MAP 中的 key 與 value 的分隔符
lines terminated by '\n'; -- 行分隔符

4)導入文本數據到測試表

hive (default)> load data local inpath '/opt/module/datas/test.txt' into table test;

5)訪問三種集合列里的數據,以下分別是 ARRAY,MAP,STRUCT 的訪問方式

hive (default)> select friends[1],children['xiao song'],address.city from test where
name="songsong";
OK
_c0 _c1 city
lili 18 beijing
Time taken: 0.076 seconds, Fetched: 1 row(s)

本博客僅為博主學習總結,感謝各大網絡平台的資料。蟹蟹!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM