如何在spark.sql()中使用regexp\u replace从字符串中提取哈希标记

wh6knrhe  于 2021-07-09  发布在  Spark
关注(0)|答案(2)|浏览(325)

我需要写一封信 regexg_replace 查询 spark.sql() 我不知道该怎么处理。为了便于阅读,我必须使用sql。我正试图从表中取出标签。我知道如何使用python方法实现这一点,但我的团队中的大多数人都是sql用户。
我的Dataframe示例如下所示:
今天,参议院民主党投票决定拯救互联网。很荣幸在众议院支持类似的网络中立立法…rt@nalcabpolicy:meeting with@repdarrensoto。感谢您抽出时间与@latinoleader ed marucci guzman会面#nalcabpolicy2018….rt@tharryry:我很高兴@repdarrensoto将投票支持cra否决fcc并保留我们的网络中立规则。找出…
我的代码:
我创建一个临时视图:

post_df.createOrReplaceTempView("post_tempview")

post_df = spark.sql("""
select
regexp_replace(Insta_post, '.*?(.|'')(#)(\w+)', '$1') as a 
from post_tempview
where Insta_post like '%#%'
""")

我的最终结果:

+--------------------------------------------------------------------------------------------------------------------------------------------+
|a                                                                                                                                           |
+--------------------------------------------------------------------------------------------------------------------------------------------+
|Today, Senate Dems vote to #SaveTheInternet. Proud to support similar #NetNeutrality legislation here in the House…  |
|RT @NALCABPolicy: Meeting with @RepDarrenSoto . Thanks for taking the time to meet with @LatinoLeader ED Marucci Guzman. #NALCABPolicy2018.…|
|RT @Tharryry: I am delighted that @RepDarrenSoto will be voting for the CRA to overrule the FCC and save our #NetNeutrality rules. Find out…|
+--------------------------------------------------------------------------------------------------------------------------------------------+

期望结果:

+---------------------------------+
|a                                |
+---------------------------------+
| #SaveTheInternet, #NetNeutrality|
| #NALCABPolicy2018               |
| #NetNeutrality                  |
+---------------------------------+

我真的没用过 regexp_replace 太多了,所以这对我来说是新的。任何帮助,以及解释如何结构的子集将不胜感激!

dwbf0jvd

dwbf0jvd1#

你真的需要看风景吗?因为下面的代码可能会这样做:

df = df.filter(F.col('Insta_post').like('%#%'))
col_trimmed = F.trim((F.regexp_replace('Insta_post', '.*?(#\w+)|.+', '$1 ')))
df = df.select(F.regexp_replace(col_trimmed,'\s',', ').alias('a'))
df.show(truncate=False)

# +--------------------------------+

# |a                               |

# +--------------------------------+

# |#SaveTheInternet, #NetNeutrality|

# |#NALCABPolicy2018               |

# |#NetNeutrality                  |

# +--------------------------------+

最后我用了两个 regexp_replace 所以可能会有更好的选择,只是想不出一个。

qhhrdooz

qhhrdooz2#

对于spark 3.1+,可以使用 regexp_extract_all 函数提取多个匹配项:

post_df = spark.sql("""
    select  regexp_extract_all(Insta_post, '(#\\\\w+)', 1) as a 
    from    post_tempview
    where   Insta_post like '%#%'
""")

post_df.show(truncate=False)

# +----------------------------------+

# |a                                 |

# +----------------------------------+

# |[#SaveTheInternet, #NetNeutrality]|

# |[#NALCABPolicy2018]               |

# |[#NetNeutrality]                  |

# +----------------------------------+

对于spark<3.1,可以使用 regexp_replace 要删除所有与hashtag模式不匹配的内容,请执行以下操作:

post_df = spark.sql("""
select  trim(trailing ',' from regexp_replace(Insta_post, '.*?(#\\\\w+)|.*', '$1,')) as a 
from    post_tempview
where   Insta_post like '%#%'
""")

post_df.show(truncate=False)

# +-------------------------------+

# |a                              |

# +-------------------------------+

# |#SaveTheInternet,#NetNeutrality|

# |#NALCABPolicy2018              |

# |#NetNeutrality                 |

# +-------------------------------+

注意用途 trim 删除由第一个replace创建的不必要的尾部逗号 $, .

相关问题