package kr.co.swh.lecture.opensource.apache.spark; 

import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.sql.SparkSession;

import scala.Tuple2;

public final class JavaWordCount {
	private static final Pattern SPACE = Pattern.compile(" ");

	public static void main(String[] args) throws Exception {

		String text = "sadfasdf sdfsdfasd fdsfsdf asdfasdf sdfasdf sadfdf";
		
		SparkConf conf = new SparkConf();
		conf.setMaster("local[2]");
//		SparkSession spark = new SparkSession(new SparkConf().setAppName("Spark WordCount").setMaster("local[2]"));
		SparkSession spark = SparkSession
				.builder()
				.appName("JavaWordCount")
//				.config(conf);
				.getOrCreate();

		JavaRDD<String> lines = spark.read().textFile(text).javaRDD();

		JavaRDD<String> words = lines.flatMap(s -> Arrays.asList(SPACE.split(s)).iterator());

		JavaPairRDD<String, Integer> ones = words.mapToPair(s -> new Tuple2<>(s, 1));

		JavaPairRDD<String, Integer> counts = ones.reduceByKey((i1, i2) -> i1 + i2);

		List<Tuple2<String, Integer>> output = counts.collect();
		for (Tuple2<?,?> tuple : output) {
			System.out.println(tuple._1() + ": " + tuple._2());
		}
		spark.stop();
	}
}
