RegEx用于从JSON检索乐谱号

时间:2019-04-20 01:26:20

标签: regex hive apache-spark-sql regex-group regex-greedy

我需要在得分后提取浮点数。

{"reason_desc":
   {
    "score":"0.1",
    "numOfIndicatrix":"0",
    "indicatrix":[]},
    "success":true,
    "id":"1555039965661065S427A2DCF5787920"
}

我希望输出0.1或任何用“”括起来的数字。

2 个答案:

答案 0 :(得分:1)

This RegEx可能会帮助您获得0.1。它将目标行分为两组,第二组($2)返回所需的浮点数:

("score":")([0-9\.\,]+)

RegEx

答案 1 :(得分:1)

您无需在Hive中使用regexp解析JSON,它具有相同的嵌入式功能:

with your_table as (--use your table instead of this
select '{"reason_desc":
   {
    "score":"0.1",
    "numOfIndicatrix":"0",
    "indicatrix":[]},
    "success":true,
    "id":"1555039965661065S427A2DCF5787920"
}' as json_col
)

select get_json_object(t.json_col,'$.reason_desc.score') as score
  from your_table t 

结果:

0.1

另请参阅:json_tuple