Guest User

Untitled

a guest
May 28th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.05 KB | None | 0 0
  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  *   http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */
  19.  
  20. import org.apache.nemo.compiler.frontend.spark.core.rdd.JavaPairRDD;
  21. import org.apache.nemo.compiler.frontend.spark.core.rdd.JavaRDD;
  22. import org.apache.nemo.compiler.frontend.spark.sql.SparkSession;
  23. import scala.Tuple2;
  24.  
  25. import java.util.Arrays;
  26. import java.util.regex.Pattern;
  27.  
  28. /**
  29.  * Java Wordcount example.
  30.  */
  31. public final class JavaWordCount {
  32.   private static final Pattern SPACE = Pattern.compile(" ");
  33.  
  34.   /**
  35.    * Private constructor.
  36.    */
  37.   private JavaWordCount() {
  38.   }
  39.  
  40.   /**
  41.    * Main method.
  42.    *
  43.    * @param args arguments.
  44.    * @throws Exception exceptions.
  45.    */
  46.   public static void main(final String[] args) throws Exception {
  47.  
  48.     String file = "/home/jedartois/dev/JD-Spark-WordCount/input.txt";
  49.  
  50.     SparkSession spark = SparkSession
  51.       .builder()
  52.       .appName("JavaWordCount")
  53.       .getOrCreate();
  54.  
  55.     JavaRDD<String> lines = spark.read().textFile(file).toJavaRDD();
  56.  
  57.     JavaRDD<String> words = lines.flatMap(s -> Arrays.asList(SPACE.split(s)).iterator());
  58.  
  59.     JavaPairRDD<String, Integer> ones = words.mapToPair(s -> new Tuple2<>(s, 1));
  60.  
  61.     JavaPairRDD<String, Integer> counts = ones.reduceByKey((i1, i2) -> i1 + i2);
  62.  
  63.  
  64.     counts.saveAsTextFile("results");
  65.  
  66.     spark.stop();
  67.   }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment