【问题标题】:How to use regexp_replace in spark.sql() to extract hashtags from string如何在 spark.sql() 中使用 regexp_replace 从字符串中提取主题标签
【发布时间】: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


    【解决方案1】:

    对于 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 ,您可以使用 regexp_replace 删除所有与主题标签模式不匹配的内容:

    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 删除由第一次替换$, 创建的不必要的尾随逗号。

    【讨论】:

      【解决方案2】:

      你真的需要一个视图吗?因为下面的代码可能会这样做:

      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,所以可能会有更好的选择,只是想不出一个。

      【讨论】:

        猜你喜欢
        • 2021-07-30
        • 2017-08-09
        • 2023-01-16
        • 2012-06-15
        • 2021-12-25
        • 2011-01-05
        • 1970-01-01
        • 2021-03-19
        • 2018-10-07
        相关资源
        最近更新 更多