Hive| DDL| DML

 

类型转换

可以使用CAST操作显示进行数据类型转换
例如CAST('1' AS INT)将把字符串'1' 转换成整数1;如果强制类型转换失败,如执行CAST('X' AS INT),表达式返回空值 NULL。
0: jdbc:hive2://hadoop101:10000> select '1'+2, cast('1'as int) + 2; +------+------+--+ | _c0 | _c1 | +------+------+--+ | 3.0 | 3 |

 

对于Hive的String类型相当于数据库的varchar类型,该类型是一个可变的字符串,不过它不能声明其中最多能存储多少个字符,理论上它可以存储2GB的字符数。

集合数据类型

Hive有三种复杂数据类型ARRAY、MAP 和 STRUCT。ARRAY和MAP与Java中的Array和Map类似,而STRUCT与C语言中的Struct类似,它封装了一个命名字段集合,复杂数据类型允许任意层次的嵌套。

[kris@hadoop101 datas]$ vim test.txt
songsong,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long guan_beijing
yangyang,caicai_susu,xiao yang:18_xiaoxiao yang:16, chao yang_beijing

hive (default)> create table test(
              > name string,
              > friends array<string>,
              > children map<string, int>,
              > address struct<street:string, city:string>)
              > row format delimited fields terminated by ','
              > collection items terminated by '_'
              > map keys terminated by ':'
              > lines terminated by '\n';
OK
Time taken: 0.249 seconds
hive (default)> load data local inpath '/opt/module/datas/test.txt/' into table test;
Loading data to table default.test
Table default.test stats: [numFiles=1, totalSize=145]
OK
Time taken: 1.365 seconds
0: jdbc:hive2://hadoop101:10000> select * from test;
0: jdbc:hive2://hadoop101:10000> select friends[1], children['xiao song'], address.city from test where name="songsong";
+-------+------+----------+--+
|  _c0  | _c1  |   city   |
+-------+------+----------+--+
| lili  | 18   | beijing  |
+-------+------+----------+--+
1 row selected (0.321 seconds)

 

DDL数据定义

创建数据库

