hiveserver2服務(wù)
前面都是使用Hive的命令行客戶端,此處記錄一下hiveserver2服務(wù)啟動(dòng)使用的方法控嗜。
Hadoop的配置文件
首先要修改Hadoop的配置文件:
# vim /usr/hadoop-2.7.7/etc/hadoop/core-site.xml
添加如下配置內(nèi)容:
<property>
<name>hadoop.proxyuser.root.hosts</name>
<value>*</value>
</property>
<property>
<name>hadoop.proxyuser.root.groups</name>
<value>*</value>
</property>
并將該文件拷貝到Hadoop集群的所有節(jié)點(diǎn)的對(duì)應(yīng)目錄下:
Hive的配置文件
由于配置Hive時(shí)茧彤,配置文件hive-site.xml是復(fù)制的hive-default.xml.template屎勘,所以其中的內(nèi)容有默認(rèn)配置示启,主要是以下兩項(xiàng):
<property>
<name>hive.server2.thrift.port</name>
<value>10000</value>
<description>Port number of HiveServer2 Thrift interface when hive.server2.transport.mode is 'binary'.</description>
</property>
<property>
<name>hive.server2.thrift.bind.host</name>
<value/>
<description>Bind host on which to run the HiveServer2 Thrift service.</description>
</property>
hive.server2.thrift.bind.host的value值湃密,可以配置本機(jī)IP地址巫员,也可以默認(rèn)庶香。
hiveserver2啟動(dòng)
在終端中數(shù)據(jù)如下命令,即可啟動(dòng)hiveserver2服務(wù):
# hiveserver2
此處使用beeline進(jìn)行連接服務(wù)简识,另開(kāi)一個(gè)終端上執(zhí)行如下命令:
# beeline
然后輸入如下信息進(jìn)行連接:
beeline> !connect jdbc:hive2://192.168.44.128:10000
用戶名:root赶掖,密碼:默認(rèn)無(wú),直接回車即可七扰。
連接成功后奢赂,即可進(jìn)行數(shù)據(jù)庫(kù)操作:
0: jdbc:hive2://192.168.44.128:10000>show databases;
DML操作
DML(Data Manipulation Language:數(shù)據(jù)操縱語(yǔ)言)主要是實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的基本操作。
數(shù)據(jù)導(dǎo)入
裝載數(shù)據(jù)(Load)
Load在前文的《Hive安裝颈走、配置和測(cè)試》中已經(jīng)使用過(guò)膳灶,前文連接:http://www.reibang.com/p/5c5d57dc2084。
語(yǔ)法:
hive> load data [local] inpath '/home/test.txt' into | overwrite table testhive.testtable [partition(partcol1=val1...)];
- load data:加載數(shù)據(jù)立由。
- local:表示從本地目錄加載數(shù)據(jù)到hive表轧钓;不加local表示從HDFS加載數(shù)據(jù)到hive表。
- inpath:加載數(shù)據(jù)的目錄锐膜。
- overwrite:覆蓋表中已有數(shù)據(jù)毕箍,不加overwrite表示追加數(shù)據(jù)到表中。
- into table:表示數(shù)據(jù)加載到哪張表道盏。
- testhive.testtable:表示數(shù)據(jù)加載到testhive庫(kù)中的testtable表而柑。
- partition:表示加載數(shù)據(jù)到指定的分區(qū)中。
實(shí)際操作
此處還是使用Hive客戶端進(jìn)行操作捞奕。
- 創(chuàng)建表:
hive (default)> create table test(id int,name string,age int) row format delimited fields terminated by ' ' lines terminated by '\n';
- 創(chuàng)建數(shù)據(jù)文件:
另打開(kāi)一個(gè)終端牺堰,在home目錄下創(chuàng)建test.txt文件:
# vim /home/test.txt
寫入如下內(nèi)容并保存:
1 Dcl_Snow 18
2 Dcl 19
3 Snow 20
- 從本地文件加載數(shù)據(jù):
hive (default)> load data local inpath '/home/test.txt' into table default.test;
- 從HDFS中加載數(shù)據(jù):
上傳本地文件到HDFS:
hive (default)> dfs -put /home/test.txt /tmp;
加載HDFS中的數(shù)據(jù):
hive (default)> load data inpath '/tmp/test.txt' into table default.test;
- 覆蓋數(shù)據(jù)加載:
在HDFS中創(chuàng)建/test目錄:
# hdfs dfs -mkdir /test
# hdfs dfs -ls /
上傳本地文件到HDFS,上傳前先在test.txt文件中增加一行內(nèi)容:
4 DclSnow 21
hive (default)> dfs -put /home/test.txt /test;
加載HDFS中的數(shù)據(jù)覆蓋表中原數(shù)據(jù):
hive (default)> load data inpath '/test/test' overwrite into table default.test;
- 創(chuàng)建新表存儲(chǔ)查詢結(jié)果:
hive (default)> create table if not exists test1 as select id, name, age from test;
可以看到颅围,是執(zhí)行了MapReduce操作。
- export導(dǎo)出:
hive (default)> export table default.test to '/test/test';
- import導(dǎo)入:
hive (default)> import table test2 from '/test/test';
- insert導(dǎo)出:
將查詢結(jié)果導(dǎo)出到本地:
hive (default)> insert overwrite local directory '/home/test' select * from test;
- Hadoop命令導(dǎo)出到本地:
hive (default)> dfs -get /test/test /home/testha;
- Hive Shell命令導(dǎo)出到本地:
在安裝Hive的虛擬機(jī)終端下執(zhí)行:
# hive -e 'select * from default.test;' >/home/testhi
- 清除表中數(shù)據(jù):
hive (default)> truncate table test;
只能清除內(nèi)部表(管理表)中的數(shù)據(jù)恨搓,不能清除外部表中的數(shù)據(jù)院促。