将Spark中的列转换为二进制数据类型

huangapple go评论71阅读模式
英文:

Cast a column to binary datatype in spark

问题

我有一个Spark用例,我需要创建一个空列并将其转换为二进制数据类型。我尝试了下面的方法,但没有成功。当我将"Binary"替换为"integer"时,它可以工作。我还尝试了"BinaryType"和"Array[Byte]",但肯定漏掉了一些东西。

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"))

任何帮助将不胜感激。

英文:

I have a Spark use case where I have to create a null column and cast to a binary datatype. I tried the below but it is not working. When I replace Binary by integer, it works. I also tried BinaryType and 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

答案1

得分: 3

.withColumn("hll", lit(null).cast(org.apache.spark.sql.types.BinaryType))

英文:

If you want a null binary column, all you need is:

.withColumn("hll", lit(null).cast(org.apache.spark.sql.types.BinaryType))

答案2

得分: 1

不需要转换,保持原文如下:

instead of casting you could also use lit directly

.withColumn("hll",lit("0".getBytes)) // gives [30]

or

.withColumn("hll",lit(Array.empty[Bytes])) // gives []

or if you want null, you can do :

.withColumn("hll",typedLit(Option.empty[Array[Byte]])) // gives null
英文:

instead of casting you could also use lit directly

.withColumn("hll",lit("0".getBytes)) // gives [30]

or

.withColumn("hll",lit(Array.empty[Bytes])) // gives []

or if you want ǹull, you can do :

.withColumn("hll",typedLit(Option.empty[Array[Byte]])) // gives null

答案3

得分: 0

It worked. Simple miss from my side

.cast("Binary") 

Any better way would be appreciated

英文:

It worked. Simple miss from my side

.cast("Binary") 

Any better way would be appreciated

huangapple
  • 本文由 发表于 2020年1月7日 02:24:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/59617081.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定