redis - 为什么使用 spark-submit 将 4000 张图片加载到 redis 需要的时间(9 分钟)比将相同的图片加载到 HBase(2.5 分钟)要长?
问题描述
将图像加载到 Redis 应该比使用 Hbase 执行相同的操作要快得多,因为 Redis 处理 RAM,而 HBase 使用 HDFS 存储数据。当我将 4000 张图片加载到 Redis 时,我很惊讶,花了 9 分钟才完成!虽然我使用 HBase 完成了相同的过程,但只用了 2.5 分钟。对此有解释吗?有什么建议可以改进我的代码吗?这是我的代码:
// The code for loading the images into Hbase (adopted from NIST)
val conf = new SparkConf().setAppName("Fingerprint.LoadData")
val sc = new SparkContext(conf)
Image.dropHBaseTable() Image.createHBaseTable()
val checksum_path = args(0)
println("Reading paths from: %s".format(checksum_path.toString))
val imagepaths = loadImageList(checksum_path) println("Got %s images".format(imagepaths.length))
imagepaths.foreach(println)
println("Reading files into RDD")
val images = sc.parallelize(imagepaths).map(paths => Image.fromFiles(paths._1, paths._2))
println(s"Saving ${images.count} images to HBase")
Image.toHBase(images)
println("Done")
} val conf = new SparkConf().setAppName("Fingerprint.LoadData") val sc = new SparkContext(conf) Image.dropHBaseTable() Image.createHBaseTable() val checksum_path = args(0) println("Reading paths from: %s".format(checksum_path.toString)) val imagepaths = loadImageList(checksum_path) println("Got %s images".format(imagepaths.length)) imagepaths.foreach(println) println("Reading files into RDD") val images = sc.parallelize(imagepaths) .map(paths => Image.fromFiles(paths._1, paths._2)) println(s"Saving ${images.count} images to HBase") Image.toHBase(images) println("Done")
} def toHBase(rdd: RDD[T]): Unit = {
val cfg = HBaseConfiguration.create()
cfg.set(TableOutputFormat.OUTPUT_TABLE, tableName)
val job = Job.getInstance(cfg)
job.setOutputFormatClass(classOf[TableOutputFormat[String]])
rdd.map(Put).saveAsNewAPIHadoopDataset(job.getConfiguration)
}
//加载图片到Redis的代码
val images = sc.parallelize(imagepaths).map(paths => Image.fromFiles(paths._1, paths._2)).collect
for(i <- images){
val stringRdd = sc.parallelize(Seq((i.uuid, new String(i.Png, StandardCharsets.UTF_8))))
sc.toRedisKV(stringRdd)(redisConfig)
stringRdd.collect}
println("Done")
解决方案
推荐阅读
- firebase-dynamic-links - Firebase 动态链接可以用于通用应用邀请和特定页面邀请以跟踪推荐吗?
- c# - 使用范围运算符给出错误预定义类型“System.range”未定义或导入
- ethereum - 使用数组中数百个预填充结构创建 Solidity 合约的 Gas 问题
- python - 如何导出 csv 或文本文件中的所有链接?
- c# - 将项目转换为 NETSDK 格式后表单中的资源异常
- python-3.x - 多台计算机通信 - multiprocessing.Listener,确保端口保持可用?
- spring - 在 @SpringBootTest 中自动装配时,WebTestClient 为空
- roslyn - Roslyn - 获取对分配给属性的字符串的引用
- network-interface - Azure 网络接口有效规则
- docx4j - 邮件与 docx4j 合并后,docx 仍然是一个模板文件