如何在Hive中创建一个数据类型为array< map< string,string>>的表

lnlaulya  于 2023-10-18  发布在  Hive
关注(0)|答案(4)|浏览(315)

我试图创建一个具有复杂数据类型的表。下面列出了数据类型。
1.阵列
1.Map

  1. array< map < String,String> >
    我正在尝试创建一个3类型的数据结构。有没有可能在Hive中创建?我的table看起来像下面。
  1. create table complexTest(names array<String>,infoMap map<String,String>, deatils array<map<String,String>>)
  2. row format delimited
  3. fields terminated by '/'
  4. collection items terminated by '|'
  5. map keys terminated by '='
  6. lines terminated by '\n';

我的示例数据如下所示。

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

无论我在哪里查询表中的数据,我都会得到以下值

  1. ["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< map < String,String>>可能的话,你能帮我找出应该是什么吗?

ego6inou

ego6inou1#

我不认为这是可能的使用内置serde。如果你事先知道你的map中的值是什么,那么我认为一个更好的方法是将你的输入数据转换为JSON,然后使用the Hive json serde
样本数据:

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

表定义代码:

  1. create table complexTest
  2. (
  3. names array<string>,
  4. infomap struct<Name:string,
  5. age:string,
  6. Sex:string>,
  7. details array<struct<Name:string,
  8. age:string,
  9. Sex:string>>
  10. )
  11. row format serde 'org.openx.data.jsonserde.JsonSerDe'
展开查看全部
brqmpdu1

brqmpdu12#

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

  1. 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= hangzhou|年龄=30
Name= yuebuo|年龄=31
虽然struct允许与Map不同的重复键值,但上面的查询应该处理数据是否具有唯一键值的询问。
选择查询将给出如下输出:给予。
hive> select * from complexStructArray;
101 [{“key”:“Name”,“value”:“Madhavan”},{“key”:“age”,“value”:“30”}]
102 [{“key”:“Name”,“value”:“Ramkumar”},{“key”:“age”,“value”:“31”}]

mzillmmw

mzillmmw3#

样本数据:{“Name”:[“Abhieet”,“Test”,“Complex”],“infoMap”:{“性别”:“男性”,“姓名”:“abhi”,“年龄”:31},“详细信息”:【{“性别”:“男性”,“姓名”:“测试”,“年龄”:30},{“性别”:“女性”,“姓名”:“复杂”,“年龄”:30}】}
表定义代码:

  1. #hive>
  2. create table complexTest
  3. (names array<string>,infomap struct<Name:string,
  4. age:string,
  5. Sex:string>,details array<struct<Name:string,
  6. age:string,
  7. Sex:string>>)
  8. row format serde 'org.apache.hive.hcatalog.data.JsonSerDe'
e5njpo68

e5njpo684#

Hive表可以创建与Parquet作为文件格式。
示例架构:

  1. create table complexTest(
  2. names array<String>,
  3. infoMap struct<sex:string, name:string, age:string>,
  4. deatils array<struct<sex:string, name:string, age:string>>
  5. )
  6. stored as parquet

相关问题