日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Spark Java API:foreach、foreachPartition、lookup

發布時間:2024/1/17 java 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spark Java API:foreach、foreachPartition、lookup 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

foreach


官方文檔描述:

Applies a function f to all elements of this RDD.

函數原型:

def foreach(f: VoidFunction[T])

foreach用于遍歷RDD,將函數f應用于每一個元素。

源碼分析:

def foreach(f: T => Unit): Unit = withScope { val cleanF = sc.clean(f) sc.runJob(this, (iter: Iterator[T]) => iter.foreach(cleanF)) }

實例:

List<Integer> data = Arrays.asList(5, 1, 1, 4, 4, 2, 2); JavaRDD<Integer> javaRDD = javaSparkContext.parallelize(data,3); javaRDD.foreach(new VoidFunction<Integer>() { @Override public void call(Integer integer) throws Exception { System.out.println(integer); } });

foreachPartition


官方文檔描述:

Applies a function f to each partition of this RDD.

函數原型:

def foreachPartition(f: VoidFunction[java.util.Iterator[T]])

foreachPartition和foreach類似,只不過是對每一個分區使用f。

源碼分析:

def foreachPartition(f: Iterator[T] => Unit): Unit = withScope { val cleanF = sc.clean(f) sc.runJob(this, (iter: Iterator[T]) => cleanF(iter)) }

實例:

List<Integer> data = Arrays.asList(5, 1, 1, 4, 4, 2, 2); JavaRDD<Integer> javaRDD = javaSparkContext.parallelize(data,3);//獲得分區ID JavaRDD<String> partitionRDD = javaRDD.mapPartitionsWithIndex(new Function2<Integer, Iterator<Integer>, Iterator<String>>() { @Override public Iterator<String> call(Integer v1, Iterator<Integer> v2) throws Exception { LinkedList<String> linkedList = new LinkedList<String>(); while(v2.hasNext()){ linkedList.add(v1 + "=" + v2.next()); }return linkedList.iterator(); } },false); System.out.println(partitionRDD.collect()); javaRDD.foreachPartition(new VoidFunction<Iterator<Integer>>() { @Override public void call(Iterator<Integer> integerIterator) throws Exception { System.out.println("___________begin_______________"); while(integerIterator.hasNext()) System.out.print(integerIterator.next() + " "); System.out.println("\n___________end_________________"); } });

lookup


官方文檔描述:

Return the list of values in the RDD for key `key`. This operation is done efficiently if the RDD has a known partitioner by only searching the partition that the key maps to.

函數原型:

def lookup(key: K): JList[V]
  • ?

lookup用于(K,V)類型的RDD,指定K值,返回RDD中該K對應的所有V值。

源碼分析:

def lookup(key: K): Seq[V] = self.withScope { self.partitioner match { case Some(p) => val index = p.getPartition(key) val process = (it: Iterator[(K, V)]) => { val buf = new ArrayBuffer[V] for (pair <- it if pair._1 == key) { buf += pair._2 } buf } : Seq[V] val res = self.context.runJob(self, process, Array(index), false) res(0) case None => self.filter(_._1 == key).map(_._2).collect() } }

從源碼中可以看出,如果partitioner不為空,計算key得到對應的partition,在從該partition中獲得key對應的所有value;如果partitioner為空,則通過filter過濾掉其他不等于key的值,然后將其value輸出。

實例:

List<Integer> data = Arrays.asList(5, 1, 1, 4, 4, 2, 2); JavaRDD<Integer> javaRDD = javaSparkContext.parallelize(data, 3); JavaPairRDD<Integer,Integer> javaPairRDD = javaRDD.mapToPair(new PairFunction<Integer, Integer, Integer>() { int i = 0; @Override public Tuple2<Integer, Integer> call(Integer integer) throws Exception { i++; return new Tuple2<Integer, Integer>(integer,i + integer); } }); System.out.println(javaPairRDD.collect()); System.out.println("lookup------------" + javaPairRDD.lookup(4));

總結

以上是生活随笔為你收集整理的Spark Java API:foreach、foreachPartition、lookup的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。