我有一个带有二进制二进制二进制二进制列的表:
>>> df.show(3)
+--------+--------------------+
| t| bytes|
+--------+--------------------+
|0.145533|[10 50 04 89 00 3...|
|0.345572|[60 94 05 89 80 9...|
|0.545574|[99 50 68 89 00 7...|
+--------+--------------------+
only showing top 3 rows
>>> df.schema
StructType(List(StructField(t,DoubleType,true),StructField(bytes,BinaryType,true)))
如果我提取了二进制的第一个字节,我会从Spark中获得例外:
>>> df.select(n["t"], df["bytes"].getItem(0)).show(3)
AnalysisException: u"Can't extract value from bytes#477;"
ArrayType(ByteType)
的演员也没有起作用:
>>> df.select(n["t"], df["bytes"].cast(ArrayType(ByteType())).getItem(0)).show(3)
AnalysisException: u"cannot resolve '`bytes`' due to data type mismatch: cannot cast BinaryType to ArrayType(ByteType,true) ..."
如何提取字节?
您可以为此做一个简单的UDF:
from pyspark.sql import functions as f
a = bytearray([10, 50, 04])
df = sqlContext.createDataFrame([(1, a), (2, a)], ("t", "bytes"))
df.show()
+---+----------+
| t| bytes|
+---+----------+
| 1|[0A 32 04]|
| 2|[0A 32 04]|
+---+----------+
u = f.udf(lambda a: a[0])
df.select(u(df['bytes']).alias("first")).show()
+-----+
|first|
+-----+
| 10|
| 10|
+-----+
编辑
如果您希望提取的位置是参数,则可以进行一些咖喱:
func = lambda i: lambda a: a[i]
my_udf = lambda i: f.udf(func(i))
df.select(my_udf(2)(df['bytes']).alias("last")).show()
+----+
|last|
+----+
| 4|
| 4|
+----+
一种替代方法是使用本机API函数substring
(DOCS),该功能可以" slice"使用位置和长度参数的二进制类型。
这是一个演示,按照接受答案的示例:
df.select("*",
f.substring("bytes", 2, 1),
f.substring("bytes", 2, 1).cast("string")).show()
+---+----------+----------------------+--------------------------------------+
| t| bytes|substring(bytes, 2, 1)|CAST(substring(bytes, 2, 1) AS STRING)|
+---+----------+----------------------+--------------------------------------+
| 1|[0A 32 04]| [32]| 2|
| 2|[0A 32 04]| [32]| 2|
+---+----------+----------------------+--------------------------------------+