【发布时间】:2021-06-20 12:55:39
【问题描述】:
我需要在spark.sql() 中编写一个regexg_replace 查询,但我不知道如何处理它。出于可读性的目的,我必须为此使用 SQL。我正在尝试从表中提取主题标签。我知道如何使用 python 方法做到这一点,但我的团队中的大多数人都是 SQL 用户。
我的数据框示例如下所示:
| Insta_post |
|---|
| 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… |
我的代码:
我创建了一个临时视图:
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,所以这对我来说是新的。任何帮助以及如何构建子集的解释都将不胜感激!
【问题讨论】:
标签: apache-spark pyspark apache-spark-sql