Hive安装和使用( 三 )

3.2.3查询和标注sql语法基本一致,例如查询一天的日活
select count(distinct cuid) from testhive;Total MapReduce CPU Time Spent: 1 minutes 5 seconds 520 msecOK2942630Time taken: 33.79 seconds, Fetched: 1 row(s)3.3 hive命令的执行方式1).CLI 方式直接执行 2).作为字符串通过shell调用hive –e执行(-S开启静默,去掉”OK”,”Time taken”)
Hql作为字符串在shell脚本中执行,查询结果可以直接导出到本地本件(默认分隔符为t): hive -e "use ${database};select * from tb"> tb.txt如果字符串较长的话,可以按照如下方式书写,sql=$(cat <<endtag 字符串endtag)方式可以将字符串复制给sql
file_path='/home/abc.txt'sql=$(cat <<!EOFUSE pmp;set mapred.queue.names=queue3;drop table if exists people_targeted_delivery;create table people_targeted_delivery( special_tag_id int,cnt bigint);INSERT OVERWRITE LOCAL DIRECTORY $file_pathROW FORMAT DELIMITED FIELDS TERMINATED BY 't' select special_tag_id,count(1) from t_pmp_special_user_tags group by special_tag_id;!EOF)############execute begin###########echo $sql$HIVE_HOME/bin/hive -e "$sql"exitCode=$?if [ $exitCode -ne 0 ];thenecho "[ERROR] hive execute failed!"exit $exitCodefi3).作为独立文件,通过shell调用 hive –f
mytest.hql书写我们编写好的hivesql文件
hive -fmytest.hql4.配置远程机器访问基于资源隔离的原则,不可能所有的hive操作会登录到hive服务本地操作,更多的是在其他机器进行.此时我们需要配置远程访问.
4.1 远程配置使用远程模式,需要在hadoop的core-site.xml文件中添加一下属性
其中,XXX是用来代理其它用户访问hdfs的用户名,此处我的配置如下
<property><name>hadoop.proxyuser.xxx.hosts</name><value>*</value></property><property><name>hadoop.proxyuser.xxx.groups</name><value>*</value></property>重启
#启动./hadoop/sbin/start-all.sh#./hadoop/sbin/stop-all.sh#关闭安全模式 hdfs dfsadmin -safemode leave设置hive-site.xml
<property><name>hive.metastore.uris</name><value>thrift://ip:9083</value></property>设置防火墙
vi /etc/sysconfig/iptable-A INPUT -m state --state NEW -m tcp -p tcp --dport 9083 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 10000 -j ACCEPTsystemctl restart iptables.service启动metastore或者hiveserver2
nohup hive --service metastore &# 下面这个支持beeline连接,官方nohup hive --service hiveserver2 & 4.2 客户端配置

  1. 确保安装了java环境
yumlocalinstall jdk-8u151-linux-x64.rpm
  1. 新建一个hadoopclient 的目录,用于存放hadoopclient和hiveclient,如下.所谓客户端就是copy远程集群的目录即可
# tree -L 1.├── hadoop├── hive
  1. 配置正确环境变量
export JAVA_HOME=/usr/java/jdk1.8.0_151/export HADOOP_HOME=/home/xxx/hadoopclient/hadoopexport HIVE_HOME=/home/xxx/hadoopclient/hiveexport HIVE_CONF_DIR=${HIVE_HOME}/confexport HADOOP_OPTS="-Djava.library.path=$HADOOP_HOME/lib:$HADOOP_HOME/lib/native"export PATH=$JAVA_HOME/bin:$HOME/bin:$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$HIVE_HOME/bin:$PATH
  1. 客户端发起连接
  • 方式1 -hive
继续使用hivecli命令
hive
  • 方式2-beeline(推荐)
beeline!connect jdbc:hive2://ip:10000#或者beeline -u jdbc:hive2://ip:10000beeline -u"jdbc:hive2://ip:10000/testdb;" Beeline和其他工具有一些不同,执行查询都是正常的SQL输入,但是如果是一些管理的命令,比如进行连接,中断,退出,执行Beeline命令需要带上“!”,不需要终止符 。常用命令介绍:1、!connect url –连接不同的Hive2服务器2、!exit –退出shell3、!help –显示全部命令列表4、!verbose –显示查询追加的明细The Beeline CLI 支持以下命令行参数:OptionDescription--autoCommit=[true/false] ---进入一个自动提交模式:beeline --autoCommit=true--autosave=[true/false]---进入一个自动保存模式:beeline --autosave=true--color=[true/false]---显示用到的颜色:beeline --color=true--delimiterForDSV= DELIMITER ---分隔值输出格式的分隔符 。默认是“|”字符 。--fastConnect=[true/false]---在连接时,跳过组建表等对象:beeline --fastConnect=false--force=[true/false]---是否强制运行脚本:beeline--force=true--headerInterval=ROWS---输出的表间隔格式,默认是100: beeline --headerInterval=50--help ---帮助beeline --help--hiveconf property=value---设置属性值,以防被hive.conf.restricted.list重置:beeline --hiveconf prop1=value1--hivevar name=value---设置变量名:beeline --hivevar var1=value1--incremental=[true/false]---输出增量--isolation=LEVEL---设置事务隔离级别:beeline --isolation=TRANSACTION_SERIALIZABLE--maxColumnWidth=MAXCOLWIDTH ---设置字符串列的最大宽度:beeline --maxColumnWidth=25--maxWidth=MAXWIDTH ---设置截断数据的最大宽度:beeline --maxWidth=150--nullemptystring=[true/false]---打印空字符串:beeline --nullemptystring=false--numberFormat=[pattern]---数字使用DecimalFormat:beeline --numberFormat="#,###,##0.00"--outputformat=[table/vertical/csv/tsv/dsv/csv2/tsv2] ---输出格式:beeline --outputformat=tsv--showHeader=[true/false]---显示查询结果的列名:beeline --showHeader=false--showNestedErrs=[true/false] ---显示嵌套错误:beeline --showNestedErrs=true--showWarnings=[true/false] ---显示警告:beeline --showWarnings=true--silent=[true/false]---减少显示的信息量:beeline --silent=true--truncateTable=[true/false] ---是否在客户端截断表的列--verbose=[true/false]---显示详细错误信息和调试信息:beeline --verbose=true-d <driver class>---使用一个驱动类:beeline -d driver_class-e <query>---使用一个查询语句:beeline -e "query_string"-f <file>---加载一个文件:beeline -f filepath多个文件用-e file1 -e file2-n <username>---加载一个用户名:beeline -n valid_user-p <password>---加载一个密码:beeline -p valid_password-u <database URL> ---加载一个JDBC连接字符串:beeline -u db_URL


推荐阅读