|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.examples.streaming; |
| 19 | + |
| 20 | +import java.util.regex.Pattern; |
| 21 | + |
| 22 | +import com.google.common.collect.Lists; |
| 23 | + |
| 24 | +import org.apache.spark.SparkConf; |
| 25 | +import org.apache.spark.SparkContext; |
| 26 | +import org.apache.spark.api.java.JavaRDD; |
| 27 | +import org.apache.spark.api.java.function.FlatMapFunction; |
| 28 | +import org.apache.spark.api.java.function.Function; |
| 29 | +import org.apache.spark.api.java.function.Function2; |
| 30 | +import org.apache.spark.sql.SQLContext; |
| 31 | +import org.apache.spark.sql.DataFrame; |
| 32 | +import org.apache.spark.api.java.StorageLevels; |
| 33 | +import org.apache.spark.streaming.Durations; |
| 34 | +import org.apache.spark.streaming.Time; |
| 35 | +import org.apache.spark.streaming.api.java.JavaDStream; |
| 36 | +import org.apache.spark.streaming.api.java.JavaReceiverInputDStream; |
| 37 | +import org.apache.spark.streaming.api.java.JavaStreamingContext; |
| 38 | + |
| 39 | +/** |
| 40 | + * Use DataFrames and SQL to count words in UTF8 encoded, '\n' delimited text received from the |
| 41 | + * network every second. |
| 42 | + * |
| 43 | + * Usage: JavaSqlNetworkWordCount <hostname> <port> |
| 44 | + * <hostname> and <port> describe the TCP server that Spark Streaming would connect to receive data. |
| 45 | + * |
| 46 | + * To run this on your local machine, you need to first run a Netcat server |
| 47 | + * `$ nc -lk 9999` |
| 48 | + * and then run the example |
| 49 | + * `$ bin/run-example org.apache.spark.examples.streaming.JavaSqlNetworkWordCount localhost 9999` |
| 50 | + */ |
| 51 | + |
| 52 | +public final class JavaSqlNetworkWordCount { |
| 53 | + private static final Pattern SPACE = Pattern.compile(" "); |
| 54 | + |
| 55 | + public static void main(String[] args) { |
| 56 | + if (args.length < 2) { |
| 57 | + System.err.println("Usage: JavaNetworkWordCount <hostname> <port>"); |
| 58 | + System.exit(1); |
| 59 | + } |
| 60 | + |
| 61 | + StreamingExamples.setStreamingLogLevels(); |
| 62 | + |
| 63 | + // Create the context with a 1 second batch size |
| 64 | + SparkConf sparkConf = new SparkConf().setAppName("JavaSqlNetworkWordCount"); |
| 65 | + JavaStreamingContext ssc = new JavaStreamingContext(sparkConf, Durations.seconds(1)); |
| 66 | + |
| 67 | + // Create a JavaReceiverInputDStream on target ip:port and count the |
| 68 | + // words in input stream of \n delimited text (eg. generated by 'nc') |
| 69 | + // Note that no duplication in storage level only for running locally. |
| 70 | + // Replication necessary in distributed scenario for fault tolerance. |
| 71 | + JavaReceiverInputDStream<String> lines = ssc.socketTextStream( |
| 72 | + args[0], Integer.parseInt(args[1]), StorageLevels.MEMORY_AND_DISK_SER); |
| 73 | + JavaDStream<String> words = lines.flatMap(new FlatMapFunction<String, String>() { |
| 74 | + @Override |
| 75 | + public Iterable<String> call(String x) { |
| 76 | + return Lists.newArrayList(SPACE.split(x)); |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + // Convert RDDs of the words DStream to DataFrame and run SQL query |
| 81 | + words.foreachRDD(new Function2<JavaRDD<String>, Time, Void>() { |
| 82 | + @Override |
| 83 | + public Void call(JavaRDD<String> rdd, Time time) { |
| 84 | + SQLContext sqlContext = JavaSQLContextSingleton.getInstance(rdd.context()); |
| 85 | + |
| 86 | + // Convert JavaRDD[String] to JavaRDD[bean class] to DataFrame |
| 87 | + JavaRDD<JavaRecord> rowRDD = rdd.map(new Function<String, JavaRecord>() { |
| 88 | + public JavaRecord call(String word) { |
| 89 | + JavaRecord record = new JavaRecord(); |
| 90 | + record.setWord(word); |
| 91 | + return record; |
| 92 | + } |
| 93 | + }); |
| 94 | + DataFrame wordsDataFrame = sqlContext.createDataFrame(rowRDD, JavaRecord.class); |
| 95 | + |
| 96 | + // Register as table |
| 97 | + wordsDataFrame.registerTempTable("words"); |
| 98 | + |
| 99 | + // Do word count on table using SQL and print it |
| 100 | + DataFrame wordCountsDataFrame = |
| 101 | + sqlContext.sql("select word, count(*) as total from words group by word"); |
| 102 | + System.out.println("========= " + time + "========="); |
| 103 | + wordCountsDataFrame.show(); |
| 104 | + return null; |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + ssc.start(); |
| 109 | + ssc.awaitTermination(); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +/** Lazily instantiated singleton instance of SQLContext */ |
| 114 | +class JavaSQLContextSingleton { |
| 115 | + static private transient SQLContext instance = null; |
| 116 | + static public SQLContext getInstance(SparkContext sparkContext) { |
| 117 | + if (instance == null) { |
| 118 | + instance = new SQLContext(sparkContext); |
| 119 | + } |
| 120 | + return instance; |
| 121 | + } |
| 122 | +} |
0 commit comments