hive命令汇总

作者: 而立之年的技术控 | 来源:发表于2019-12-15 12:21 被阅读0次
前台启动(服务端)
cd  /export/servers/hive-1.1.0-cdh5.14.0
bin/hive --service hiveserver2


后台启动(服务端)
cd  /export/servers/hive-1.1.0-cdh5.14.0
nohup bin/hive --service hiveserver2  &

###    beeline 链接 hiveserver2
bin/beeline
beeline> !connect jdbc:hive2://node03.hadoop.com:10000


第三种交互方式(客户端)
###  使用 –e  参数来直接执行hql的语句
bin/hive -e "use myhive;select * from test;"

###  使用 –f  参数通过指定文本文件来执行hql的语句
vim hive.sql
use myhive;select * from test;

bin/hive -f hive.sql

===============================================================

1、创建数据库

create database if not exists myhive;
use  myhive;

###  说明:hive的表存放位置模式是由hive-site.xml当中的一个属性指定的
<name>hive.metastore.warehouse.dir</name>
<value>/user/hive/warehouse</value>

###  创建数据库并指定hdfs存储位置
create database myhive2 location '/myhive2';

###  修改数据库(可以使用alter  database  命令来修改数据库的一些属性。但是数据库的元数据信息是不可更改的,包括数据库的名称以及数据库所在的位置)
alter  database  myhive2  set  dbproperties('createtime'='20180611');

###  查看数据库详细信息
desc  database  myhive2;  ## 基本信息
desc database extended  myhive2;  ## 详细信息

###  删除数据库
drop  database  myhive2;    ## 删除一个空数据库,如果数据库下面有数据表,那么就会报错
drop  database  myhive  cascade;  ## 强制删除数据库,包含数据库下面的表一起删除

2、创建表

##################   总语法   ############
CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name
   [(col_name data_type [COMMENT col_comment], ...)]
   [COMMENT table_comment]
   [PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)]
   [CLUSTERED BY (col_name, col_name, ...)
   [SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS]
   [ROW FORMAT row_format]
   [STORED AS file_format]
   [LOCATION hdfs_path]

1)内部表

# hive表初体验
use myhive;
create table stu(id int,name string);
insert into stu values (1, "zhangsan");
select * from stu;

# 创建表并指定字段之间的分隔符
create  table if not exists stu2(id int ,name string) \
    row format delimited fields terminated by '\t' \
    stored as textfile \
    location '/user/stu2';
    
# 根据查询结果创建表
create table stu3 as select * from stu2;

# 根据已经存在的表结构创建表
create table stu4 like stu2;

# 查询表的类型
desc formatted stu2;

2) 外部表

# 老师表
create external table techer (t_id string,t_name string) \
    row format delimited fields terminated by '\t';
# 学生表
create external table student (s_id string,s_name string,s_birth string , s_sex string ) \
    row format delimited fields terminated by '\t';

# 从本地文件系统向表中加载数据
load data local inpath '/export/servers/hivedatas/student.csv' \
    into table student;

# 加载并覆盖已有的数据
load data local inpath '/export/servers/hivedatas/student.csv' \
    overwrite  into table student;

# 从hdfs文件系统像表中添加数据(需要提前将数据上传到hdfs文件系统,其实就是一个移动文件的操作)
cd /export/servers/hivedatas
hdfs dfs -mkdir -p /hivedatas
hdfs dfs -put techer.csv /hivedatas/
load data inpath '/hivedatas/techer.csv' into table techer;

###################  【分区表】   #################
# 创建分区表的语法
create table score(s_id string,c_id string, s_score int) \
    partitioned by (month string) \
    row format delimited fields terminated by '\t';

# 创建一个表带多个分区
create table score2 (s_id string,c_id string, s_score int) \
    partitioned by (year string,month string,day string) \
    row format delimited fields terminated by '\t';
    
# 建在数据到分区表中
load data local inpath '/export/servers/hivedatas/score.csv' \
    into table score partition (month='201806');
    
# 加载数据到一个多分区表中
load data local inpath '/export/servers/hivedatas/score.csv' \
    into table score2 partition(year='2018',month='06',day='01');
    
# 多分区联合查询使用union  all来实现
select * from score where month = '201806' \
    union all select * from score where month = '201806';

# 查看分区的命令
show  partitions  score;

# 添加一个分区
alter table score add partition(month='201805');

# 同时添加多个分区
alter table score add partition(month='201804') partition(month = '201803');

# 删除表分区
alter table score drop partition(month = '201806');

###############  【分区表的应用】  ############
hdfs dfs -mkdir -p /scoredatas/month=201806
hdfs dfs -put score.csv /scoredatas/month=201806/

create external table score4(s_id string, c_id string,s_score int) \
    partitioned by (month string) \
    row format delimited fields terminated by '\t' \
    location '/scoredatas';

# 进行表的修复,说白了就是建立我们表与我们数据文件之间的一个关系映射
# 修复成功之后即可看到数据已经全部加载到表当中去了
msck  repair   table  score4; 

3) 分桶表

###################  【分桶表】   #################
## 将数据按照指定的字段进行分成多个桶中去,
## 将数据按照字段进行划分,按照字段划分到多个文件中去
set hive.enforce.bucketing=true;  # 开启hive的桶表功能
set mapreduce.job.reduces=3;  # 设置reduce的个数

# 创建桶表,通过insert overwrite
create table course (c_id string,c_name string,t_id string) \
    clustered by(c_id) into 3 buckets \
    row format delimited fields terminated by '\t';
# 创建普通表,并通过insert  overwrite的方式将普通表的数据通过查询的方式加载到桶表当中去
create table course_common (c_id string,c_name string,t_id string) \
    row format delimited fields terminated by '\t';
# 普通表中加载数据
load data local inpath '/export/servers/hivedatas/course.csv' \
    into table course_common;
# 通过insert overwrite 给他桶表中加载数据
insert overwrite table course \
    select * from course_common cluster by(c_id);

4) 修改表

################# 【表重命名】 ##################
## 基本语法
alter  table  old_table_name  rename  to  new_table_name;

# 把表score4修改成score5
alter table score4 rename to score5;

################# 【增加\修改列信息】 ##################
desc score5;  # 查看表结构
alter table score5 add columns (mycol string, mysco string); ## 添加列
alter table score5 change column mysco mysconew int;  # 更新列

################# 【删除表】 ##################
drop table score5;

5) hive表中加载数据

########### 【直接向分区表中插入数据】 ###########
create table score3 like score;
insert into table score3 partition(month ='201807') values ('001','002','100');

########### 【通过查询插入数据】 ###########
# 通过load方式加载数据
load data local inpath '/export/servers/hivedatas/score.csv' \
    overwrite into table score partition(month='201806');
# 通过查询方式加载数据
create table score4 like score;
insert overwrite[a1]  table score4 partition(month = '201806') \
    select s_id,c_id,s_score from score;

########### 【多插入模式】 ###########
# 常用于实际生产环境当中,将一张表拆开成两部分或者多部分
# 给score表加载数据
load data local inpath '/export/servers/hivedatas/score.csv' \
    overwrite into table score partition(month='201806');
# 创建第一部分表
create table score_first( s_id string,c_id  string) \
    partitioned by (month string) \
    row format delimited fields terminated by '\t' ;
# 创建第二部分表
create table score_second(c_id string,s_score int) \
    partitioned by (month string) \
    row format delimited fields terminated by '\t';
# 分别给第一部分与第二部分表加载数据
from score \
    insert overwrite table score_first partition(month='201806') select s_id,c_id \
    insert overwrite table score_second partition(month = '201806') select c_id,s_score;  

############ 【查询语句中创建表并加载数据(as select)】#########
# 将查询的结果保存到一张表当中去
create table score5 as select * from score;

############ 【创建表时通过location指定加载数据路径】############
# 1)创建表时通过location指定加载数据路径(主要针对外部表)
create external table score6 (s_id string,c_id string,s_score int) \
    row format delimited fields terminated by '\t' \
    location '/myscore6';
# 2)上传数据到hdfs上
hdfs dfs -mkdir -p /myscore6;
hdfs dfs -put score.csv /myscore6;

########【export导出与import导入hive表数据(内部表操作)#######
# 都是在hdfs上
create table techer2 like techer;
export table techer to  '/export/techer';
import table techer2 from '/export/techer';

6) hive表中的数据导出

##############【insert导出】############
# 1)将查询的结果导出到本地
insert overwrite local directory '/export/servers/exporthive' \
    select * from score;
# 2)将查询的结果格式化导出到本地
insert overwrite local directory '/export/servers/exporthive' \
    row format delimited fields terminated by '\t' \
    collection items terminated by '#' \
    select * from student;
# 3)将查询出的结果导出到HDFS(没有local)
insert overwrite directory '/export/servers/exporthive' \
    row format delimited fields terminated by '\t' \
    collection items terminated by[a1]  '#' \
    select * from score;

##############【hadoop命令导出到本地】############
dfs -get \
    /export/servers/exporthive/000000_0 \
    /export/servers/exporthive/local.txt;

##############【hive shell命令导出】############
# 基本语法
hive -f/-e 执行语句或者脚本 > file
bin/hive -e "select * from myhive.score;" \
    > /export/servers/exporthive/score.txt

##############【export导出到HDFS上】############
export table score to '/export/exporthive/score';

7) 清空表数据

#### 只能清空内部表
truncate table score6;  # 清空外部表会报错

3、hive函数

#################【内置函数】#################
show fuctions;
desc function upper;
desc function extended upper;
 
 #################【自定义函数】#################
 1)UDF(一进一出)
 2)UDAF(多进一出)
 3)UDTF(一进多出)
 编程步骤:
(1)继承org.apache.hadoop.hive.ql.UDF
(2)需要实现evaluate函数;evaluate函数支持重载;
注意事项
(1)UDF必须要有返回类型,可以返回null,但是返回类型不能为void;
(2)UDF中常用Text/LongWritable等类型,不推荐使用java类型;

###### 第一步  创建maven  java 工程,导入jar包
<repositories>
    <repository>
        <id>cloudera</id>
 <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-common</artifactId>
        <version>2.6.0-cdh5.14.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-exec</artifactId>
        <version>1.1.0-cdh5.14.0</version>
    </dependency>
</dependencies>
<build>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.0</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <encoding>UTF-8</encoding>
        </configuration>
    </plugin>
     <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-shade-plugin</artifactId>
         <version>2.2</version>
         <executions>
             <execution>
                 <phase>package</phase>
                 <goals>
                     <goal>shade</goal>
                 </goals>
                 <configuration>
                     <filters>
                         <filter>
                             <artifact>*:*</artifact>
                             <excludes>
                                 <exclude>META-INF/*.SF</exclude>
                                 <exclude>META-INF/*.DSA</exclude>
                                 <exclude>META-INF/*/RSA</exclude>
                             </excludes>
                         </filter>
                     </filters>
                 </configuration>
             </execution>
         </executions>
     </plugin>
</plugins>
</build>

###### 第二步  开发java类继承UDF,并重载evaluate 方法
public class ItcastUDF extends UDF {
    public Text evaluate(final Text s) {
        if (null == s) {
            return null;
        }
        //返回大写字母
        return new Text(s.toString().toUpperCase());

    }
}

###### 第三步  将我们的项目打包,并上传到hive的lib目录下

###### 第四步  将我们的项目打包,并上传到hive的lib目录下
# 重命名我们的jar包名称
cd /export/servers/hive-1.1.0-cdh5.14.0/lib
mv original-day_06_hive_udf-1.0-SNAPSHOT.jar udf.jar
# hive的客户端添加我们的jar包
add jar /export/servers/hive-1.1.0-cdh5.14.0/lib/udf.jar;

###### 第五步  设置函数与我们的自定义函数关联
create temporary function tolowercase as 'cn.itcast.udf.ItcastUDF';

###### 第六步  使用自定义函数
select tolowercase('abc');

相关文章

  • hive命令汇总

    =========================================================...

  • Hive常用的几种交互操作

    查看hive下的交互命令方式 -help(hive 外) 命令:bin/hive -helpusage: hive...

  • 大数据开发之Hive优化篇2-Hive的explain命令

    备注:Hive 版本 2.1.1 一.Hive explain命令概述 Hive的explain命令用来看Hive...

  • HiveQL快速使用

    hive命令包括Hive cli 和 hiveQL命令 Hive cli cli 就是命令行界面,可以通过cli创...

  • Hive进阶

    hive配置,命令 hive查询显示列名 hive默认分隔符 \001 hive命令行中查看当前hive环境变量 ...

  • Hive运行方式

    1.最基本的hive命令行; Linux命令行直接hive 进入hive 命令行; 2.脚本运行方式(适用于生产环...

  • Hive 基础

    Hive 基础 Hive启动: 插入数据: 插入数据文件: Hive交互命令: 参数配置方式: hive> set...

  • hive的几个小知识点

    1.hive DEBUG日志:(hive终端中查看日志的命令): hive--hiveconfhive.root....

  • Hive常用基础语句(一)

    常用Hive命令 # 清屏 hive> !clear; # 退出 hive> exit; # 查看有哪些数据库 h...

  • hive基础操作(一)

    hive CLI 在shell环境下执行一次hive命令:hive -e select * from dual...

网友评论

    本文标题:hive命令汇总

    本文链接:https://www.haomeiwen.com/subject/plmknctx.html