【问题标题】:regex for specific delimiter string in Hive serdeHive serde 中特定分隔符字符串的正则表达式
【发布时间】:2017-10-02 22:06:03
【问题描述】:

我使用 serde 读取带有分隔符的特定格式的数据 |

我的一行数据可能看起来像:key1=value2|key2=value2|key3="va , lues",我创建的配置单元表如下:

CREATE EXTERNAL TABLE(
field1 STRING,
field2 STRING,
field3 STRING
)
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.RegexSerDe'
WITH SERDEPROPERTIES (
  "input.regex" = "([^\\|]*)\\|([^\\|]*)\\|([^\\|]*)",
  "output.format.string" = "%1$s %2$s %3$s"
)
STORED AS TEXTFILE;

我需要提取所有值,忽略所有配额(如果存在)。 结果看起来像一个

 value2  value2 va , lues

如何更改我当前的正则表达式以获取提取值?

【问题讨论】:

  • 给定输入的当前输出结果是什么?
  • key1=value2 key2=value2 key3="va , lues"
  • 所以改变这个:"input.regex" = "[^\\|=]*=\"?([^\\|]*)\"?\\|[^\\|=]*=\"?([^\\|]*)\"?\\|[^\\|=]*=\"?([^\\|]*)\"?",
  • 用例看起来很奇怪。你怎么不需要钥匙?
  • @horcrux - | 可能包含在引用的值中

标签: regex hadoop hive hive-serde


【解决方案1】:

我目前可以提供 2 个选项,但没有一个是完美的。
顺便说一句,"output.format.string" 已过时且没有任何作用。

1

create external table mytable
(
    q1          string    
   ,field1      string
   ,q2          string
   ,field2      string
   ,q3          string
   ,field3      string
)
row format serde 'org.apache.hadoop.hive.serde2.RegexSerDe'
with serdeproperties ('input.regex' = '.*?=(?<q1>"?)(.*?)(?:\\k<q1>)\\|.*?=(?<q2>"?)(.*?)(?:\\k<q2>)\\|.*?=(?<q3>"?)(.*?)(?:\\k<q3>)')
stored as textfile
;

select * from mytable
;

+----+--------+----+--------+----+-----------+
| q1 | field1 | q2 | field2 | q3 |  field3   |
+----+--------+----+--------+----+-----------+
|    | value2 |    | value2 | "  | va , lues |
+----+--------+----+--------+----+-----------+

2

create external table mytable
(
    field1 string
   ,field2 string
   ,field3 string
)
row format serde 'org.apache.hadoop.hive.serde2.RegexSerDe'
with serdeproperties ('input.regex' = '.*?=(".*?"|.*?)\\|.*?=(".*?"|.*?)\\|.*?=(".*?"|.*?)')
stored as textfile
;

select * from mytable
;

+--------+--------+-------------+
| field1 | field2 |   field3    |
+--------+--------+-------------+
| value2 | value2 | "va , lues" |
+--------+--------+-------------+

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多