繁体   English   中英

从clojure.lang.LazySeq转换为org.apache.spark.api.java.JavaRDD类型

[英]Convert from clojure.lang.LazySeq to type org.apache.spark.api.java.JavaRDD

我在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(火花包装器)将其转换回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"]]

谢谢。

首先RDDS都没有可迭代(不执行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-pairsmap-partitions

(-> rdd (f/map-partitions fill-pairs) (f/collect))

这里隐藏的假设是分区的顺序遵循值的顺序。 它可能会或可能不会在一般情况下出现,但如果没有明确的排序,则可能是您可以获得的最佳选择。

另一种方法是zipWithIndex ,交换值的顺序并执行带有offset的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)

接下来,您可以使用与以前相似的方法进行映射。

编辑

关于使用原子的快速说明。 问题是缺少参照透明性,您正在利用给定实现的附带属性,而不是合同。 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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM