ApacheCN | apache中文网linux
hive命令的3种调用方式 sql
官网地址:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Cli(可参考)shell
方式1:hive –f /root/shell/hive-script.sql(适合多语句)数据库
hive-script.sql相似于script同样,直接写查询命令就行apache
例如:bash
[root@cloud4 shell]# vi hive_script3.sqloop
select * from t1;性能
select count(*) from t1;ui
不进入交互模式,执行一个hive script spa
这里能够和静音模式-S联合使用,经过第三方程序调用,第三方程序经过hive的标准输出获取结果集。
$HIVE_HOME/bin/hive -S -f /home/my/hive-script.sql (不会显示mapreduct的操做过程)
那么问题来了:如何传递参数呢?
demo以下:
start_hql.sh 内容:
#!/bin/bash
# -S 打印输出mapreduce日志
hive \
-hivevar id=1 \
-hivevar col2=2 \
-S -f test.sql
test.sql 内容:
-- 数据库
use tmp;
-- 表名
select *
from tmp_jzl_20140725_test11
where
id='${hivevar:id}' and col2='${hivevar:col2}';
方式2:hive -e 'sql语句'(适合短语句)
直接执行sql语句
例如:
[root@cloud4 shell]# hive -e 'select * from t1'
静音模式:
[root@cloud4 shell]# hive -S -e 'select * from t1' (用法与第一种方式的静音模式同样,不会显示mapreduce的操做过程)
此处还有一亮点,用于导出数据到linux本地目录下
例如:
[root@cloud4 shell]# hive -e 'select * from t1' > test.txt
有点相似pig导出分析结果同样,都挺方便的
方式3:hive (直接使用hive交互式模式)
都挺方便的
介绍一种有意思的用法:
1.sql的语法
#hive 启动
hive>quit; 退出hive
hive> show databases; 查看数据库
hive> create database test; 建立数据库
hive> use default; 使用哪一个数据库
hive>create table t1 (key string); 建立表
对于建立表咱们能够选择读取文件字段按照什么字符进行分割
例如:
hive>create table t1(id ) '/wlan'
partitioned by (log_date string) 表示经过log_date进行分区
row format delimited fields terminated by '\t' 表示表明用‘\t’进行分割来读取字段
stored as textfile/sequencefile/rcfile/; 表示文件的存储的格式
存储格式的参考地址:http://blog.csdn.net/yfkiss/article/details/7787742
textfile 默认格式,数据不作压缩,磁盘开销大,数据解析开销大。
可结合Gzip、Bzip2使用(系统自动检查,执行查询时自动解压),但使用这种方式,hive不会对数据进行切分,从而没法对数据进行并行操做。
实例:
[plain] view plaincopy
> create table test1(str STRING)
> STORED AS TEXTFILE;
OK
Time taken: 0.786 seconds
#写脚本生成一个随机字符串文件,导入文件:
> LOAD DATA LOCAL INPATH '/home/work/data/test.txt' INTO TABLE test1;
Copying data from file:/home/work/data/test.txt
Copying file: file:/home/work/data/test.txt
Loading data to table default.test1
OK
Time taken: 0.243 seconds
SequenceFile是Hadoop API提供的一种二进制文件支持,其具备使用方便、可分割、可压缩的特色。
SequenceFile支持三种压缩选择:NONE, RECORD, BLOCK。 Record压缩率低,通常建议使用BLOCK压缩。
示例:
[plain] view plaincopy
> create table test2(str STRING)
> STORED AS SEQUENCEFILE;
OK
Time taken: 5.526 seconds
hive> SET hive.exec.compress.output=true;
hive> SET io.seqfile.compression.type=BLOCK;
hive> INSERT OVERWRITE TABLE test2 SELECT * FROM test1;
rcfile是一种行列存储相结合的存储方式。首先,其将数据按行分块,保证同一个record在一个块上,避免读一个记录须要读取多个block。其次,块数据列式存储,有利于数据压缩和快速的列存取。RCFILE文件示例:
实例:
[plain] view plaincopy
> create table test3(str STRING)
> STORED AS RCFILE;
OK
Time taken: 0.184 seconds
> INSERT OVERWRITE TABLE test3 SELECT * FROM test1;
总结:
相比textfile和SequenceFile,rcfile因为列式存储方式,数据加载时性能消耗较大,可是具备较好的压缩比和查询响应。数据仓库的特色是一次写入、屡次读取,所以,总体来看,rcfile相比其他两种格式具备较明显的优点。
hive>show tables; 查看该数据库中的全部表
hive>show tables ‘*t*’; //支持模糊查询
hive>show partitions t1; //查看表有哪些分区
hive>drop table t1 ; 删除表
hive不支持修改表中数据,可是能够修改表结构,而不影响数据
有local的速度明显比没有local慢:
hive>load data inpath '/root/inner_table.dat' into table t1; 移动hdfs中数据到t1表中
hive>load data local inpath '/root/inner_table.dat' into table t1; 上传本地数据到hdfs中
hive> !ls; 查询当前linux文件夹下的文件
hive> dfs -ls /; 查询当前hdfs文件系统下 '/'目录下的文件
但愿经过共享本身的笔记,来找到一群和我同样愿意分享笔记和心得的朋友,让你们一块儿进步
个人QQ:529815144,外号:小头