【问题标题】:How to create a table in Hive with a column of data type array<map<string, string>>如何在 Hive 中使用数据类型 array<map<string, string>> 的列创建表
【发布时间】:2015-06-10 00:07:01
【问题描述】:

我正在尝试创建一个具有复杂数据类型的表。下面列出了数据类型。

  1. 数组

  2. 地图

  3. 数组 >

我正在尝试创建 3 类型的数据结构。是否可以在 Hive 中创建?我的表 DDL 如下所示。

create table complexTest(names array<String>,infoMap map<String,String>, deatils array<map<String,String>>)           
row format delimited                                                                                       
fields terminated by '/'                                                                                   
collection items terminated by '|'                                                                         
map keys terminated by '='                                                                                 
lines terminated by '\n';

我的示例数据如下所示。

Abhieet|Test|Complex/Name=abhi|age=31|Sex=male/Name=Test,age=30,Sex=male|Name=Complex,age=30,Sex=female

无论我从表中查询数据时,我都会得到以下值

["Abhieet"," Test"," Complex"]  {"Name":"abhi","age":"31","Sex":"male"} [{"Name":null,"Test,age":null,"31,Sex":null,"male":null},{"Name":null,"Complex,age":null,"30,Sex":null,"female":null}]

这不是我所期待的。如果数据类型 array&lt; map &lt; String,String&gt;&gt; 可能的话,您能否帮我找出 DDL 应该是什么?

【问题讨论】:

    标签: arrays dictionary hive


    【解决方案1】:

    我认为使用内置的 serde 是不可能的。如果您事先知道地图中的值将是什么,那么我认为解决此问题的更好方法是将您的输入数据转换为 JSON,然后使用the Hive json serde

    样本数据:

    {'Name': ['Abhieet', 'Test', 'Complex'],
    'infoMap': {'Sex': 'male', 'Name': 'abhi', 'age': '31'},
     'details': [{'Sex': 'male', 'Name': 'Test', 'age': '30'}, {'Sex': 'female', 'Name': 'Complex', 'age': '30'}]
     }
    

    表定义代码:

    create table complexTest
    (
    names array<string>,
    infomap struct<Name:string,
                   age:string,
                   Sex:string>,
    details array<struct<Name:string,
                   age:string,
                   Sex:string>>
    )
    row format serde 'org.openx.data.jsonserde.JsonSerDe'
    

    【讨论】:

      【解决方案2】:

      这可以通过使用以下查询的结构数组来处理。

      create table complexStructArray(custID String,nameValuePairs array<struct< key:String, value:String>>) row format delimited fields terminated by '/' collection items terminated by '|' map keys terminated by '=' lines terminated by '\n';
      

      样本数据:

      101/Name=Madhavan|年龄=30

      102/姓名=拉姆库马尔|年龄=31

      虽然与 Map 不同,struct 允许重复键值,但上面的查询应该处理询问数据是否具有唯一键值。

      选择查询将给出如下输出。

      hive> 从 complexStructArray 中选择 *;

      101 [{"key":"Name","value":"Madhavan"},{"key":"age","value":"30"}]

      102 [{"key":"Name","value":"Ramkumar"},{"key":"age","value":"31"}]

      【讨论】:

        【解决方案3】:

        样本数据:{"Name": ["Abhieet", "Test", "Complex"],"infoMap": {"Sex":"male", "Name":"abhi", "age": 31},"details": [{"Sex":"male", "Name":"Test", "age":30}, {"Sex":"female", "Name":"Complex", "年龄":30}]}

        表定义代码:

        #hive>
        create table complexTest
        (names array<string>,infomap struct<Name:string,
                       age:string,
                       Sex:string>,details array<struct<Name:string,
                       age:string,
                       Sex:string>>)
        row format serde 'org.apache.hive.hcatalog.data.JsonSerDe'

        【讨论】:

          猜你喜欢
          • 2017-10-11
          • 1970-01-01
          • 2021-09-04
          • 1970-01-01
          • 2021-04-02
          • 1970-01-01
          • 2019-11-23
          • 2017-01-29
          • 1970-01-01
          相关资源
          最近更新 更多