【问题标题】:How to change case of whole column to lowercase?如何将整列的大小写更改为小写?
【发布时间】:2017-09-15 23:37:06
【问题描述】:

我想在 Spark 数据集中将整列的大小写更改为小写

        Desired Input
        +------+--------------------+
        |ItemID|       Category name|
        +------+--------------------+
        |   ABC|BRUSH & BROOM HAN...|
        |   XYZ|WHEEL BRUSH PARTS...|
        +------+--------------------+

        Desired Output
        +------+--------------------+
        |ItemID|       Category name|
        +------+--------------------+
        |   ABC|brush & broom han...|
        |   XYZ|wheel brush parts...|
        +------+--------------------+

我尝试使用 collectAsList()toString(),对于非常大的数据集,这是一个缓慢而复杂的过程。

我还发现了一个“低”的方法,但不知道如何让它在 dasaset 中工作 请建议我一个简单或有效的方法来完成上述操作。提前致谢

【问题讨论】:

    标签: java apache-spark apache-spark-sql apache-spark-dataset


    【解决方案1】:

    首先你应该添加库

    import static org.apache.spark.sql.functions.lower;
    

    那么您需要将lower 方法放在正确的位置。这是一个例子:

    .and(lower(df1.col("field_name")).equalTo("offeringname"))
    

    我在这里阅读了所有答案,然后自己尝试了一下,出于某种原因,我被 IntelliJ Idea 卡住了几分钟,直到我能理解它(图书馆方面)。如果您遇到此故障,只需按照 IntelliJ 的建议添加库,因为它会在未知时弹出。

    祝你好运。

    【讨论】:

      【解决方案2】:

      我知道了(使用Functions#lower,参见Javadoc

      import org.apache.spark.sql.functions.lower

              String columnName="Category name";
              src=src.withColumn(columnName, lower(col(columnName)));
              src.show();
      

      这用保留整个数据集的新列替换了旧列。

              +------+--------------------+
              |ItemID|       Category name|
              +------+--------------------+
              |   ABC|brush & broom han...|
              |   XYZ|wheel brush parts...|
              +------+--------------------+
      

      【讨论】:

      • import org.apache.spark.sql.functions._ 更好,因为您还需要导入才能使用 col() 函数
      【解决方案3】:

      使用org.apache.spark.sql.functions中的lower函数

      例如:

      df.select($"q1Content", lower($"q1Content")).show
      

      输出。

      +--------------------+--------------------+
      |           q1Content|    lower(q1Content)|
      +--------------------+--------------------+
      |What is the step ...|what is the step ...|
      |What is the story...|what is the story...|
      |How can I increas...|how can i increas...|
      |Why am I mentally...|why am i mentally...|
      |Which one dissolv...|which one dissolv...|
      |Astrology: I am a...|astrology: i am a...|
      | Should I buy tiago?| should i buy tiago?|
      |How can I be a go...|how can i be a go...|
      |When do you use  ...|when do you use  ...|
      |Motorola (company...|motorola (company...|
      |Method to find se...|method to find se...|
      |How do I read and...|how do i read and...|
      |What can make Phy...|what can make phy...|
      |What was your fir...|what was your fir...|
      |What are the laws...|what are the laws...|
      |What would a Trum...|what would a trum...|
      |What does manipul...|what does manipul...|
      |Why do girls want...|why do girls want...|
      |Why are so many Q...|why are so many q...|
      |Which is the best...|which is the best...|
      +--------------------+--------------------+
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-11-24
        • 2019-07-20
        • 2012-03-08
        • 2020-12-29
        • 2010-09-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多