百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程字典 > 正文

hive命令汇总

toyiye 2024-06-21 12:39 9 浏览 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');

相关推荐

为何越来越多的编程语言使用JSON(为什么编程)

JSON是JavascriptObjectNotation的缩写,意思是Javascript对象表示法,是一种易于人类阅读和对编程友好的文本数据传递方法,是JavaScript语言规范定义的一个子...

何时在数据库中使用 JSON(数据库用json格式存储)

在本文中,您将了解何时应考虑将JSON数据类型添加到表中以及何时应避免使用它们。每天?分享?最新?软件?开发?,Devops,敏捷?,测试?以及?项目?管理?最新?,最热门?的?文章?,每天?花?...

MySQL 从零开始:05 数据类型(mysql数据类型有哪些,并举例)

前面的讲解中已经接触到了表的创建,表的创建是对字段的声明,比如:上述语句声明了字段的名称、类型、所占空间、默认值和是否可以为空等信息。其中的int、varchar、char和decimal都...

JSON对象花样进阶(json格式对象)

一、引言在现代Web开发中,JSON(JavaScriptObjectNotation)已经成为数据交换的标准格式。无论是从前端向后端发送数据,还是从后端接收数据,JSON都是不可或缺的一部分。...

深入理解 JSON 和 Form-data(json和formdata提交区别)

在讨论现代网络开发与API设计的语境下,理解客户端和服务器间如何有效且可靠地交换数据变得尤为关键。这里,特别值得关注的是两种主流数据格式:...

JSON 语法(json 语法 priority)

JSON语法是JavaScript语法的子集。JSON语法规则JSON语法是JavaScript对象表示法语法的子集。数据在名称/值对中数据由逗号分隔花括号保存对象方括号保存数组JS...

JSON语法详解(json的语法规则)

JSON语法规则JSON语法是JavaScript对象表示法语法的子集。数据在名称/值对中数据由逗号分隔大括号保存对象中括号保存数组注意:json的key是字符串,且必须是双引号,不能是单引号...

MySQL JSON数据类型操作(mysql的json)

概述mysql自5.7.8版本开始,就支持了json结构的数据存储和查询,这表明了mysql也在不断的学习和增加nosql数据库的有点。但mysql毕竟是关系型数据库,在处理json这种非结构化的数据...

JSON的数据模式(json数据格式示例)

像XML模式一样,JSON数据格式也有Schema,这是一个基于JSON格式的规范。JSON模式也以JSON格式编写。它用于验证JSON数据。JSON模式示例以下代码显示了基本的JSON模式。{"...

前端学习——JSON格式详解(后端json格式)

JSON(JavaScriptObjectNotation)是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。它基于JavaScriptProgrammingLa...

什么是 JSON:详解 JSON 及其优势(什么叫json)

现在程序员还有谁不知道JSON吗?无论对于前端还是后端,JSON都是一种常见的数据格式。那么JSON到底是什么呢?JSON的定义...

PostgreSQL JSON 类型:处理结构化数据

PostgreSQL提供JSON类型,以存储结构化数据。JSON是一种开放的数据格式,可用于存储各种类型的值。什么是JSON类型?JSON类型表示JSON(JavaScriptO...

JavaScript:JSON、三种包装类(javascript 包)

JOSN:我们希望可以将一个对象在不同的语言中进行传递,以达到通信的目的,最佳方式就是将一个对象转换为字符串的形式JSON(JavaScriptObjectNotation)-JS的对象表示法...

Python数据分析 只要1分钟 教你玩转JSON 全程干货

Json简介:Json,全名JavaScriptObjectNotation,JSON(JavaScriptObjectNotation(记号、标记))是一种轻量级的数据交换格式。它基于J...

比较一下JSON与XML两种数据格式?(json和xml哪个好)

JSON(JavaScriptObjectNotation)和XML(eXtensibleMarkupLanguage)是在日常开发中比较常用的两种数据格式,它们主要的作用就是用来进行数据的传...

取消回复欢迎 发表评论:

请填写验证码