Composite types of Hive

Source: Internet
Author: User

hive Array, map, struct use

Hive provides a composite data type:
Structs:structs internal data can be accessed via dot (.), for example, a column C in a table is of type struct{a int; b int}, we can access domain A by C.A
Maps (k-v pair): Access to the specified domain can be done through ["Specified domain name"], for example, a map m contains a group-"gid of the kv pair, the GID value can be obtained by m[' group ')
The data in Arrays:array is the same type, for example, if the element in array a [' A ', ' B ', ' C '], then the value of a[1] is ' B '

Struct Use

Build table:

· Hive>create table student_test (id INT, infostruct<name:string, age:int>)

· > ROW FORMAT delimited fields Terminatedby ', '

· > COLLECTION ITEMS TERMINATED by ': ';

· Ok

· timetaken:0.446 seconds

' Field TERMINATED by ': Separator between fields and fields
' COLLECTION ITEMS TERMINATED by ': delimiter for each item in a field
Import data:

· $ cattest5.txt

· 1,zhou:30

· 2,yan:30

· 3,chen:20

· 4,li:80

· Hive>load DATA LOCAL inpath '/home/work/data/test5.txt ' into tablestudent_test;

· Copyingdata from File:/home/work/data/test5.txt

· Copyingfile:file:/home/work/data/test5.txt

· Loadingdata to Table Default.student_test

· Ok

· timetaken:0.35 seconds

Inquire:

· Hive>select info.age from Student_test;

· Totalmapreduce jobs = 1

· ......

· Totalmapreduce CPU Time spent:490 msec

· Ok

· 30

· 30

· 20

· 80

· timetaken:21.677 seconds


Array Use
Build table:

· Hive>create table Class_test (name string, Student_id_list array<int>)

· > ROW FORMAT Delimited

· > TERMINATED by ', '

· > COLLECTION ITEMS TERMINATED by ': ';

· Ok

· timetaken:0.099 seconds

Import data:

· $ cattest6.txt

· 034,1:2:3:4

· 035,5:6

· 036,7:8:9:10

· hive> LOAD DATA LOCAL inpath '/home/work/data/test6.txt ' into TABLE class_test;

· Copyingdata from File:/home/work/data/test6.txt

· Copyingfile:file:/home/work/data/test6.txt

· Loadingdata to Table Default.class_test

· Ok

· timetaken:0.198 seconds

Inquire:

· Hive>select student_id_list[3] from Class_test;

· Totalmapreduce jobs = 1

· ......

· Totalmapreduce CPU Time spent:480 msec

· Ok

· 4

· Null

· 10

· timetaken:21.574 seconds


Map Use
Build table:

· Hive>create table employee (ID string, perfmap<string, int>)

· > ROW FORMAT Delimited

· > Fields TERMINATED by ' \ t '

· > COLLECTION ITEMS TERMINATED by ', '

· > MAP KEYS TERMINATED by ': ';

· Ok

· timetaken:0.144 seconds

' Mapkeys TERMINATED by ': KeyValue delimiter

Import data:

· $ cattest7.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/work/data/test7.txt ' into TABLE employee;

Inquire:

· Hive>select perf[' person ') from employee;

· Totalmapreduce jobs = 1

· ......

· Totalmapreduce CPU Time spent:460 msec

· Ok

· 70

· Null

· 100

· timetaken:20.902 seconds

· Hive>select perf[' person ') from the employee where perf[' person ' was not null;

· Totalmapreduce jobs = 1

· .......

· Totalmapreduce CPU Time spent:610 msec

· Ok

· 70

· 100

· timetaken:21.989 seconds

· Hive>

· <spanstyle= "Font-family:arial, Helvetica, Sans-serif;" ><spanstyle= "White-space:normal;" >

· </span></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.