创建一个数据库,数据库在HDFS上的默认存储路径是/user/hive/warehouse/*.db。

修改

用户可以使用ALTER DATABASE命令为某个数据库的DBPROPERTIES设置键-值对属性值,来描述这个数据库的属性信息。数据库的其他元数据信息都是不可更改的,包括数据库名和数据库所在的目录位置。

① 创建数据库
0: jdbc:hive2://hadoop101:10000> create database if not exists db_hive; 避免要创建的数据库已经存在错误,增加if not exists判断。(标准写法) No rows affected (0.032 seconds) 0: jdbc:hive2://hadoop101:10000> create database if not exists db_hive2 location '/db_hive2.db'; 指定数据库在HDFS上存放的位置

② 修改数据库
hive (db_hive)> alter database db_hive set dbproperties('createtime'='20190215');
OK
Time taken: 0.031 seconds
③ 查看数据库| 切换数据库 use xx; hive (db_hive)> desc database extended db_hive; 显示数据库详细信息; 也可以去掉extended即显示数据库信息; OK db_name comment location owner_name owner_type parameters db_hive hdfs://hadoop101:9000/user/hive/warehouse/db_hive.db kris USER {createtime=20190215} Time taken: 0.016 seconds, Fetched: 1 row(s) ④ 删除数据库 hive (db_hive)> drop database db_hive2; hive (db_hive)> drop database if exists db_hive2; hive (db_hive)> drop database db_hive cascade; ##若数据库不为空,则强制删除用cascade;

创建表

hive (default)> create table if not exists student2(
              > id int, name string)
              > row format delimited fields terminated by '\t'
              > stored as textfile
              > location '/user/hive/warehouse/student2';
OK

管理表| 内部表

管理表,有时也被称为内部表。因为这种表,Hive会(或多或少地)控制着数据的生命周期。Hive默认情况下会将这些表的数据存储在由配置项hive.metastore.warehouse.dir(例如,/user/hive/warehouse)所定义的目录的子目录下。   当我们删除一个管理表时,Hive也会删除这个表中数据。管理表不适合和其他工具共享数据。

外部表,Hive并非认为其完全拥有这份数据。删除该表并不会删除掉这份数据,不过描述表的元数据信息会被删除掉。

使用场景:每天将收集到的网站日志定期流入HDFS文本文件。在外部表(原始日志表)的基础上做大量的统计分析,用到的中间表、结果表使用内部表存储,数据通过SELECT+INSERT进入内部表。

① 普通创建表
hive (default)> create table if not exists student3 as select id, name from student; hive (default)> create table if not exists student4 like student; //根据已经存在的表机构创建表 hive (default)> desc formatted student2; #查询表的类型;查看格式化数据 OK col_name data_type comment ② 外部表 hive (default)> dfs -mkdir /student; hive (default)> dfs -put /opt/module/datas/student.txt /student; hive (default)> create external table stu_external( //创建外部表 id int, name string) row format delimited fields terminated by '\t' location '/student';
0: jdbc:hive2://hadoop101:10000> select * from stu_external; 0: jdbc:hive2://hadoop101:10000> desc formatted stu_external; Table Type: | EXTERNAL_TABLE 0: jdbc:hive2://hadoop101:10000> drop table stu_external; 外部表删除后,hdfs中的数据还在,但是metadata中stu_external的元数据已被删除
③ 内部表和外部表的互相转换 desc formatted student2; Table Type: | MANAGED_TABLE 0: jdbc:hive2://hadoop101:10000> alter table student2 set tblproperties('EXTERNAL'='TRUE'); Table Type: | EXTERNAL_TABLE 0: jdbc:hive2://hadoop101:10000> alter table student2 set tblproperties('EXTERNAL'='FALSE');

分区表

分区表实际上就是对应一个HDFS文件系统上的独立的文件夹,该文件夹下是该分区所有的数据文件。Hive中的分区就是分目录,把一个大的数据集根据业务需要分割成小的数据集。在查询时通过WHERE子句中的表达式选择查询所需要的指定的分区,这样的查询效率会提高很多。

 

① 创建分区表
hive (default)> create table dept_partition( > deptno int, dname string, loc string) > partitioned by (month string) > row format delimited fields terminated by '\t'; OK
  加载数据 hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='201709'); Loading data to table default.dept_partition partition (month=201709) Partition default.dept_partition{month=201709} stats: [numFiles=1, numRows=0, totalSize=71, rawDataSize=0] OK load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='201708'); load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='201707'); ② 单分区查询 0: jdbc:hive2://hadoop101:10000> select * from dept_partition where month='201708'; +------------------------+-----------------------+---------------------+-----------------------+--+ | dept_partition.deptno | dept_partition.dname | dept_partition.loc | dept_partition.month | +------------------------+-----------------------+---------------------+-----------------------+--+ | 10 | ACCOUNTING | 1700 | 201708 | | 20 | RESEARCH | 1800 | 201708 | | 30 | SALES | 1900 | 201708 | | 40 | OPERATIONS | 1700 | 201708 | +------------------------+-----------------------+---------------------+-----------------------+--   多分区联合查询 0: jdbc:hive2://hadoop101:10000> select * from dept_partition where month='201707' 0: jdbc:hive2://hadoop101:10000> union 0: jdbc:hive2://hadoop101:10000> select * from dept_partition where month='201708' 0: jdbc:hive2://hadoop101:10000> union 0: jdbc:hive2://hadoop101:10000> select * from dept_partition where month='201709';

③ 增加分区| 增加单个、增加多个分区 0: jdbc:hive2://hadoop101:10000> alter table dept_partition add partition(month='201705') partition(month='201704');

④ 删除分区| 单个、删多个用,连接 0: jdbc:hive2://hadoop101:10000> alter table dept_partition drop partition(month='201705'), partition(month='201706');

⑤ 查看分区有多少分区 0: jdbc:hive2://hadoop101:10000> show partitions dept_partition; +---------------+--+ | partition | +---------------+--+ | month=201707 | | month=201708 | | month=201709 | +---------------+--+

⑥ 查看分区表结构 0: jdbc:hive2://hadoop101:10000> desc formatted dept_partition;

⑦ 创建二级分区

  hive (default)> create table dept_partition2(
    deptno int, dname string, loc string)
    partitioned by (month string, day string)
    row format delimited fields terminated by '\t';

  加载数据到二级分区
0: jdbc:hive2://hadoop101:10000> load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition2 partition(month='201709', day='13');
0: jdbc:hive2://hadoop101:10000> select * from dept_partition2 where month='201709' and day='13'; 查看分区数据

  把数据直接上传到分区目录上,让分区表和数据产生关联的三种方式

方式一:上传数据后修复
0: jdbc:hive2://hadoop101:10000> dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201709/day=12; 0: jdbc:hive2://hadoop101:10000> dfs -put /opt/module/datas/dept.txt /user/hive/warehouse/dept_partition2/month=201709/day=12; 0: jdbc:hive2://hadoop101:10000> msck repair table dept_partition2; //修复下才能查到数据 No rows affected (0.15 seconds) 0: jdbc:hive2://hadoop101:10000> select * from dept_partition2 where month='201709' and day='12'; alter table dept_partition2 drop partition(month='201709', day='11'); 删除 方式二:上传数据后添加分区 0: jdbc:hive2://hadoop101:10000> dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201709/day=11; 不能加引号 0: jdbc:hive2://hadoop101:10000> dfs -put /opt/module/datas/dept.txt /user/hive/warehouse/dept_partition2/month=201709/day=11; 0: jdbc:hive2://hadoop101:10000> alter table dept_partition2 add partition(month='201709', day='11'); 0: jdbc:hive2://hadoop101:10000> select * from dept_partition2 where month='201709' and day='11'; 方式三:创建文件夹后load数据到分区 0: jdbc:hive2://hadoop101:10000> dfs -mkdir -p /user/hive/warehouse/dept_partition2/month='201709'/day='10'; 0: jdbc:hive2://hadoop101:10000> load data local inpath '/opt/module/datas/dept.txt' into table dept_partition2 partition(month='201709',day='10'); 0: jdbc:hive2://hadoop101:10000> select * from dept_partition2 where month='201709' and day='10';

 

修改表

重命名表
 jdbc:hive2://hadoop101:10000> alter table teacher rename to new_teacher;
添加列 0: jdbc:hive2://hadoop101:10000> alter table dept_partition add columns(deptdesc string);
更新列 0: jdbc:hive2://hadoop101:10000> alter table dept_partition change column deptdesc desc int; No rows affected (0.112 seconds) 0: jdbc:hive2://hadoop101:10000> desc dept_partition;
替换列 0: jdbc:hive2://hadoop101:10000> alter table dept_partition replace columns(deptid int, name string, loc string);
删除表 0: jdbc:hive2://hadoop101:10000> drop table new_teacher;

 

DML数据操作

数据导入

向表中装载数据(Load)

① 向表中装载数据: 
0: jdbc:hive2://hadoop101:10000> create table student(id int, name string) row format delimited fields terminated by '\t'; 0: jdbc:hive2://hadoop101:10000> load data local inpath '/opt/module/datas/student.txt' into table default.student; 加载本地文件到hive 0: jdbc:hive2://hadoop101:10000> dfs -mkdir -p /user/kris/hive; 0: jdbc:hive2://hadoop101:10000> dfs -put /opt/module/datas/student.txt /user/kris/hive; 0: jdbc:hive2://hadoop101:10000> load data inpath '/user/kris/hive/student.txt' into table default.student; //移动hdfs上的文件;加载HDFS上的数据 0: jdbc:hive2://hadoop101:10000> load data inpath '/user/kris/hive/student.txt' overwrite into table default.student; 加载数据覆盖表中已有的数据

② 通过查询语句向表中插入数据Insert create table student(id int, name string) partitioned by (month string)   row format delimited fields terminated by '\t'; 创建一张分区表 0: jdbc:hive2://hadoop101:10000> insert into table student partition(month='201902') values (1, "kris"), (2, "egon"); 插入数据 -rwxrwxr-x kris supergroup 14 B 2019/2/15 下午7:16:26 3 128 MB 000000_0   根据单张表查询结果来插入insert into是追加数据的方式插入表或分区,原有数据不会被删除;

              insert overwrite是会覆盖表或分区中已有数据;
0: jdbc:hive2://hadoop101:10000> insert overwrite table student partition(month="201905") select id,name from student where month='201902'; 在原本基础上追加 0: jdbc:hive2://hadoop101:10000> select * from student; +-------------+---------------+----------------+--+ | student.id | student.name | student.month | +-------------+---------------+----------------+--+ | 1 | kris | 201902 | | 2 | egon | 201902 | | 1 | kris | 201905 | | 2 | egon | 201905 | +-------------+---------------+----------------+--+   多表查询结果插入 hive (default)> from student insert overwrite table student partition(month="201904") > select id, name where month="201905" > insert overwrite table student partition(month="201903") > select id, name where month="201905"; 0: jdbc:hive2://hadoop101:10000> select * from student; +-------------+---------------+----------------+--+ | student.id | student.name | student.month | +-------------+---------------+----------------+--+ | 1 | kris | 201902 | | 2 | egon | 201902 | | 1 | kris | 201903 | | 2 | egon | 201903 | | 1 | kris | 201904 | | 2 | egon | 201904 | | 1 | kris | 201905 | | 2 | egon | 201905 | +-------------+---------------+----------------+-
③ 查询语句中创建并加载数据 AS Select
create table if not exists student3 as select id, name from student;
create table if not exists student4 like student; 
④ 创建表时通过Location指定加载数据路径
0: jdbc:hive2://hadoop101:10000> create external table if not exists stu(id int, name string) row format delimited fields terminated by '\t' location '/student';
No rows affected (0.093 seconds)
0: jdbc:hive2://hadoop101:10000> select * from stu;
⑤ Import数据到指定Hive表中
create table student22(
id int, name string)
partitioned by (month string)
row format delimited fields terminated by '\t';

import table student22 partition(month='201904') from  //student22必须要有分区才能导入成功
 '/user/hive/warehouse/export/student';

 

数据导出

① Insert导出
将输出文件导出到本地/opt/module/datas/export/student中; 0: jdbc:hive2://hadoop101:10000> insert overwrite local directory '/opt/module/datas/export/student' select * from student;   结果格式化导出到本地
hive (default)> insert overwrite local directory '/opt/module/datas/export/student1' > ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' select * from student; 结果导出到HDFS hive (default)> insert overwrite directory '/user/kris/student2' > row format delimited fields terminated by '\t' > select * from student; ② Hadoop命令导出到本地 hive (default)> dfs -get /user/hive/warehouse/student/month=201902/000000_0 /opt/module/datas/export/student3.txt; [kris@hadoop101 export]$ cat student3.txt 1 kris 2 egon [kris@hadoop101 export]$ pwd /opt/module/datas/export ③ Shell命令导出 [kris@hadoop101 hive]$ bin/hive -e 'select * from default.student;' > /opt/module/datas/export/student4.txt ④ Export导出到HDFS上 hive (default)> export table default.student to '/user/hive/warehouse/export/student';

⑤ Sqoop导出

 

清除表中数据(Truncate)

注意:Truncate只能删除管理表,不能删除外部表中数据

  hive (default)> truncate table student;

 

原文地址:https://www.cnblogs.com/shengyang17/p/10386327.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


连接数据库的方式:第一种方式:ODBC:开放数据库连接是微软公司开放服务结构中有关数据库的一个组成部分,是数据库访问接口标准。ODBC是基于C语言实现的。提供了语言和数据库进行交互的一致性的接口,便于语言和数据库通信以及语言对数据库的各种操作。第二种方式:JDBC(本章重点)在Java中,
JDBCRequest 使用VariableNamesmysql:数据库连接池对象variousname:设置的变量名称 如何使用该变量a_#、b_#、c_#、d_#:代表行数a_1:第1行、第1列b_2:第2行、第2列
 1.JDBCDBC(JavaDataBaseConnectivity):Java数据库连接技术:具体讲就是通过Java连接广泛的数据库,并对表中数据执行增、删、改、查等操作的技术。JDBC是数据库与Java代码的桥梁。JDBC中定义了操作数据库的各种接口和类型:增删改基本操作:(1)获取连接:Connectionconnection=
1.需要jar包的支持:java.sqljavax.sqlmysql-conneter-java...连接驱动(必须要导入)<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.46</version></depend
1.简介Activiti是一个业务流程管理(BPM)框架,它是覆盖了业务流程管理,工作流,服务协作等领域的一个开源,灵活的,易扩展的可执行流程语言框架。在Java工作流引擎中可谓是主流,我们的项目也是使用的这个框架进行流程相关的开发。与流程息息相关的就是我们的流程定义BPMN文件,包含有一系列
1.JDBC体系系统一组规范:接口JDBC接口(API)包括两个层次:面向应用的API:JavaAPI,抽象接口,供应用开发人员使用(连接数据库,执行SQL语句,获得结果)面向数据库的API:JavaDriverAPI,供开发商开发数据库驱动程序JDBC是sun公司提供一套用于数据库操作的接口,java程序员只需要面向这套接
原文链接JDBC一般指Java数据库连接(JavaDatabaseConnectivity)api应用程序接口(API):可以调用或者使用类/接口/方法等去完成某个目标。API制定的类/方法可以做什么。API由开发人员调用。spi服务提供接口(SPI):需要继承或实现某些类/接口/方法等去完成某个目标。SPI告诉你
spring.shardingsphere.datasource.names=#省略数据源配置,请参考用法#标准分表配置spring.shardingsphere.rules.sharding.tables.<table-name>.actual-data-nodes=#描述数据源名称和实际表,分隔符为点,多个数据节点用逗号分隔,支持内联表达式。Absent表示仅对数据库进行分片
1问题Cannotloaddriverclass:com.mysql.cj.jdbc.Driver 2解决方案2.1已解决2.1.1首先,去查看项目中MySQL的版本如果找不到,说明可能还没有jdbc驱动,需要配置或者引入       (1)如果是直接引用的jar包,就去lib文件夹中查看,后缀是版
JDBC一、JDBC概述什么是JDBC?JDBC是使用Java语言操作关系型数据库的一套API。这套API是交由不同的数据库厂商实现的。我们利用JDBC编写操作数据库的代码,真正执行的是各个数据库的实现类(驱动)。全称:(JavaDataBaseConnectivity)Java数据库连接。JDBC的好处面向接口编
说明:/*需要引入依赖<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.18</version></dependency>*/代码:objec
1.概要在“配置元件”中添加“JDBCConnectionConfiguration”。配置如下图: 2.重点:配置人大金仓数据库连接下面着重介绍人大金仓数据库连接配置:DatabaseURL输入:jdbc:kingbase8://192.132.180.101:54321/hj_yc   (备注:应输入 jdbc:kingbase8://IP:
JDBC概念:JavaDataBaseconnectivityJava数据库连接,Java语言操作数据库JDBc本质∶其实是官方(sun公司)定义的一套操作所有关系型数据库的规则,即接口。各个数据库商去实现这套接口,提供数据库驱动jar包。我们可以使用这套接口(JDBC)编程,真正执行的代码是驱动jar包中的实现类
Mybatis核心配置文件习惯上命名mybatis-config.xml,整合Spring之后,整个配置文件可以省略核心配置文件主要用于配置连接数据库的环境以及MyBatis的全局配置信息标签顺序顺序出错会报错propertiessettingstypeAliasestypeHandlersobjectFactoryobjectWrapperFa
JDBC1.概念:JavaDataBaseConnectivityJava数据库连接,Java语言操作数据库JDBC本质:其实是官方(sun公司)定义的一套操作所有关系型数据库的规则,即接口。各个数据库厂商去实现这套接口,提供数据库驱动jar包。我们可以使用这套接口(JDBC)编程,真正执行的代码是驱动jar包中的实现类。
  1.出现这个问题的原因 :在安装mysql的时候时区设置的不正确,mysql默认的是美国的时区,而我们中国大陆要比他们迟8小时,采用+8:00格式使用的数据库是MySQL,没有指定MySQL驱动版本的情况下它自动依赖的驱动是8.0.12很高的版本,这是由于数据库和系统时区差异所造成的,在jdbc连
一、环境准备1.数据库创建2个库2个表:xdclass_shop_order_0product_order_0product_order_1ad_configproduct_order_item_0product_order_item_1xdclass_shop_order_1product_order_0product_order_1ad_configproduct_order_item_0product_order_item_1数据
编写配置文件(application.yml)spring:datasource:username:rootpassword:123456url:jdbc:mysql://localhost:3306/mybatis?useUnicode&characterEncoding=utf-8driver-class-name:com.mysql.cj.jdbc.Driver测试连接@SpringBootTestclassSprin
结构图pom.xml<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http:/
MySql链接url参数详解 jdbc:mysql://[host:port],[host:port].../[database][?参数名1][=参数值1][&参数名2][=参数值2]... 常用的几个较为重要的参数: 参数名称参数说明缺省值最低版本要求 user 数据库用户名(用于连接数据库) 所有版本passWord用户密码(用于连接