易筋經Hive——Hive建立資料庫、資料表及插入資料
轉載請註明出處:http://blog.csdn.net/dongdong9223/article/details/86438951
本文出自【我是幹勾魚的部落格】
Ingredients:
-
Java:Java SE Development Kit 8u162(Oracle Java Archive),Linux下安裝JDK修改環境變數
-
Hadoop:hadoop-2.9.1.tar.gz(Apache Hadoop Releases Downloads, All previous releases of Hadoop are available from the Apache release archive site
-
Hive:hive-2.3.4(mirrors.tuna.tsinghua.edu.cn,Mirror site for Hive)
之前在易筋經Hive——Hive安裝及簡單使用中講解了Hive的環境搭建及簡單操作方法,今天來講解一下Hive中資料庫、資料表以及資料資訊的插入。
Hive具體資料的操作可以在官網User Documentation中找到。
1 啟動Hive伺服器
執行hiveserver2命令:
hiveserver2
2 建立資料庫、資料表
在UserInfo.hql檔案中儲存HQL指令碼命令,內容如下:
create database if not exists mydata; use mydata; drop table if exists UserInfo; create table UserInfo( id int, --id name string, --姓名 hobby array<string>, --個人愛好 address map<string,string> --地址資訊 ) row format delimited fields terminated by ',' collection items terminated by '-' map keys terminated by ':';
hive命令匯入資料:
# hive -f UserInfo.hql
which: no hbase in (/opt/hive/apache-hive-2.3.4-bin/bin:/opt/java/jdk1.8.0_162/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/hive/apache-hive-2.3.4-bin/lib/log4j-slf4j-impl-2.6.2.jar! /org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/hadoop/hadoop-2.9.1/share/hadoop/common/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
Logging initialized using configuration in jar:file:/opt/hive/apache-hive-2.3.4-bin/lib/hive-common-2.3.4.jar!/hive-log4j2.properties Async: true
OK
Time taken: 5.497 seconds
OK
Time taken: 0.026 seconds
OK
Time taken: 0.118 seconds
OK
Time taken: 0.669 seconds
進入Hive檢視:
3 上傳資料檔案
先要在Hadoop的hdfs-site.xml檔案中加入:
<property>
<name>dfs.permissions</name>
<value>false</value>
</property>
否則在插入資料的時候會出現許可權的問題。
在HDFS上建立一個資料夾:
/opt/hadoop/hadoop-2.9.1/bin/hadoop fs -mkdir -p /tmp/myfile/
可以使用Hue檔案瀏覽器,上傳本地csv檔案。假設路徑為/tmp/hive, 檔名為: UserInfo.csv。或者直接使用命令上傳檔案:
/opt/hadoop/hadoop-2.9.1/bin/hadoop fs -put /opt/git/WebCrawler/UserInfo.csv /tmp/myfile/
給資料夾授權:
/opt/hadoop/hadoop-2.9.1/bin/hadoop fs -chmod 755 /tmp/myfile
檢視:
# /opt/hadoop/hadoop-2.9.1/bin/hadoop fs -ls /tmp/myfile/
Found 1 items
-rw-r--r-- 1 root supergroup 146 2019-01-13 22:48 /tmp/myfile/UserInfo.csv
能夠看到csv檔案已經儲存到HDFS中了。
4 插入資料
在客戶端連線Hive伺服器,執行命令:
beeline -u jdbc:hive2://localhost:10000
如下:
# beeline -u jdbc:hive2://localhost:10000
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/hive/apache-hive-2.3.4-bin/lib/log4j-slf4j-impl-2.6.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/hadoop/hadoop-2.9.1/share/hadoop/common/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
Connecting to jdbc:hive2://localhost:10000
Connected to: Apache Hive (version 2.3.4)
Driver: Hive JDBC (version 2.3.4)
Transaction isolation: TRANSACTION_REPEATABLE_READ
Beeline version 2.3.4 by Apache Hive
插入資料:
0: jdbc:hive2://localhost:10000> load data inpath '/user/root/myfile/UserInfo.csv' overwrite into table mydata.UserInfo;
No rows affected (2.411 seconds)
檢視資料:
0: jdbc:hive2://localhost:10000> select * from userinfo;
+--------------+----------------+-----------------------+---------------------------------------------+
| userinfo.id | userinfo.name | userinfo.hobby | userinfo.address |
+--------------+----------------+-----------------------+---------------------------------------------+
| 1 | xiaoming | ["book","TV","code"] | {"beijing":"chaoyang","shagnhai":"pudong"} |
| 2 | lilei | ["book","code"] | {"nanjing":"jiangning","taiwan":"taibei"} |
| 3 | lihua | ["music","book"] | {"heilongjiang":"haerbin"} |
+--------------+----------------+-----------------------+---------------------------------------------+
3 rows selected (1.411 seconds)
參考
Permission denied: user=administrator, access=WRITE, inode="/":root:supergroup:drwxr-xr-x