Python 在 Spark 中将一个简单的单行字符串转换为 RDD

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26157620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:10:19  来源:igfitidea点击:

Convert a simple one line string to RDD in Spark

pythonapache-sparkpysparkdistributed-computingrdd

提问by poiuytrez

I have a simple line:

我有一条简单的线:

line = "Hello, world"

I would like to convert it to an RDD with only one element. I have tried

我想将其转换为只有一个元素的 RDD。我试过了

sc.parallelize(line)

But it get:

但它得到:

sc.parallelize(line).collect()
['H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd']

Any ideas?

有任何想法吗?

采纳答案by michaeltang

try using List as parameter:

尝试使用 List 作为参数:

sc.parallelize(List(line)).collect()

it returns

它返回

res1: Array[String] = Array(hello,world)

回答by Dhruv

The below code works fine in Python

下面的代码在 Python 中运行良好

sc.parallelize([line]).collect()

['Hello, world']

sc.parallelize([line]).collect()

['你好,世界']

Here we are passing the parameter "line" as a list.

这里我们将参数“line”作为列表传递。