【发布时间】:2017-05-12 19:26:57
【问题描述】:
我在看一个谜。我在 RDD 中有一堆长文档可用作 Python 字节串 (b"I'm a byte string")。现在我将此 RDD 转换为 DataFrame 以将其加入另一个 DataFrame。我是这样做的:
Data_RDD = Paths_RDD.map(open_paths).flatMap(split_files)
Data_schema = StructType([
StructField("URI", StringType(), True),
StructField("Content", StringType(), True),
])
Data_DF = sqlContext.createDataFrame(Data_RDD, schema=Data_schema)
print(Data_DF.show(5))
+--------------------+-----------+
| URI| Content|
+--------------------+-----------+
|http://01storytel...|[B@10628e42|
|http://05yxgs.com...|[B@36699775|
|http://1.lhcmaima...|[B@4e569e3b|
|http://100100.ove...|[B@18ae5bab|
|http://1015theriv...|[B@5f044435|
+--------------------+-----------+
only showing top 5 rows
这些简短的"[B@10628e42" 字符串对我来说似乎毫无用处,可能是某种指针。字节串在 RDD 中仍然是“完整的”,因为我仍然可以访问它们。所以在从 RDD 到DataFrame 的转换中,问题就出现了。现在我尝试将字节串存储在其他类型的字段中,即ByteType() 和BinaryType()。两者都不起作用,因为这些错误消息不接受字节字符串:
TypeError: ByteType can not accept object b'some string' in type <class 'bytes'>
TypeError: BinaryType can not accept object b'some string' in type <class 'bytes'>
但它变得更加奇怪。当我设置一个小规模实验时:
ByteStrings = [b'one',b'two',b'three']
rdd_ByteStrings = sc.parallelize(ByteStrings)
print(rdd_ByteStrings.take(3))
DF2_schema = StructType([
StructField("ByteString", StringType(), True),
])
DF_ByteStrings = sqlContext.createDataFrame(rdd_ByteStrings,schema=DF2_schema)
print(DF_ByteStrings.show())
在 StringType 列中也不允许使用小字节串。当我尝试运行它时,我收到以下错误消息:
StructType can not accept object b'one' in type <class 'bytes'>
当我尝试让 spark 推断类型时,它也会失败并显示以下消息:
TypeError: Can not infer schema for type: <class 'bytes'>
所以任何想法我可以如何将字节串存储在 DataFrame 中而不是 .decode() 它们。这是我只有在将两个DataFrames 连接在一起后才能做到的事情,因为另一个拥有解码信息。
我使用 Python 3.5 和 Spark 2.0.1
提前致谢!
【问题讨论】:
标签: python-3.x apache-spark dataframe pyspark apache-spark-sql