-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordCount.java
39 lines (33 loc) · 1.21 KB
/
WordCount.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package base;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.sql.SparkSession;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class WordCount {
public static final List<String> DATA = Stream
.of("Abc", "Abcdef", "bC", "Dd","eC", "dD", "Abcdef", "bC", "bC", "Dd","eC", "dD", "Abcdef", "bC")
.collect(Collectors.toList());
public static void main(String[] args) {
SparkSession spark = SparkSession
.builder()
.appName("test")
.master("local")
.getOrCreate();
JavaSparkContext javaSparkContext = new JavaSparkContext(spark.sparkContext());
Map<String, Integer> res = javaSparkContext
.parallelize(DATA)
.groupBy(s -> s)
.mapValues(strings -> {
int count = 0;
for (String s : strings) {
count++;
}
return count;
})
.collectAsMap();
res.forEach((k, v) -> System.out.println(k + "\t" + v));
spark.close();
}
}