[英]Cast a column to binary datatype in spark
I have a Spark use case where I have to create a null column and cast to a binary datatype.我有一个 Spark 用例,我必须创建一个空列并转换为二进制数据类型。 I tried the below but it is not working.我尝试了以下但它不起作用。 When I replace Binary by integer, it works.当我用整数替换二进制时,它起作用了。 I also tried BinaryType and Array[Byte].我也试过 BinaryType 和 Array[Byte]。 Must be missing something here.这里一定少了点什么。
val ip1 = sqlContext.read
.parquet("/home/hadoop/work/aa/bbb/ccc/data/today")
.toDF();
val ip2 = ip1
.withColumn("user_hll",lit("0"))
.select(col("start_timestamp_hr"),col("user_hll"))
.withColumn("hll",col("user_hll").cast("Binary"))
Any help is appreciated任何帮助表示赞赏
如果你想要一个空二进制列,你只需要:
.withColumn("hll", lit(null).cast(org.apache.spark.sql.types.BinaryType))
instead of casting you could also use lit
directly除了投射,您还可以直接使用lit
.withColumn("hll",lit("0".getBytes)) // gives [30]
or或者
.withColumn("hll",lit(Array.empty[Bytes])) // gives []
or if you want ǹull
, you can do :或者如果你想要ǹull
,你可以这样做:
.withColumn("hll",typedLit(Option.empty[Array[Byte]])) // gives null
It worked.有效。 Simple miss from my side我身边的简单想念
.cast("Binary")
Any better way would be appreciated任何更好的方法将不胜感激
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.