hive语法中主要提供了以下复合数据类型:

1)Structs: structs内部的数据可以通过DOT(.)来存取。例如,表中一列c的类型为STRUCT{a INT; b INT},我们可以通过c.a来访问域a。

2)Map(K-V对):访问指定域可以通过["指定域名称"]进行。例如,一个Map中M包含了一个group->gid的K-V对,gid的值可以通过M['group']来获取。

3)Array:array中的数据为相同类型。例如,假如array A中元素['a','b','c'],则A[1]的值为'b'

1、Struct使用

1) 建表

hive> create table student_test(id int, info struct<name:string, age:int>) row format delimited fields terminated by ',' collection items terminated by ':';
OK
Time taken: 0.386 seconds

 hive> show tables;

 OK

 student_test

 Time taken: 0.046 seconds, Fetched: 1 row(s)

'FIELDS TERMINATED BY' :字段与字段之间的分隔符。'COLLECTION ITEMS TERMINATED BY' :一个字段各个item的分隔符。

2) 导入数据

[hadoop@master ~]$ cd /home/hadoop/test

[hadoop@master test]$ sudo vim test1.txt

1,zhou:30

2,yan:30

3,chen:20

4,li:80

[hadoop@master test]$ ll

total 12

-rw-r--r-- 1 root root  43 May 24 03:40 test1.txt

-rw-r--r-- 1 root root 106 May 18 04:41 test.txt

-rw-r--r-- 1 root root 421 May 18 04:03 user.txt

hive> load data local inpath '/home/hadoop/test/test1.txt' into table student_test;

Loading data to table hive.student_test

Table hive.student_test stats: [numFiles=1, totalSize=37]

OK

Time taken: 0.363 seconds

3) 查询数据

hive> select info.age from student_test;
OK
30
30
20
80
Time taken: 0.078 seconds, Fetched: row(s)

2、Array使用

1) 建表

hive> create table class_test(name string, stu_id_list array<int>) row format delimited fields terminated by ',' collection items terminated by ':';
OK
Time taken: 0.079 seconds

2) 导入数据

[hadoop@master test]$ sudo vim test2.txt
034,1:2:3:4
035,5:6
036,7:8:9:10
hive> load data local inpath '/home/hadoop/test/test2.txt' into table class_test;
Loading data to table hive.class_test
Table hive.class_test stats: [numFiles=, totalSize=]
OK
Time taken: 0.299 seconds

3) 查询

hive> select stu_id_list[3] from class_test;
OK
4
NULL
10
Time taken: 0.048 seconds, Fetched: row(s)

3、Map使用

1) 建表

hive> create table employee(id string, perf map<string, int>) row format delimited fields terminated by '\t' collection items terminated by ',' map keys terminated by ':';
OK
Time taken: 0.082 seconds

‘MAP KEYS TERMINATED BY’ :key value分隔符

2) 导入数据

[hadoop@master test]$ sudo vim test3.txt
1 job:80,team:60,person:70
2 job:60,team:80
3 job:90,team:70,person:100
hive> load data local inpath '/home/hadoop/test/test3.txt' into table employee;
Loading data to table default.employee
Table default.employee stats: [numFiles=, totalSize=]
OK
Time taken: 0.643 seconds

3) 查询

hive> select perf['person'] from employee;
OK
70
NULL
100
Time taken: 0.473 seconds, Fetched: row(s)

以上就是博主为大家介绍的这一板块的主要内容,这都是博主自己的学习过程,希望能给大家带来一定的指导作用,有用的还望大家点个支持,如果对你没用也望包涵,有错误烦请指出。如有期待可关注博主以第一时间获取更新哦,谢谢! 

版权声明:本文为博主原创文章,未经博主允许不得转载。

最新文章

  1. 10天学会phpWeChat——第七天:创建一个自适应PC网站+H5移动端的模块
  2. CentOS 6.5安装 ASM lib
  3. 探求网页同步提交、ajax和comet不为人知的秘密(中篇)
  4. -bash: sudo: command not found Error and Solution
  5. Oracle 管道化表函数
  6. JavaScript知识架构学习路径(一)- 变量篇
  7. QT 按钮类继承处理带定时器
  8. Android EditText截获与监听输入事件
  9. AngularJS如何给动态添加的DOM中绑定事件
  10. php正则表达式的基本语法
  11. android开发技巧
  12. Compilation err ororg.eclipse.jdt.internal.compiler.classfmt.ClassFormatException
  13. 1583: [Usaco2009 Mar]Moon Mooing 哞哞叫
  14. [译]在Asp.Net Core 中使用外部登陆(google、微博...)
  15. [Luogu3121][USACO15FEB]审查Censoring
  16. js 提取字符串中所有的英文
  17. STL的内存管理
  18. C#路径的八种相关操作,判断字符串是否为路径等
  19. WDA基础一:激活相关服务
  20. Activity的四大启动模式

热门文章

  1. windows服务和进程的区别和联系
  2. oracle--pl/sql变量定义----
  3. Ajax前端调后台方法
  4. k8s组件简介
  5. Math类简介
  6. SQL标量值函数:返回汉字拼音首拼
  7. Ubuntu 解决:当执行`sudo apt-get update`命令时 出现的 “apt-get 404 Not Found Package Repository Errors” 问题
  8. R-安装、卸载、查看及卸除加载包
  9. p4180 次小生成树
  10. Delphi Runtime Library在哪里?