《ProgrammingHive》阅读笔记-第二章

书本第二章的一些知识点,在cloudera-quickstart-vm-5.8.0-0上进行操作。

  • 配置文件
    配置在/etc/hive/conf/hive-site.xml文件里面,采用mysql作为metastore。"hive.metastore.warehouse.dir"这个属性没有在文件中进行配置,因此采用默认值/user/hive/warehouse作为Hive的默认路径。
[cloudera@quickstart ~]$ hdfs dfs -ls /user/hive/warehouse
Found 9 items
drwxrwxrwx   - cloudera supergroup          0 2016-08-27 02:34 /user/hive/warehouse/categories
drwxrwxrwx   - cloudera supergroup          0 2016-08-27 02:37 /user/hive/warehouse/customers
drwxrwxrwx   - cloudera supergroup          0 2016-08-27 02:39 /user/hive/warehouse/departments
drwxrwxrwx   - cloudera supergroup          0 2016-08-27 02:42 /user/hive/warehouse/order_items
drwxrwxrwx   - cloudera supergroup          0 2016-08-27 02:44 /user/hive/warehouse/orders
drwxr-xr-x   - hdfs     supergroup          0 2016-12-27 00:34 /user/hive/warehouse/original_access_logs
drwxrwxrwx   - cloudera supergroup          0 2016-08-27 02:46 /user/hive/warehouse/products
drwxrwxrwx   - cloudera supergroup          0 2016-12-27 05:52 /user/hive/warehouse/tokenized_access_logs
drwxrwxrwx   - cloudera supergroup          0 2016-09-04 02:09 /user/hive/warehouse/userinfo
  • 进入CLI
    $ hive
    使用set可以查看配置信息hive> set;

  • 临时变量赋值和查看

hive> set foo;
foo=bar
hive> set hivevar:foo
    > ;
hivevar:foo=bar

define的变量只在当前会话有效,define中赋值的变量在hivevar命名空间。

  • hiveconf命名空间变量
[cloudera@quickstart ~]$ hive --hiveconf hive.cli.print.current.db=true

Logging initialized using configuration in file:/etc/hive/conf.dist/hive-log4j.properties
WARNING: Hive CLI is deprecated and migration to Beeline is recommended.
hive (default)> set hiveconf:hive.cli.print.current.db=false;
hive> set hiveconf:hive.cli.print.current.db=true;
hive (default)> 

同时,我们也可以在hiveconf中增加新的变量。

[cloudera@quickstart ~]$ hive --hiveconf y=5

Logging initialized using configuration in file:/etc/hive/conf.dist/hive-log4j.properties
WARNING: Hive CLI is deprecated and migration to Beeline is recommended.
hive> set y;
y=5
  • system命名空间
    用于访问Java系统属性,system命名空间下的变量也可以读写。
hive> set system:user.name;
system:user.name=cloudera
hive> set system:user.name=camash
    > ;
hive> set system:user.name;
system:user.name=camash
hive> set env:HOMJE;
env:HOME=/home/cloudera
  • 获得所有定义的表
hive> show tables like '*';
OK
categories
customers
departments
intermediate_access_logs
order_items
orders
products
tokenized_access_logs
userinfo
Time taken: 0.065 seconds, Fetched: 9 row(s)
  • 单次执行hive命令
    在shell中单次执行,打印结果在shell窗口。
[cloudera@quickstart ~]$ hive -e "select * from userinfo limit 3";

Logging initialized using configuration in file:/etc/hive/conf.dist/hive-log4j.properties
OK
root    x       0       0       root    /root   /bin/bash
bin     x       1       1       bin     /bin    /sbin/nologin
daemon  x       2       2       daemon  /sbin   /sbin/nologin
Time taken: 2.049 seconds, Fetched: 3 row(s)

使用-S命令减少查询过程信息,从而可以将查询结果放至文件。

[cloudera@quickstart ~]$ hive -S -e "select * from userinfo limit 3"; > /tmp/myquery
[cloudera@quickstart ~]$ cat /tmp/myquery

同时方便执行set命令

[cloudera@quickstart ~]$ hive -S -e "set" | grep warehouse
hive.metastore.warehouse.dir=/user/hive/warehouse
hive.warehouse.subdir.inherit.perms=true
  • 从文件中执行

第一种方式使用hive -f命令,第二种方式hive对话框中使用source命令。

[cloudera@quickstart ~]$ hive -f filequery.hql 

Logging initialized using configuration in file:/etc/hive/conf.dist/hive-log4j.properties
OK
root    x       0       0       root    /root   /bin/bash
bin     x       1       1       bin     /bin    /sbin/nologin
daemon  x       2       2       daemon  /sbin   /sbin/nologin
Time taken: 2.504 seconds, Fetched: 3 row(s)
[cloudera@quickstart ~]$ cat filequery.hql
select * from userinfo limit 3;
hive> source /home/cloudera/filequery.shql
    > ;
OK
root    x       0       0       root    /root   /bin/bash
bin     x       1       1       bin     /bin    /sbin/nologin
daemon  x       2       2       daemon  /sbin   /sbin/nologin
Time taken: 1.648 seconds, Fetched: 3 row(s)
  • 在hive会话中执行shell命令
hive> ! pwd
    > ;
/home/cloudera
原文地址:https://www.cnblogs.com/shenfeng/p/programminghive_2.html