$HIVE_HOME/bin/hive是一个shell工具,它可以用来运行于交互或批处理方式配置单元查询。
语法:
Usage: hive [-hiveconf x=y]*[<-i filename>]* [<-ffilename>|<-equery-string>] [-S]
-i<filename>: Initialization Sql fromfile (executed automatically and silently before any othercommands)
-e 'quoted querystring': 运行引号内sql查询语句 Sql from command line
-f<filename>: 从文件中运行sql语句 Sqlfrom file
-S: Silent mode in interactive shellwhere only data is emitted
-hiveconf x=y: Use this to sethive/hadoop configuration variables.
-e 与 -f 不能同时使用。In the absence ofthese options, interactive shell is started. 但是,-i 能够与任意参数同时使用。多个–i 的实例可以用来执行多个初始化脚本scripts。However, -i can be used with any otheroptions. Multiple instances of -i can be used to execute multipleinit scripts.
通过命令:hive –h 可以查看应用的帮助文档.To see thisusage help, run hive -h
运行一个查询:
$HIVE_HOME/bin/ hive -e 'selectcount(*) from c02_clickstat_fatdt1'
Example of setting hive configurationvariables
$HIVE_HOME/bin/hive -e 'select a.colfrom tab1 a' -hiveconfhive.exec.scratchdir=/home/my/hive_scratch -hiveconfmapred.reduce.tasks=32
将查询结果导出到一个文件
HIVE_HOME/bin/hive -S -e 'select count(*) from c02_clickstat_fatdt1' >a.txt
运行一个脚本
HIVE_HOME/bin/hive -f/home/my/hive-script.sql
Example of running an initializationscript before entering interactive mode
HIVE_HOME/bin/hive -i/home/my/hive-init.sql
hive使用 ^A 符号作为域的分隔符在python中可以使用line.split('\x01')来进行切分
如果不用命令行形式,用insert overwrite local directory ‘path’ select *
则用命令 sed -e 's/\x01/\t/g' file 自定义分隔符来替换hive默认分隔符
echo "abc 23" | sed 's/[ ]\{2,\}/ /' 把多个空格替换成一个空格
在python中可以使用line.split('\x01')来进行切分
如果确实需要将查询结果导出到本地文件,最好使用hive的命令:
- bin/hive -e "select * from test" >> res.csv
- 或者是:
- bin/hive -f sql.q >> res.csv
- 其中文件sql.q写入你想要执行的查询语句