簡體   English   中英

多行字符串到 Spark Dataframe 沒有中斷

[英]Multiline string to Spark Dataframe no break

我有一個多行字符串,我想轉換為 df。

val string= 
"""
Here is the

multiline/multi paragraph

example.
"""

我正在尋找一個 df 看起來像:

+--------------------+---+
|               value|doc|
+--------------------+---+
|Here is the         |  1|
|multiline/multipar..|   |
|example.            |   |
+--------------------+---+

我得到的是:

+--------------------+---+
|               value|doc|
+--------------------+---+
|Here is the         |  1|
|multiline/multipar..|  2|
|example.            |  3|
+--------------------+---+

這是我的代碼:

val df = spark.read.option("multiLine", "true").text("test1.txt")
val df_id = df.withColumn("doc",monotonicallyIncreasingId)
df_id.show()

您必須引用輸入字符串,並且您應該使用csv reader 作為 DataFrameReader 的text方法沒有選項multiLine

val df = spark.read.option("multiLine", "true")
              .option("quote", "\"")
              .option("escape", "\"")
              .csv("test1.txt")

您可以在此處找到適用於每種方法的選項。

@jxc 能夠在上面的評論中幫助我,我只是在這里重寫他們的解決方案,因為我沒有選擇將他們的評論顯示為已接受的解決方案。

val df = spark.read.option("wholetext", "true").text("test1.txt")
val df_id = df.withColumn("doc",monotonicallyIncreasingId)
df_id.show()

val input = sc.wholeTextFiles("test1.txt").toDF("text", "doc")

兩者都有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM