【问题标题】:Spark : converting Array[Byte] data to RDD or DataFrameSpark:将 Array[Byte] 数据转换为 RDD 或 DataFrame
【发布时间】:2020-02-28 02:41:43
【问题描述】:

我有 Array[Byte] 形式的数据,我想将其转换为 Spark RDD 或 DataFrame,以便我可以将我的数据以文件的形式直接写入 Google 存储桶。我无法将 Array[Byte] 数据直接写入 Google 存储桶。所以寻找这种转换。

我下面的代码能够将数据写入本地 FS,但不能写入谷歌存储桶

val encrypted = encrypt(original, readPublicKey(pubKey), outFile, true, true)
val dfis = new FileOutputStream(outFile)
dfis.write(encrypted)
dfis.close()

def encrypt(clearData: Array[Byte], encKey: PGPPublicKey, fileName: String, withIntegrityCheck: Boolean, armor: Boolean): Array[Byte] = {
...
}

那么如何将 Array[Byte] 数据转换为 RDD 或 DataFrame?我正在使用 Scala。

【问题讨论】:

    标签: scala apache-spark


    【解决方案1】:

    只需使用.toDF().toDF().rdd

    scala> val arr: Array[Byte] = Array(192.toByte, 168.toByte, 1.toByte, 4.toByte)
    arr: Array[Byte] = Array(-64, -88, 1, 4)
    
    scala> val df = arr.toSeq.toDF()
    df: org.apache.spark.sql.DataFrame = [value: tinyint]
    
    scala> df.show()
    +-----+
    |value|
    +-----+
    |  -64|
    |  -88|
    |    1|
    |    4|
    +-----+
    
    
    scala> df.printSchema()
    root
     |-- value: byte (nullable = false)
    

    【讨论】:

    • 非常感谢您的 input.scala> val arr: Array[Byte] = Array(192.toByte, 168.toByte, 1.toByte, 4.toByte) arr: Array[Byte] = Array(-64, -88, 1, 4) scala> val df = arr.toSeq.toDF() :19: error: value toDF is not a member of Seq[Byte] val df = arr.toSeq. toDF() ^ 在我面临这个问题的环境中尝试时。知道如何摆脱这个吗?
    猜你喜欢
    • 2019-04-04
    • 1970-01-01
    • 2016-05-12
    • 2016-01-20
    • 2018-03-05
    • 2017-06-02
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    相关资源
    最近更新 更多