本篇来学习一下Hive中的数据类型,以及如何在Hive SQL中去处理这些数据类型。...* from datatype_test1; 结果如下: 2、字符串类型 Hive中的字符串类型包含如下三种: 数据类型 长度 示例 STRING – ‘abc’ VARCHAR 1-65535 ‘...接下来通过例子进行说明: 创建表: create table if not exists datatype_test2(id int,str_col string,varchar_col varchar...datatype_test3; 结果如下: 4、集合类型 Hive中集合类型主要包含三种:STRUCT、ARRAY、MAP。...string,weight:double>, score arrayInt>, info_map mapstring,string>) row format delimited
use default; (4)显示default数据库中的表 hive> show tables; (5)创建一张表 hive> create table student(id int, name...string); (6)显示数据库中有几张表 hive> show tables; (7)查看表的结构 hive> desc student; (8)向表中插入数据 hive> insert into...ARRAY和MAP与Java中的Array和Map类似,而STRUCT与C语言中的Struct类似,它封装了一个命名字段集合,复杂数据类型允许任意层次的嵌套。...create table test( name string, friends arraystring>, children mapstring, int>, address struct<street..., friends arraystring>, children mapstring, int>, address structstring, city:string> ) row
一种类型的值的组合 arraystring...> uniontype: (string,map,struct,array) 注意:在创建hive表时可根据需要导入的数据进行类型识别并创建适合的数据类型...item之间的分隔符[可用于array和struct类型] MAP KEYS TERMINATED BY 表示map类型中的key/value的分隔符[可用于map类型] # 创建表 create table...union_testnew( foo uniontypeint, double, string, arraystring>, mapstring, string>> ) row format...类型的使用 1.1 array类型的基本使用 类型结构: array 例如:arraystring>,arrayint> 数据表示: 例如:[string1,string2],[int1...结构的表结构创建 hive> create table employee(id string, perf mapstring, int>) > ROW FORMAT DELIMITED
-- (Note: Available in Hive 0.13.0 and later) array_type : ARRAY map_type : MAP...是 如下:根据上述文件内容,创建一个表t1 create table t1( id int ,name string ,hobby arraystring> ,add map...,则需要修复(MSCK REPAIR TABLE table_name;) 创建一个外部表t2 create external table t2( id int ,name string...,hobby arraystring> ,add mapString,string> ) row format delimited fields terminated by ',' collection...external table t2( id int ,name string ,hobby arraystring> ,add mapString,string> )
create table test( name string, friends arraystring>, children mapstring, int>, address struct<...| | children mapstring,int>, | | address structstring...,friends arraystring>, children mapstring,int>,address structstring,city:string>,age varchar...表名; 查看表的格式:desc formatted 表名; 查看表的分区(有分区才能查看):show partitions 表名; 在shell中将数据文件先上传到hdfs上的目录下,如:hdfs...dfs -put 文件名 /xxxx 加载数据到表中:load data inpath '/hdfs目录名/文件名全称' into table 表名; (执行命令后,原数据文件被移动到了hive的目录下面
Java来向Hive插入,查询数据。...query,MapString, String> condition) throws SQLException { String where = " where "; int...; } catch (SQLException e) { e.printStackTrace(); } } //将文件中的数据加载到表中...String like = "array('basketball', 'music', 'dance')"; String map = "map('address','xxxx')"; String...,newAddValue,like,map); System.out.println("插入数据后全表查询:"); hive.queryAndPrint(query); System.out.println
# 举例: ## 含有数据的表 (通过上面第一种方式插入数据) create table tb_emp ( id int, name string, likes arraystring>,...( id int, name string, likes arraystring>, -- 爱好采用数组类型 address mapstring,string> -- 地址采取map类型 ) row...举例 create table tb_emp4 ( id int, name string, likes arraystring>, address mapstring,string> ) partitioned...int, name string, age int, sex string, likes arraystring>, -- 爱好采用数组类型 address mapstring,string> -...create table emp2 ( id int, name string, likes arraystring>, -- 爱好采用数组类型 address mapstring,string>
使用explode函数将hive表中的Map和Array字段数据进行拆分 使用explode拆分json字符串 配合LATERAL VIEW使用 行转列 列转行 reflect函数 Hive 窗口函数...drop table score5; 清空表:truncate table score6; 向hive表中加载数据 直接向分区表中插入数据: insert into table score partition...清空的表是不进回收站的,所以无法恢复truncate清空的表 所以 truncate 一定慎用,一旦清空将无力回天 向hive表中加载数据 直接向分区表中插入数据 insert into table...A[n] 操作类型: A为array类型,n为int类型 说明:返回数组A中的第n个变量值。...一列中复杂的array或者map结构拆分成多行。
,但是可以通过查询创建新表来将结果导入到文件中;(hive 2.3.2 版本支持记录级别的插入操作) Hive 延迟较高,不适用于实时分析; Hive 不支持事物,因为没有增删改,所以主要用来做 OLAP...opt/module/datas/hivef.sql 2.2 本地文件导入 Hive 表中 首先需要创建一张表: create table student( id int, name string...ARRAY 和 MAP 与 Java 中的 Array 和 Map 类似,而 STRUCT 与 C 语言中的 Struct 类似,它封装了一个命名字段集合,复杂数据类型允许任意层次的嵌套。...Hive 上创建测试表 test: create table test( name string, friends arraystring>, children mapstring...隐式类型转换规则如下 任何整数类型都可以隐式地转换为一个范围更广的类型,如 TINYINT 可以转换 成 INT,INT 可以转换成 BIGINT; 所有整数类型、FLOAT 和 STRING 类型都可以隐式地转换成
中struct,map,array数据类型 所有的数据类型都是Java接口的实现,所有所有的具体行为细节和实现与对应的java是一致的。...集合类型主要包括:array,map,struct等,hive的特性支持集合类型,这特性是关系型数据库所不支持的,利用好集合类型可以有效提升SQL的查询速率。...create table t_person( id int, name string, likes arraystring> ) row format delimited fields terminated...(reduced端执行) set hive.optimize.skewjoin=true; mapjoin(map side join) 在map端把小表加载到内存中,然后读取大表,和内存中的小表完成连接操作...如:抽取原hive表中10%的数据 (注意:测试过程中发现,select语句不能带where条件且不支持子查询,可通过新建中间表或使用随机抽样解决) 也可以 tablesample(n M) 指定抽样数据的大小
Hive 有三种复杂数据类型 ARRAY、MAP 和 STRUCT。...ARRAY 和 MAP 与 Java 中的Array 和 Map 类似,而 STRUCT 与 C 语言中的 Struct 类似,它封装了一个命名字段集合,复杂数据类型允许任意层次的嵌套。...guan", "city": "beijing" } } 2)基于上述数据结构,我们在 Hive 里创建对应的表,并导入数据。...3)Hive 上创建测试表 test create table test( name string, friends arraystring>, children mapstring, int...隐式类型转换规则如下 (1)任何整数类型都可以隐式地转换为一个范围更广的类型,如 TINYINT 可以转换 成 INT,INT 可以转换成 BIGINT。
terminated by ',' select * from t_access; 2)将hive表中的数据导入本地磁盘文件 insert overwrite local... create table t_user2 like t_user; 2)在建表的时候插入数据 create table t_access2 as ...t_person(id int,name string,femily_members mapstring,string>,age int) row format delimited fields...) substr(string, int start, int len) substring(string, int start, int len) concat...6.4 map_keys(Map)和map_values(Map) 7).表生成函数 7.1 行转列函数 explode() select distinct
复杂数据类型包括数组(ARRAY)、映射(MAP)和结构体(STRUCT),具体如下表所示: 复杂数据类型 类型 描述 示例 ARRAY 一组有序字段。...字段的类型必须相同 Array(1,2) MAP 一组无序的键/值对。...字段类型可以不同 Struct(‘a’,1,1,0) 下面我们看看hive使用复杂数据类型的实例,建表: Create table complex(col1 ARRAYINT>, Col2 MAP表(table):hive的表逻辑上由存储的数据和描述表格中的数据形式的相关元数据组成。...hive>create table student(id INT, age INT, name STRING) >partitioned by(stat_date STRING)
psn1 :演示如何将File文件中的数据加载到Hive表中,要符合data_type 用到的 row format CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT...[constraint_specification])] CREATE TABLE IF NOT EXISTS psn1 ( id int, name string, hobby ARRAY...string, hobby ARRAY string >, address MAP string, string > )ROW FORMAT DELIMITED FIELDS TERMINATED...string, hobby ARRAY string >, address MAP string, string > )ROW FORMAT DELIMITED FIELDS TERMINATED...CTAS 创建表结构,用MapReduce将数据转到hive下面来管理,数据格式,HIve的分隔符 DDL 总结 Hive 数据结构 基础数据结构 和Sql很像 高级数据结构 array 和map C
mapstring,int>,> class arraystring>); 图1-20 Hive创建表 语法:create [external] table [if not exists] table_name...hive> create table teacher(> num int,> name string,> email mapstring,int>,> class arraystring>) > partitioned...hive> create table teacher(> num int,> name string,> email mapstring,int>,> class arraystring>) > clustered...student(> num int,> name string,> class string,> body mapstring,int>,> exam arraystring>)> row format...',60),array('80','70');图1-26 插入单条信息步骤二:批量导入在插入数据过程中调用MR操作,效率低下,既Hive的数据通常为Load批量导入。
by(day string) row format delimited fields terminated by ','; 将来把数据向表中insert的时候,就需要指定一个day了,如day...(plus:这个子目录day=2017-09-16并不是在建表时候就有的,而是在插入/导入数据时候才在HDFS中生成该目录的) 注意:分区字段不能是表定义中的已存在字段,否则会冲突,实际上分区字段是伪字段...string,access_time string) stored as sequencefile; 从文本表中查询数据插入sequencefile表中,生成数据文件就是sequencefile格式的了...类型来对上述数据中key-value的家庭成员进行描述 2、建表语句: create table t_person(id int,name string,family_members mapstring...create table t_stu_subject(id int,name string,subjects arraystring>) row format delimited fields terminated
drop table score5; 清空表:truncate table score6; 向hive表中加载数据 直接向分区表中插入数据:insert into table score partition...清空的表是不进回收站的,所以无法恢复truncate清空的表 所以 truncate 一定慎用,一旦清空将无力回天 向hive表中加载数据 直接向分区表中插入数据 insert into table...表中的Map和Array字段数据进行拆分 lateral view用于和split、explode等UDTF一起使用的,能将一行数据拆分成多行数据,在此基础上可以对拆分的数据进行聚合,lateral...表,然后使用explode拆分map和array hive (hive_explode)> create table t3(name string,children arraystring>,address...一列中复杂的array或者map结构拆分成多行。
写了一个建表语句:“这是一个内部表” create table t1( id int ,name string ,hobby arraystring>...int ,name string ,hobby arraystring> ,add mapString,string> ) row format delimited...分桶表的实质,就是对分桶的字段做了hash 然后存放到对应文件中,也就是说向分桶表中插入数据的时候必然要执行一次MAPREDUCE,所以分桶表的数据只能通过从结果集查询插入的方式进行导入。...-- 创建一个临时表 create table student( id int, name string, sex string, age int, department string...目标表中 set hive.enforce.bucketing=true; set mapreduce.job.reduces=2; -- 插入 insert into table bck_student
使用全局临时表时需要全路径访问,如:global_temp.people5....= [name: string, age: int] peopleRDD.map(x=>{People(x._1,x._2)}).toDF 3....DataFrame: testDF.map{ case Row(col1:String,col2:int)=>{ println(col1) println(col2) col1 }...工作中要跟外部Hive关联的。...object Practice { // 将DataFrame插入到Hive表 private def insertHive(spark: SparkSession, tableName:
清空的表是不进回收站的,所以无法恢复truncate清空的表 所以 truncate 一定慎用,一旦清空将无力回天 向hive表中加载数据 直接向分区表中插入数据 insert into table...类型访问: A[n] 语法: A[n] 操作类型: A为array类型,n为int类型 说明:返回数组A中的第n个变量值。...hive表中的Map和Array字段数据进行拆分 lateral view用于和split、explode等UDTF一起使用的,能将一行数据拆分成多行数据,在此基础上可以对拆分的数据进行聚合,lateral...表,然后使用explode拆分map和array hive (hive_explode)> create table t3(name string,children arraystring>,address...一列中复杂的array或者map结构拆分成多行。
领取专属 10元无门槛券
手把手带您无忧上云