Spark Pipe调用外部程序

spark 中,有种特殊的Rdd,即pipedRdd,提供了调用外部程序如基于CUDA的C++程序,使其能够更快的进行计算。caffe on spark 和tensorflow on spark 也是基于此机制,那么,spark 和 外部程序是怎么交互的呢? 下面通过一个简单的例子验证。

步骤1:创建外部脚本

#!/bin/sh
echo "Running shell script"
while read LINE; do
   echo ${LINE}!
done

步骤2:spark rdd 调用

val data = sc.parallelize(List("hi","hello","how","are","you"))
val scriptPath = "/root/echo.sh"
val pipeRDD = dataRDD.pipe(scriptPath)
pipeRDD.collect()

查看运行结果,发现为:

Array[String] = Array(Running shell script, hi!, Running shell script, hello!, Running shell script, how!, Running shell script, are!, you!)

其中,Running shell script只出现了4次,rdd的count为9,可见有两次共享了一个外部进程。在此推断和RDD并行度有关。

步骤3:调整并行度

dataRDD.repartition(5)

结果为:

Array[String] = Array(Running shell script, are!, Running shell script, hi!, hello!, you!, Running shell script, Running shell script, Running shell script, how!)

Running shell script只出现了5次,rdd的count为10了。

结论

rdd pipe 每个分区,启动一次外部程序,输入通过stdin传入,结果通过stdout传出。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值