欢迎投稿

今日深度:

hive( 二)--hive常用命令操作,

hive( 二)--hive常用命令操作,


一、简介
这节将介绍hive常用命令操作,包括数据库操作、表操作、数据操作等。
hive的采用了类sql的语法,也称为hql。
二、命令执行方式
1、以交互方式执行命令
bin/hive
此时可以在命令行中输入set hive.cli.print.current.db=true 可以显示当前选中数据库。
2、以非交互方式执行命令
bin/hive -e  hql
3、以文件方式执行命令
bin/hive -f hql文件
备注:
hive常用参数如下:

bin/hive -option
option:
-H 帮助
-e 执行hql语句
-f 执行hql文件
-p 指定端口
-S 不打印日志
-i 从文件初始化hql
-v 输出执行的hql到控制台

三、常用命令
这里采用非交互方式执行hql语句,定义:

hive_exe="/home/china/programs/hive/bin/hive -e"

1、数据库操作

######### 数据库操作 ###########
#查看数据库
${hive_exe} "show databases;"

#创建数据库
${hive_exe} "create database school;"

#删除数据库
${hive_exe} "drop database school;"

#选中数据库
${hive_exe} "use school;"
##################################

2、表操作

######### 表操作 ##################
#创建表
${hive_exe} "create table school.stu(id int, stu_name string)  row format delimited fields terminated by ',' ;"

#查看表 方式一
${hive_exe} "desc school.stu;"

#查看表 方式二
show create table stu;

#修改表 修改表名
#格式:alter table name rename to new_name
${hive_exe} "alter table school.stu_1 rename to school.stu_2;"

#修改表 新增字段
#格式:alter table name add columns (col_spec[, col_spec...])
${hive_exe} "alter table school.stu_2 add columns (stu_id int);"

#修改表 修改字段
#格式:alter table name change column_name new_name new_type
${hive_exe} "alter table school.stu_2 change stu_id stu_id string;"

#修改表 删除字段,hive没有删除字段的语句,不过可以通过replace间接删除
#格式:alter table name replace columns(col_spec[, col_spec...]);
${hive_exe} "alter table school.stu_2 replace columns(id int, stu_name string);"

#删除表
#格式:drop table if exists table_name;
${hive_exe} "drop table if exists school.stu_2;"
##################################

3、数据操作
hive没有行级的数据增删改操作。

######### 数据操作 ##################
#hive没有行级的数据插入、更新、删除操作
#从本地文件导入数据
#格式: load data local inpath data_file overwrite into table table_name
${hive_exe} "load data local inpath 'hive_data_stu.csv' overwrite into table school.stu_1;"

#从hdfs目录导入数据
#格式: load data inpath data_file overwrite into table table_name
${hive_exe} "load data inpath '/hive_data_dump/000000_0' overwrite into table school.stu;"

#查询
${hive_exe} "select * from school.stu_1;"
#条件查询
${hive_exe} "select * from stu where stu_name='apple';"


#数据导出到本地目录
#格式: insert overwrite local directory result_dir select * from table_name;
${hive_exe} "insert overwrite local directory '/home/china/shell_space/my_study/hive_data_dump' row format delimited fields terminated by ',' select * from school.stu_1;"
${hive_exe} "insert overwrite local directory '/home/china/shell_space/my_study/hive_data_dump' select * from school.stu_1;"

#数据导出到hdfs目录
#格式: insert overwrite directory result_dir select * from table_name;
${hive_exe} "insert overwrite directory '/hive_data_dump' row format delimited fields terminated by ',' select * from school.stu_1;"
${hive_exe} "insert overwrite directory '/hive_data_dump' select * from school.stu_1;"
################################

四、常用数据类型

######### 常用数据类型 ###########
boolean 布尔类型,true或false

#整数
tinyint 1byte有符号整数
smallint 2byte有符号整数
int 4byte有符号整数
bigint 8byte有符号整数

#浮点数
float 4byte单精度浮点数
double 8byte双精度浮点数

binary 字节数组

#字符串
string 字符串

#日期
timestamp  可以为整数(距1970年1月1日的秒数),也可以是浮点数(距1970年1月1日的秒数,小数点后是纳秒,保留9位),也可以是字符串(格式为YYYY-MM-DD hh:mm:ss.fffffffff)

#集合数据类型
array<type> 数组类型,如array<string>
struct<col:type, col:type> 结构体类型,如:struct<id:string,age:int>
map<key_type, val_type> 键值对类型,如:map<string,string>
################################

类型之前转换可以使用cast(s as type)






 

www.htsjk.Com true http://www.htsjk.com/hive/31552.html NewsArticle hive( 二)--hive常用命令操作, 一、简介 这节将介绍hive常用命令操作,包括数据库操作、表操作、数据操作等。 hive的采用了类sql的语法,也称为hql。 二、命令执行方式 1、以交互方式...
相关文章
    暂无相关文章
评论暂时关闭