scala - Spark:将 Array[Byte] 数据转换为 RDD 或 DataFrame
问题描述
我有 Array[Byte] 形式的数据,我想将其转换为 Spark RDD 或 DataFrame,以便我可以将数据以文件的形式直接写入 Google 存储桶。我无法将 Array[Byte] 数据直接写入 Google 存储桶。所以寻找这种转换。
我下面的代码能够将数据写入本地 FS,但不能写入 Google 存储桶
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?我正在使用斯卡拉。
解决方案
只需使用.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)
推荐阅读
- r - 有没有用标签替换 value.labels 的功能?R
- amazon-web-services - 多个 terraform 项目共享基础设施
- node.js - 我如何通过 API 使用节点服务器在 mongo db 中存储二维矩阵
- javascript - 如何实现开帘动画
- swift - SwiftUI - 如何在上传到 Firebase 存储之前调整图像大小
- sql - 我试图在结果中检索所有产品及其总销售额
- python-3.8 - 从获取请求中下载多个页面
- r - 用于变量分析的自动 VIF(变量重要性因子)
- flutter - Flutter App 在系统开启时启动
- algorithm - 从 {0,21} 中选出三个数不放回,其中有多少个数之和是 3 的倍数?