`

1 hive变量调用几种方式

    博客分类:
  • hive
 
阅读更多

 

 参考链接  http://www.crazyant.net/1451.html   疯狂蚂蚁

 

目前  hive需要的外部参数完全可以通过 将hive脚本写在 xxx.sh中 利用sh的变量来获取

这是目前工作中最常用方式。

 



 

 

1 hive使用外部变量: 分为4种情况,

 

hive中引用外部变量:  在代码中引用时使用${...};

方式1:
[root@h2single bin]# hive -d name=username;      随便定义变量 后在hive中使用此变量  
hive> create table t1(${name} string);
OK
Time taken: 3.499 seconds
hive> describe t1;
OK
username                string   





方式2:

hiveconf: 覆盖hive-site hive-default的值:

[root@h2single bin]# pwd
/usr/local/hive/bin
[root@h2single bin]# hive --hiveconf hive.cli.print.current.db=true;  显示库名称

hive (default)>   展示库名称为default

[root@h2single bin]# hive --hiveconf hive.cli.print.header=true;    查表记录时,展示字段名称

hive> select * from t4;
OK
t4.root    展示字段




方式3:system 读取java 变量 从java定义的配置属性,如system:user.name

hive> create table t3(${system:user.name} string)
    > ;
OK
Time taken: 1.278 seconds
hive> describe t3;
OK
root                    string                                      
Time taken: 0.894 seconds, Fetche


方式4:env 读取shell环境变量

hive> create table t4(${env:USER} string);
OK
Time taken: 2.891 seconds
hive> describe t4;
OK
root                    string  

 

 

 

 

2 启动时 指定hive目录:
#hive --hiveconf hive.metastore.warehouse.dir=/hive/$USER;

 

 

 

3 linux环境下执行hive语句:

 


 

如下命令均在linux下执行:
[root@h2single bin]# hive -e "show tables";
 hive -e "show tables"    linux shell下将此语句发送到shell执行 后将结果打印在linux shell下
 hive -e "show tables">tablename  将结果写到当前linux目录的tablename文件中
 hive -S -e "show tables">tablename  将和hive交互过程信息 比如耗时等 不在控制台中显示出来
$ hive -S -e "select * from stu limit 3;" > /tmp/myquery
$ cat /tmp/myquery   里面会将交互信息OK  耗时信息Time taken:4.95 seconds忽略掉
1  zhangsan
2  lisi



[root@h2single bin]# vi test.hql
show tables



[root@h2single bin]# hive -f test.hql >> a.txt

  将外部文件a.hql内容发送到hive中执行并将返回结果 追加写到当前shell目录下的 a.txt中

[root@h2single bin]# cat a.txt 
t1
t3
t4


hive -f 操作hdfs文件写法:



hive -f hdfs://<namenode>:<port>/hive-script.sql

 

 

4 hive 环境下执行linux语句:   hive环境下访问hdfs内容:
 

 

hive> dfs -ls /
    > ;
Found 4 items
drwxr-xr-x   - root supergroup          0 2015-03-06 02:35 /flume
drwxr-xr-x   - root supergroup          0 2015-03-12 17:31 /hbase
drwx-wx-wx   - root supergroup          0 2015-03-13 19:09 /tmp
drwx------   - root supergroup          0 2015-03-13 19:17 /user
hive> 

hive>dfs -rm -r /root/t5


hive环境下访问shell内容:

hive>!mkdri /usr/local/test;
 hive> ! mkdir 111;
hive> ! ls;
111
a.txt
ext
hive
hive-config.sh
test.hql
hive> 

 

 

 5  hiverc 文件 做初始化操作:

 

 

[root@h2single bin]# ls -a ~


[root@h2single bin]# vi ~/.hiverc  创建此文件 里面添加如下内容

set  hive.cli.print.current.db=true;
set  hive.cli.print.header=true;



此时在进入hive> 就会看到 显示数据库名称 查询表也会展示表字段名称

 

 

  • 大小: 15.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics