Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
995 views
in Technique[技术] by (71.8m points)

hadoop - HIVE nested ARRAY in MAP data type

I have HIVE table structured like this:-

Create table test_stg(employee_id INT, name STRING, abu ARRAY <String>, sabu MAP <String, ARRAY<INT>)
row format delimited fields terminated by '|'                                                              
collection items terminated by '/'                                                                         
map keys terminated by ':'; 

I will import the data from local file system using LOAD DATA LOCAL....

The question is how should I frame the contents of my local file so that Map datatype field sabu can have nested array.

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Hive's default delimiters are:

  • Row Delimiter => Control-A ('01')
  • Collection Item Delimiter => Control-B ('02')
  • Map Key Delimiter => Control-C ('03')

If you override these delimiters then overridden delimiters are used during parsing. The preceding description of delimiters is correct for the usual case of flat data structures, where the complex types only contain primitive types. For nested types the level of the nesting determines the delimiter.

For an array of arrays, for example, the delimiters for the outer array are Control-B ('02') characters, as expected, but for the inner array they are Control-C ('03') characters, the next delimiter in the list.

Hive actually supports eight levels of delimiters, corresponding to ASCII codes 1, 2, ... 8, but you can only override the first three.

For your case delimiter for items in nested Array of Map datatype field sabu will be '04' as Map Key Delimiter is '03' (Overridden as ':').

So you can write your input file as following format:

1|JOHN|abu1/abu2|key1:1'04'2'04'3/key2:6'04'7'04'8

Output of SELECT * FROM test_stg; will be:

1       JOHN     ["abu1","abu2"]     {"key1":[1,2,3],"key2":[6,7,8]}

Reference: Hadoop The Definitive Guide - Chapter 12: Hive, Page No: 433, 434


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...