【问题标题】:Creating a HIVE table that filters data from a .csv in HDFS based on the value in a column创建一个 HIVE 表,根据列中的值从 HDFS 中的 .csv 过滤数据
【发布时间】:2017-06-19 11:18:21
【问题描述】:

我目前有一个文件,其中包含需要填充 9 个不同表的数据。这些表中的每一个都有不同数量的列和数据类型,因此我需要过滤源文件(使用确定行将进入哪个表的第一列)。

我当前的方法是创建一个包含通用列名称 col_1、col_2 等直到文件中最后填充列的表,然后创建 9 个引用该文件的视图。我遇到的问题是,由于表都是不同的结构,因此同一列中出现了不同的数据类型。

是否有可能创建一个动态架构,根据第一列过滤 HIVE 表指向的 .csv??

谢谢

【问题讨论】:

  • 请添加数据样本

标签: csv hive hdfs hiveql avro


【解决方案1】:

演示

data.csv

1,1,Now,11,22,2016-12-12
1,2,I,33,44,2017-01-01
3,3,heard,55,66,2017-02-02
1,4,you,77,88,2017-03-03
2,5,know,99,1010,2017-04-04
1,6,that,1111,1212,2017-05-05
2,7,secret,1313,1414,2017-06-06

create external table mycsv
(
    rec_type    int
   ,id          int
   ,mystring    string
   ,myint1      int
   ,myint2      int
   ,mydate      date
)
row format delimited
fields terminated by ','
stored as textfile
;

select * from mycsv;

+----------+----+----------+--------+--------+------------+
| rec_type | id | mystring | myint1 | myint2 |   mydate   |
+----------+----+----------+--------+--------+------------+
|        1 |  1 | Now      | 11     | 22     | 2016-12-12 |
|        1 |  2 | I        | 33     | 44     | 2017-01-01 |
|        3 |  3 | heard    | 55     | 66     | 2017-02-02 |
|        1 |  4 | you      | 77     | 88     | 2017-03-03 |
|        2 |  5 | know     | 99     | 1010   | 2017-04-04 |
|        1 |  6 | that     | 1111   | 1212   | 2017-05-05 |
|        2 |  7 | secret   | 1313   | 1414   | 2017-06-06 |
+----------+----+----------+--------+--------+------------+

create table t1(id int,mystring string);
create table t2(id int,mystring string,mydate date);
create table t3(id int,mydate date,myint1 int,myint2 int);

from mycsv 
insert into t1 select id,mystring               where rec_type = 1
insert into t2 select id,mystring,mydate        where rec_type = 2
insert into t3 select id,mydate,myint1,myint2   where rec_type = 3

select * from t1;

+----+----------+
| id | mystring |
+----+----------+
|  1 | Now      |
|  2 | I        |
|  4 | you      |
|  6 | that     |
+----+----------+

select * from t2;

+----+----------+------------+
| id | mystring |   mydate   |
+----+----------+------------+
|  5 | know     | 2017-04-04 |
|  7 | secret   | 2017-06-06 |
+----+----------+------------+

select * from t3;

+----+------------+--------+--------+
| id |   mydate   | myint1 | myint2 |
+----+------------+--------+--------+
|  3 | 2017-02-02 |     55 |     66 |
+----+------------+--------+--------+

【讨论】:

  • 您好 Dudu,谢谢您的回复我感觉这是唯一的选择。为了自动化,最好的方法是创建一个在 apache PIG 中运行的 UDF?理想情况下,用户文件提供者只需将文件放到 HDFS 文件存储中,它就可以立即在 HIVE 表中看到。..
  • 这(from + 多个insert)似乎是一个干净的解决方案。为什么你觉得它无关紧要?
猜你喜欢
  • 2020-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多