sql - 配置单元:无法从配置单元表中的文件插入数组和映射

标签 sql hadoop hive hql

这是我的表的架构

CREATE DATABASE IF NOT EXISTS mydb;
USE mydb;

CREATE TABLE IF NOT EXISTS mytab (

idcol   string,
arrcol  array<string>,
mapcol  map<string,string>
)
PARTITIONED BY (data_date string)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '|'
STORED AS TEXTFILE;

现在我只想在此表中插入一行。我在 psv 文件中有那一行

123|["a","b"]|{"1":"a","2":"b"}

这是我尝试加载数据的方式

使用 mydb; LOAD DATA INPATH '/path/to/file' INTO TABLE mytab PARTITION (data_date='2019-02-02');

查询成功,但是当我看到结果时

hive -e "use mydb; select * from mytab where data_date='2019-02-02';"

我明白了

hive> select * from mytab;
OK
123 ["[\"a\",\"b\"]"]   {"{\"1\":\"a\",\"2\":\"b\"}":null}  2019-02-02
Time taken: 2.39 seconds, Fetched: 1 row(s)

看起来 LOAD 对数据做了一些转换。它使字符串值保持良好,但数组和映射存在一些问题。

如何正确插入数组和映射?

我还尝试了以下作为输入

123|array("a","b")|{"1":"a","2":"b"}

加载成功,但是查询数据的时候,得到了

root@0d2b0044b4c1:/opt# hive -e "use mydb;select * from mytab;"
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/hive/lib/log4j-slf4j-impl-2.6.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/hadoop/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]

Logging initialized using configuration in jar:file:/opt/hive/lib/hive-common-2.3.2.jar!/hive-log4j2.properties Async: true
OK
Time taken: 6.096 seconds
OK
123 ["array(\"a\",\"b\")"]  {"{\"1\":\"a\",\"2\":\"b\"}":null}  1554090675
Time taken: 3.266 seconds, Fetched: 1 row(s)

更新

非常感谢@pedram bashiri 的回答。我创建了外部表并能够填充它。然而,一切都被填充为字符串

hive> drop table if exists extab;
OK
Time taken: 0.01 seconds
hive> create external table extab(idcol string,arrcol array<string>,mapcol map<string,string>, data_date string)
    >   row format serde 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
    > with serdeproperties (
    >   "separatorChar" = "|",
    >   "quoteChar"     = "\"",
    >   "escapeChar"    = "\\"
    >   )
    >   stored as textfile
    > location '/tmp/serdes/';
OK
Time taken: 0.078 seconds
hive> desc extab;
OK
idcol                   string                  from deserializer
arrcol                  string                  from deserializer
mapcol                  string                  from deserializer
data_date               string                  from deserializer
Time taken: 0.059 seconds, Fetched: 4 row(s)
hive> select * from extab;
OK
123 ["a","b"]   {"1":"a","2":"b"}   2019
Time taken: 0.153 seconds, Fetched: 1 row(s)
hive>

这是存储在hdfs中的内容

root@0d2b0044b4c1:/opt# hadoop fs -ls -R /tmp/serdes/
-rw-r--r--   1 root root         37 2019-04-04 22:06 /tmp/serdes/x.psv
root@0d2b0044b4c1:/opt# hadoop fs -cat /tmp/serdes/x.psv
123|["a","b"]|{"1":"a","2":"b"}|2019
root@0d2b0044b4c1:/opt#

我也试过

create external table extab(idcol string,arrcol array<string>,mapcol map<string,string>, data_date string)
  row format serde 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
with serdeproperties (
  "separatorChar" = "|"
  )
  stored as textfile
location '/tmp/serdes/';

但是,所有内容都存储为字符串,所以现在当我尝试插入时,我得到类型不匹配。

最佳答案

经过大量的挖掘,我想通了

CREATE TABLE IF NOT EXISTS mytab (

idcol   string,
arrcol  array<string>,
mapcol  map<string,string>
)
PARTITIONED BY (data_date string)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '|'
COLLECTION ITEMS TERMINATED BY ','
MAP KEYS TERMINATED BY '='
STORED AS TEXTFILE;

然后,我可以加载以下内容

123|a,b|1=a,2=b|2019

root@0d2b0044b4c1:/opt# hive -e "use mydb; LOAD DATA INPATH '/path/to/file' INTO TABLE mytab PARTITION (data_date='2019');"
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/hive/lib/log4j-slf4j-impl-2.6.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/hadoop/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]

Logging initialized using configuration in jar:file:/opt/hive/lib/hive-common-2.3.2.jar!/hive-log4j2.properties Async: true
OK
Time taken: 6.456 seconds
Loading data to table mydb.mytab partition (data_date=2019)
OK
Time taken: 1.912 seconds
root@0d2b0044b4c1:/opt# hive -e "use mydb; select * from mytab;"
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/hive/lib/log4j-slf4j-impl-2.6.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/hadoop/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]

Logging initialized using configuration in jar:file:/opt/hive/lib/hive-common-2.3.2.jar!/hive-log4j2.properties Async: true
OK
Time taken: 6.843 seconds
OK
123 ["a","b"]   {"1":"a","2":"b"}   2019
root@0d2b0044b4c1:/opt#

这正是我所需要的

关于sql - 配置单元:无法从配置单元表中的文件插入数组和映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55524074/

相关文章:

hadoop - Hive 查询以根据多个可选键分配分组键

hadoop - 在Windows上安装配置单元时出错

hadoop - Apache Spark : In SparkSql, 是易受 SQL 注入(inject)攻击的 sql

php - 将要上传的图像的路径保存在数据库的服务器文件夹中

mysql - EXCEPT 命令 - 查找预订了所有船只的水手

SQL - 如何添加具有平均值的列,分组

sql - SQL Server 2005 中的数据库分区

hadoop - 如何创建我自己的 RecommenderJob?

json - 将 Json 转换为 HIVE 中的单独列

hadoop - 如何根据阿里巴巴MaxCompute中的键对值进行分组?