Hive基本使用

Hive基本使用

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL

库操作

创建库

# 进入终端
beeline -u " jdbc:hive2://10.240.xx.xx:10000/dc_ods " -n user


# 判断是否存在并添加注释
create database if not exists zxl_test comment 'hive_test';

# 添加属性
create database if not exists zxl_test with dbproperties('creator'='zxl','date'='2019-04-09');

# 描述库
desc database zxl_test;
desc database extended zxl_test;

# 查看建库语句
show create database zxl_test;

删除库

# 库下无表,直接删除
drop database dbname;
drop database if exists dbname;

# 库下有表
方式1:先删除表再删除库;
方式2: drop database if exists zxl_test cascade;

表操作

创建表

# 内部表
create table student(id int, name string, sex string, age int,department string) row format delimited fields terminated by ",";

# 外部表
create external table student_ext(id int, name string, sex string, age int,department string) row format delimited fields terminated by "," location "/hive/student";

# 分区表
create external table student_ptn(id int, name string, sex string, age int,department string) partitioned by (city string) row format delimited fields terminated by "," location "/hive/student_ptn";
# 添加分区
alter table student_ptn add partition(city="beijing");

#分桶表
create external table student_bck(id int, name string, sex string, age int,department string) clustered by (id) sorted by (id asc, name desc) into 4 buckets row format delimited fields terminated by "," location "/hive/student_bck";

# 导入本地数据
load data local inpath "/data1/student.txt" into table student;

#使用CTAS创建表
 create table student_ctas as select * from student where id < 95012;
 
 # 复制表结构(able前面没有加external关键字则是内部表,否则外部表)
 create table student_copy like student;

查看表

# 查看库下的表
show tables in zxl_test;

# 模糊匹配表名
show tables like '*dent*';

# 查看表结构
desc student;
desc extended student;
desc formatted student;(格式好)

# 查看分区信息
show partitions student_ptn;

# 查看建表语句
show create table student_ptn;


修改表

# 修改表名
alter table student rename to new_student;

# 增加字段
alter table new_student add columns (score int);

# 修改字段定义
alter table new_student change name new_name string;

# 替换所有字段
alter table new_student replace columns (id int, name string, address string);
# 替换可以用来删除字段
alter table new_student replace columns (id int, name string);

# 删除表
drop table new_student;

# 清空表
truncate table student_ptn;

修改分区

# 静态分区
alter table student_ptn add partition(city="shenzhen");

# 动态分区 [内容直接插入到另一张表student_ptn_age中,并实现age为动态分区]
load data local inpath "/data1/student.txt" into table student_ptn partition(city="beijing");

create table student_ptn_age(id int,name string,sex string,department string) partitioned by (age int);

set hive.exec.dynamic.partition.mode=nonstrict;
insert overwrite table student_ptn_age partition(age) select id,name,sex,department,age from student_ptn;

# 删除分区
alter table student_ptn drop partition (city='beijing');

Hive函数

内置函数

# 查看内置函数
show functions;

# 显示函数的详细信息
desc function extended substr;

自定义函数UDF

UDF(user-defined function)作用于单个数据行,产生一个数据行作为输出。(数学函数,字 符串函数)

UDAF(用户定义聚集函数 User- Defined Aggregation Funcation):接收多个输入数据行,并产 生一个输出数据行。(count,max)

UDTF(表格生成函数 User-Defined Table Functions):接收一行输入,输出多行(explode)

Hive命令行

# hive -e sql语句  执行完退出终端
hive -e "select * from zxl_test.student";

#hive -f sql文件名 执行完退出终端
select * from zxl_test.student # test.sql内容
hive -f test.sql

# hive临时设置
查看属性值:  		set 属性名
临时修改属性值:   set 属性名 = 属性值

Shylin

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1.上传tar包 2.解压 tar -zxvf hive-1.2.1.tar.gz 3.安装mysql数据库 推荐yum 在线安装 4.配置hive (a)配置HIVE_HOME环境变量 vi conf/hive-env.sh 配置其中的$hadoop_home (b)配置元数据库信息 vi hive-site.xml 添加如下内容: javax.jdo.option.ConnectionURL jdbc:mysql://localhost:3306/hive?createDatabaseIfNotExist=true JDBC connect string for a JDBC metastore javax.jdo.option.ConnectionDriverName com.mysql.jdbc.Driver Driver class name for a JDBC metastore javax.jdo.option.ConnectionUserName root username to use against metastore database javax.jdo.option.ConnectionPassword hadoop password to use against metastore database 5.安装hive和mysq完成后,将mysql的连接jar包拷贝到$HIVE_HOME/lib目录下 如果出现没有权限的问题,在mysql授权(在安装mysql的机器上执行) mysql -uroot -p #(执行下面的语句 *.*:所有库下的所有表 %:任何IP地址或主机都可以连接) GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; FLUSH PRIVILEGES; 6. Jline包版本不一致的问题,需要拷贝hive的lib目录中jline.2.12.jar的jar包替换掉hadoop中的 /home/hadoop/app/hadoop-2.6.4/share/hadoop/yarn/lib/jline-0.9.94.jar 启动hive bin/hive ---------------------------------------------------------------------------------------------------- Hive几种使用方式: 1.Hive交互shell bin/hive 2.Hive JDBC服务(参考java jdbc连接mysql) 3.hive启动为一个服务器,来对外提供服务 bin/hiveserver2 nohup bin/hiveserver2 1>/var/log/hiveserver.log 2>/var/log/hiveserver.err & 启动成功后,可以在别的节点上用beeline去连接 bin/beeline -u jdbc:hive2://mini1:10000 -n root 或者 bin/beeline ! connect jdbc:hive2://mini1:10000 4.Hive命令 hive -e ‘sql’ bin/hive -e 'select * from t_test'
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值