将本地檔案導入 Hive 案例
需求
将本地/opt/module/data/student.txt 這個目錄下的資料導入到 hive 的
student(id int, name string)表中。
1 資料準備
在/opt/module/data 這個目錄下準備資料
(1)在/opt/module/目錄下建立 data
mkdir -p /opt/module/data
(2)在/opt/module/data/目錄下建立 student.txt 檔案并添加資料
[dev1@hadoop102 data]$ touch student.txt
[dev1@hadoop102 data]$ vi student.txt
内容如下
1001 zhangshan
1002 lishi
1003 zhaoliu
注意以 tab 鍵間隔。
2 Hive 實際操作
(1)啟動hive
[dev1@hadoop102 hive]$ bin/hive
(2)顯示資料庫
hive>show databases;
(3)使用default 資料庫
hive>use default;
(4)顯示default 資料庫中的表
hive>show tables;
(5)删除已建立的 student 表
hive> drop table student;
(6)建立 student 表, 并聲明檔案分隔符’\t’
hive> create table student(id int, name string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';
terminated
英[ˈtɜːmɪneɪtɪd]
美[ˈtɜːrmɪneɪtɪd]
v. (使)停止,結束,終止; 到達終點站;
(7)加載/opt/module/data/student.txt 檔案到 student 資料庫表中。
hive> load data local inpath '/opt/module/data/student.txt' into table student;
(8)Hive 查詢結果
hive> select * from student;
3.遇到的問題
Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient