hive命令汇总

前台启动(服务端)
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');
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,980评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,422评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,130评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,553评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,408评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,326评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,720评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,373评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,678评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,722评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,486评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,335评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,738评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,009评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,283评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,692评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,893评论 2 335

推荐阅读更多精彩内容

  • hive.ddl.output.format:hive的ddl语句的输出格式,默认是text,纯文本,还有json...
    博弈史密斯阅读 1,906评论 0 6
  • 快乐大数据第5次课 hive(1)工作原理Hive的执行入口是Driver,执行的SQL语句首先提交到Drive驱...
    快乐大数据阅读 376评论 0 0
  • Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供类SQL查询功能。本...
    felix521阅读 1,291评论 0 0
  • 2.6. Hive 的交互方式 第一种交互方式 bin/hive 创建一个数据库 第二种交互方式:使用sql语句或...
    你值得拥有更好的12138阅读 10,860评论 0 4
  • 七月流火。有一个词是形容到底有多热----七月流火。流动的火,是指空气吧。晌午时分,几乎就是夜深人静的万籁俱寂,实...
    陈春艳_cc36阅读 395评论 0 0