【Spark Java API】Action(3)—foreach、f
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);//獲得分割槽IDJavaRDD<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));
作者:小飛_俠_kobe
連結:
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/2310/viewspace-2818913/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【Spark Java API】Action(4)—sortBy、taSparkJavaAPI
- Spark3學習【基於Java】3. Spark-Sql常用APISparkJavaSQLAPI
- Spark in action on Kubernetes - Spark Operator的原理解析Spark
- Spark in action on Kubernetes - 儲存篇Spark
- 《Java 8 in Action》Chapter 3:Lambda表示式JavaAPT
- Spark in action on Kubernetes - 儲存篇(一)Spark
- Spark API 全集(1):Spark SQL Dataset & DataFrame APISparkAPISQL
- 《Java 8 in Action》Chapter 12:新的日期和時間APIJavaAPTAPI
- Spark RDD APISparkAPI
- Spark REST API & metricsSparkRESTAPI
- Spark 系列(九)—— Spark SQL 之 Structured APISparkSQLStructAPI
- Spark系列 - (3) Spark SQLSparkSQL
- 讓ASP.NET Web API的Action方法ASP.NETWebAPI
- JAVA中的foreach怎麼用Java
- [Java 8 Tutorial翻譯系列]Java forEach詳解Java
- JAVAEE框架學習——Struts2——Action API 使用Java框架API
- spark讀取hive異常,java.lang.NoClassDefFoundError: org/apache/tez/dag/api/SessionNotRunningSparkHiveJavaErrorApacheAPISession
- Java foreach 中List移除元素丟擲ConcurrentMJava
- Apache Spark Day3ApacheSpark
- eclipse Java Spark local模式EclipseJavaSpark模式
- Spark 3.x Spark Core詳解 & 效能優化Spark優化
- 《Java 8 in Action》Chapter 10:用Optional取代nullJavaAPTNull
- 鏈式呼叫 f(1)(2)(3) f(1,2)(3,4,5)
- Go 通過 Map/Filter/ForEach 等流式 API 高效處理資料GoFilterAPI
- Java基礎(二)- 普通for迴圈、foreach效能比較Java
- Iterator & foreach
- foreach陷阱
- JavaScript forEach()JavaScript
- F5 api介面開發實戰(一)API
- 2024-03-21 跳出forEach迴圈的3個方法
- C#效率優化(3)-- 使用foreach時避免裝箱C#優化
- Codeforces Round 970 (Div. 3)A~F
- Java 日期 APIJavaAPI
- Java -fastjson apiJavaASTJSONAPI
- Java 反射 APIJava反射API
- java常用APIJavaAPI
- ElasticSearch—— Java APIElasticsearchJavaAPI
- action概念