我有一个多行字符串,我想转换成一个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()发布于 2020-01-03 23:12:54
@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")两者都起作用了。
发布于 2020-01-03 04:40:47
您必须用引号将输入字符串引起来,并且应该使用csv阅读器,因为DataFrameReader的text方法没有multiLine选项
val df = spark.read.option("multiLine", "true")
.option("quote", "\"")
.option("escape", "\"")
.csv("test1.txt")您可以在here中找到每种方法可用的选项。
https://stackoverflow.com/questions/59569284
复制相似问题