scala - 在 Spark Databricks (scala) 中写入/读取/删除二进制数据
问题描述
我对 Databricks (Scala) 上的 Spark 很陌生,我想知道如何将类型为变量的内容写入装载存储(Azure Data Lake)中Array[Byte]
的临时文件或. 然后我想知道如何将它作为 InputStream 读取,然后在我完成后将其删除。data.bin
mtn/somewhere/tmp/
file:/tmp/
到目前为止我读过的所有方法都不起作用或不适用于二进制数据。
谢谢你。
解决方案
原来这段代码工作正常:
import java.io._
import org.apache.commons.io.FileUtils
// Create or collect the data
val bytes: Array[Byte] = <some_data>
try {
// Write data to temp file
// Note : Here I use GRIB2 file as I manipulate forecast data,
// but you can use .bin or .png/.jpg (if it's an image data)
// extensions or no extension at all. It doesn't matter.
val path: String = "mf-data.grib"
val file: File = new File(path)
FileUtils.writeByteArrayToFile(file, bytes)
// Read the temp file
val input = new FileInputStream(path)
////////// Do something with it //////////
// Remove the temp file
if (!file.delete()) {
println("Cannot delete temporary file !")
}
} catch {
case _: Throwable => println("An I/O error occured")
推荐阅读
- javascript - 仅获取图像 URL 的替换字符失败
- angular - Angular中使用flatMap的大量api调用
- node.js - Async/Await 在递归函数中不起作用
- javascript - 在属性中附加带有三引号的 HTML
- c++ - 有没有办法在 GFlags 中传递带有命令行参数的整数数组?
- android - 禁用一个文件的 ANDROID 编译标志
- ruby-on-rails - Rails 和 Wordpress 单点登录 - 使用设计进行身份验证
- search-engine - 如何在 Google 中搜索具有特定查询参数的内部链接
- simulation - 在 NetLogo 中模拟烟雾的扩展
- scala - Scala 发散隐式扩展