| /** |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package org.apache.falcon.example.spark; |
| |
| import org.apache.spark.SparkConf; |
| import org.apache.spark.api.java.JavaPairRDD; |
| import org.apache.spark.api.java.JavaRDD; |
| import org.apache.spark.api.java.JavaSparkContext; |
| import org.apache.spark.api.java.function.FlatMapFunction; |
| import org.apache.spark.api.java.function.Function2; |
| import org.apache.spark.api.java.function.PairFunction; |
| import scala.Tuple2; |
| |
| import java.util.Arrays; |
| |
| /** |
| * Spark Word Count example. |
| */ |
| public final class SparkWordCount { |
| |
| private SparkWordCount() { |
| } |
| protected static final FlatMapFunction<String, String> WORDS_EXTRACTOR = |
| new FlatMapFunction<String, String>() { |
| public Iterable<String> call(String s) throws Exception { |
| return Arrays.asList(s.split(" ")); |
| } |
| }; |
| |
| protected static final PairFunction<String, String, Integer> WORDS_MAPPER = |
| new PairFunction<String, String, Integer>() { |
| public Tuple2<String, Integer> call(String s) throws Exception { |
| return new Tuple2<String, Integer>(s, 1); |
| } |
| }; |
| |
| protected static final Function2<Integer, Integer, Integer> WORDS_REDUCER = |
| new Function2<Integer, Integer, Integer>() { |
| public Integer call(Integer a, Integer b) throws Exception { |
| return a + b; |
| } |
| }; |
| |
| public static void main(String[] args) { |
| if (args.length < 1) { |
| System.err.println("Please provide the input file full path as argument"); |
| System.exit(0); |
| } |
| |
| SparkConf conf = new SparkConf().setAppName("Java WordCount"); |
| JavaSparkContext context = new JavaSparkContext(conf); |
| JavaRDD<String> file = context.textFile(args[0]); |
| JavaRDD<String> words = file.flatMap(WORDS_EXTRACTOR); |
| JavaPairRDD<String, Integer> pairs = words.mapToPair(WORDS_MAPPER); |
| JavaPairRDD<String, Integer> counter = pairs.reduceByKey(WORDS_REDUCER); |
| counter.saveAsTextFile(args[1]); |
| } |
| } |