collectAsMap : 针对K,V格式的RDD进行操作,将K,V格式的RDD回收到Driver端形成一个Map
- scala
package action
import org.apache.spark.{SparkConf, SparkContext}
/**
* @Author yqq
* @Date 2021/12/10 11:52
* @Version 1.0
*/
object CollectAsMapTest1 {
def main(args: Array[String]): Unit = {
val context = new SparkContext(
new SparkConf()
.setMaster("local")
.setAppName("CollectAsMapTest")
)
context.setLogLevel("Error")
context.parallelize(Array[(String,Int)](
("科比",24),
("科比",8),
("詹姆斯",23),
("詹姆斯",6),
("乔丹",23)
)).collectAsMap().foreach(println)
}
}
2. java
package action;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaSparkContext;
import scala.Tuple2;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
/**
* @Author yqq
* @Date 2021/12/10 11:41
* @Version 1.0
*/
public class CollectAsMapTest {
public static void main(String[] args) {
JavaSparkContext context = new JavaSparkContext(
new SparkConf()
.setMaster("local")
.setAppName("CollectAsMapTest")
);
context.setLogLevel("Error");
Map<String, Integer> map = context.parallelizePairs(Arrays.asList(
new Tuple2<>("科比", 24),
new Tuple2<>("科比", 8),
new Tuple2<>("詹姆斯", 23),
new Tuple2<>("詹姆斯", 6),
new Tuple2<>("乔丹", 23)
)).collectAsMap();
Set<Map.Entry<String,Integer>> set = map.entrySet();
set.forEach(e->{
String key = e.getKey();
Integer value = e.getValue();
System.out.println("key=:"+key+",value=:"+value);
});
}
}