我在 clojure 中开发了一个函数,用于从最后一个非空值填充空列,我假设这可行,给定
(:require [flambo.api :as f])
(defn replicate-val
[ rdd input ]
(let [{:keys [ col ]} input
result (reductions (fn [a b]
(if (empty? (nth b col))
(assoc b col (nth a col))
b)) rdd )]
(println "Result type is: "(type result))))
得到这个:
;=> "Result type is: clojure.lang.LazySeq"
问题是如何使用 flambo(spark 包装器)将其转换回 JavaRDD 类型
我在 let
表单中尝试了 (f/map result #(.toJavaRDD %))
来尝试转换为 JavaRDD
类型
我收到这个错误
"No matching method found: map for class clojure.lang.LazySeq"
这是预期的,因为结果的类型为 clojure.lang.LazySeq
问题是如何进行这种转换,或者如何重构代码以适应这种情况。
这是一个示例输入 rdd:
(type rdd) ;=> "org.apache.spark.api.java.JavaRDD"
但看起来像:
[["04" "2" "3"] ["04" "" "5"] ["5" "16" ""] ["07" "" "36"] ["07" "" "34"] ["07" "25" "34"]]
所需的输出是:
[["04" "2" "3"] ["04" "2" "5"] ["5" "16" ""] ["07" "16" "36"] ["07" "16" "34"] ["07" "25" "34"]]
谢谢。
最佳答案
首先,RDD 不可迭代(不实现 ISeq
),因此您不能使用 reductions
。忽略访问先前记录的整个想法是相当棘手的。首先,您无法直接访问另一个分区的值。此外,只有不需要打乱的转换才能保留顺序。
这里最简单的方法是使用具有明确顺序的数据帧和窗口函数,但据我所知,Flambo 没有实现所需的方法。始终可以使用原始 SQL 或访问 Java/Scala API,但如果您想避免这种情况,可以尝试以下管道。
首先让我们创建一个广播变量,其中包含每个分区的最后值:
(require '[flambo.broadcast :as bd])
(import org.apache.spark.TaskContext)
(def last-per-part (f/fn [it]
(let [context (TaskContext/get) xs (iterator-seq it)]
[[(.partitionId context) (last xs)]])))
(def last-vals-bd
(bd/broadcast sc
(into {} (-> rdd (f/map-partitions last-per-part) (f/collect)))))
接下来是实际工作的一些助手:
(defn fill-pair [col]
(fn [x] (let [[a b] x] (if (empty? (nth b col)) (assoc b col (nth a col)) b))))
(def fill-pairs
(f/fn [it] (let [part-id (.partitionId (TaskContext/get)) ;; Get partion ID
xs (iterator-seq it) ;; Convert input to seq
prev (if (zero? part-id) ;; Find previous element
(first xs) ((bd/value last-vals-bd) part-id))
;; Create seq of pairs (prev, current)
pairs (partition 2 1 (cons prev xs))
;; Same as before
{:keys [ col ]} input
;; Prepare mapping function
mapper (fill-pair col)]
(map mapper pairs))))
最后你可以使用fill-pairs
来map-partitions
:
(-> rdd (f/map-partitions fill-pairs) (f/collect))
这里隐藏的假设是分区的顺序遵循值的顺序。它可能是也可能不是一般情况,但如果没有明确的排序,它可能是你能得到的最好的。
替代方法是zipWithIndex
,交换值的顺序并使用偏移量执行连接。
(require '[flambo.tuple :as tp])
(def rdd-idx (f/map-to-pair (.zipWithIndex rdd) #(.swap %)))
(def rdd-idx-offset
(f/map-to-pair rdd-idx
(fn [t] (let [p (f/untuple t)] (tp/tuple (dec' (first p)) (second p))))))
(f/map (f/values (.rightOuterJoin rdd-idx-offset rdd-idx)) f/untuple)
接下来您可以使用与以前类似的方法进行映射。
编辑
快速说明on using atoms 。问题在于缺乏引用透明度,并且您正在利用给定实现的附带属性而不是契约(Contract)。 map
语义中没有任何内容要求按给定顺序处理元素。如果内部实现发生变化,它可能不再有效。使用 Clojure
(defn foo [x] (let [aa @a] (swap! a (fn [&args] x)) aa))
(def a (atom 0))
(map foo (range 1 20))
相比:
(def a (atom 0))
(pmap foo (range 1 20))
关于java - 从 clojure.lang.LazySeq 转换为类型 org.apache.spark.api.java.JavaRDD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32207234/