从压缩(gz格式)文件创建外部表而不选择所有字段

bweufnob  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(417)

我在一个文件夹里有gz文件。我只需要从这些文件3列,但每行有超过100个。现在我用这种方式创建一个视图。

  1. drop table MAK_CHARGE_RCR;
  2. create external table MAK_CHARGE_RCR
  3. (LINE string)
  4. STORED as SEQUENCEFILE
  5. LOCATION '/apps/hive/warehouse/mydb.db/file_rcr';
  6. drop view VW_MAK_CHARGE_RCR;
  7. create view VW_MAK_CHARGE_RCR as
  8. Select LINE[57] as CREATE_DATE, LINE[64] as SUBS_KEY, LINE[63] as RC_TERM_NAME
  9. from
  10. (Select split(LINE, '\\|') as LINE
  11. from MAK_CHARGE_RCR) a;

视图中有我需要的字段。现在我也要这么做,但是没有CTA,我不知道该怎么做。我能做什么?
有人告诉我table一定是这个样子

  1. create external table MAK_CHARGE_RCR
  2. (CREATE_DATE string, SUBS_KEY string, RC_TERM_NAME etc)

我可以这样把线分开

  1. ROW FORMAT DELIMITED
  2. FIELDS TERMINATED BY '\\|'

但我需要列出每一列。我有另外一组超过1000列的文件。所有这些我都要列出来。这似乎有点过分,所以我想知道是否有可能这样做

  1. create external table arstel.MAK_CHARGE_RCR
  2. (split(LINE, '\\|')[57] string,
  3. split(LINE, '\\|')[64] string
  4. etc)

这显然不起作用,但也许有解决办法?

cfh9epnr

cfh9epnr1#

正则表达式
出于教育目的
附笔
我打算创建一个csv serde的增强版本,除了一个额外的参数和请求列的位置。

演示

猛击

  1. echo {a..c}{1..100} | xargs -n 100 | tr ' ' '|' | \
  2. hdfs dfs -put - /user/hive/warehouse/mytable/data.txt

Hive

  1. create external table mytable
  2. (
  3. col58 string
  4. ,col64 string
  5. ,col65 string
  6. )
  7. row format serde 'org.apache.hadoop.hive.serde2.RegexSerDe'
  8. with serdeproperties ("input.regex" = "^(?:([^|]*)\\|){58}(?:([^|]*)\\|){6}([^|]*)\\|.*$")
  9. stored as textfile
  10. location '/user/hive/warehouse/mytable'
  11. ;
  1. select * from mytable
  2. ;
  1. +---------------+---------------+---------------+
  2. | mytable.col58 | mytable.col64 | mytable.col65 |
  3. +---------------+---------------+---------------+
  4. | a58 | a64 | a65 |
  5. | b58 | b64 | b65 |
  6. | c58 | c64 | c65 |
  7. +---------------+---------------+---------------+
展开查看全部

相关问题