hive 命令行
--------------------------------
1.定义变量:
$>hive -d name=hyxy; //进入到客户端,并定义了变量为name,值为hyxy;作用域在hivevar下
查看设置后的属性值:
hive> set name;
hive> set hivevar:name;
查看系统默认的所有属性项:
hive> set name=zhangyuejiu;
hive> set -v;
在shell命令行中,可以使用${}方式引用变量
hive> create table ${name}(id int,${name} string);
hive> show tables;
tt
zhangyuejiu
hive> describe zhangyuejiu;
id int
zhangyuejiu string
应用:显示当前所在的database:
hive> set hive.cli.print.current.db;
hive.cli.print.current.db=false
hive> set hive.cli.print.current.db=true;
hive (default)> show databases;
2.hive中“一次使用”命令
$> hive -e "select * from tt";
$> hive -S -e "select * from tt";
3.hive执行sql文件
要求:一般情况下,将文件定义为*.q或*.hql;
$>echo 'select * from tt' >> ~/text.hql
$>hive -f ~/text.hql;
hive> source ~/text.hql;
4.设置默认加载初始化文件:
$>cd ~
$>gedit .hiverc
set hive.cli.print.current.db=true;
set name=hyxy;
$>hive;
测试成功!
5.hive CLI自动补全。
6.默认情况下,在/home/hyxy目录下,可以查看.hivehistory文件,显示历史SQL的操作;
7.在hive Cli可以使用shell命令
hive>!clear;
8.在hive Cli中可以使用hadoop的dfs命令。
hive>dfs -lsr /user;
9.查找表时,显示字段名称;
hive>set hive.cli.print.header=true;
hive基本数据类型
------------------------------------
1.基本数据类型
hive类型 说明 java类型 实例
1).tinyint 1byte有符号的整数 byte 20
2).smalint 2byte有符号的整数 short 20
3).int 4byte有符号的整数 int 20
4).bigint 8byte有符号的整数 long 20
5).boolean 布尔类型,true或false boolean true
6).float 单精度 float 3.217
7).double 双精度 double 3.212
8).string 字符序列,单双即可 string ‘zhang’;“ashakjds”
9).timestamp 时间戳,精确的纳秒 timestamp ‘158030219111’
10).binary 字节数组 byte[]
2.集合数据类型
hive类型 说明 java类型 实例
1).struct 对象类型,可以通过字段名.元素名来访问 object struct('name','age')
2).map 一组键值对的元组 map map('name','zhangsan','age','23')
3).array 数组 array array('name','age')
4).union 组合
3.案例:
hive>create table employees(
> name string,
> salary float,
> subordinates array<string>,
> deductions map<string,float>,
> address struct<street:string,city:string,state:string,zip:int>
> );
hive数据编码格式
-----------------------------------
1.默认hive通过^A(\001)、^B(\002)、^C(\003)分别对列、(array和struct)、map进行匹配;
2.创建表时,可以通过以下命令进行设置:
row format delimited
fields terminated by '\001'
collection items terminated by '\002'
map keys terminated by '\003'
lines terminated by '\n'
3.加载数据
$>cd ~
$>cp /mnt/hgfs/2.安装环境/data/employees/employees.txt .
hive> load data local inpath '/home/hyxy/employees.txt' into table employees;
hive>select * from employees;
hive读时模式
---------------------------------
1.传统的关系型数据库在进行数据加载时,必须验证数据格式是否符合表字段定义,如果不符合,数据将无法插入
至数据库表中。这种模式称为“写时模式”。
2.hive中,数据加载过程采用“读时模式”: