spark streaming是怎么接受socket数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spark streaming是怎么接受socket数据相关的知识,希望对你有一定的参考价值。
scala版本
package org.apache.spark.examples.streamingimport org.apache.spark.SparkConf
import org.apache.spark.streaming.Seconds, StreamingContext
import org.apache.spark.storage.StorageLevel
object NetworkWordCount
def main(args: Array[String])
if (args.length < 2)
System.err.println("Usage: NetworkWordCount <hostname> <port>")
System.exit(1)
StreamingExamples.setStreamingLogLevels()
// Create the context with a 1 second batch size
val sparkConf = new SparkConf().setAppName("NetworkWordCount")
val ssc = new StreamingContext(sparkConf, Seconds(1))
// Create a socket stream on target ip:port and count the
// words in input stream of \\n delimited text (eg. generated by 'nc')
// Note that no duplication in storage level only for running locally.
// Replication necessary in distributed scenario for fault tolerance.
val lines = ssc.socketTextStream(args(0), args(1).toInt, StorageLevel.MEMORY_AND_DISK_SER)
val words = lines.flatMap(_.split(" "))
val wordCounts = words.map(x => (x, 1)).reduceByKey(_ + _)
wordCounts.print()
ssc.start()
ssc.awaitTermination()
java版本
package org.apache.spark.examples.streaming;import scala.Tuple2;
import com.google.common.collect.Lists;
import org.apache.spark.SparkConf;
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 org.apache.spark.api.java.StorageLevels;
import org.apache.spark.streaming.Durations;
import org.apache.spark.streaming.api.java.JavaDStream;
import org.apache.spark.streaming.api.java.JavaPairDStream;
import org.apache.spark.streaming.api.java.JavaReceiverInputDStream;
import org.apache.spark.streaming.api.java.JavaStreamingContext;
import java.util.regex.Pattern;
public final class JavaNetworkWordCount
private static final Pattern SPACE = Pattern.compile(" ");
public static void main(String[] args)
if (args.length < 2)
System.err.println("Usage: JavaNetworkWordCount <hostname> <port>");
System.exit(1);
StreamingExamples.setStreamingLogLevels();
// Create the context with a 1 second batch size
SparkConf sparkConf = new SparkConf().setAppName("JavaNetworkWordCount");
JavaStreamingContext ssc = new JavaStreamingContext(sparkConf, Durations.seconds(1));
// Create a JavaReceiverInputDStream on target ip:port and count the
// words in input stream of \\n delimited text (eg. generated by 'nc')
// Note that no duplication in storage level only for running locally.
// Replication necessary in distributed scenario for fault tolerance.
JavaReceiverInputDStream<String> lines = ssc.socketTextStream(
args[0], Integer.parseInt(args[1]), StorageLevels.MEMORY_AND_DISK_SER);
JavaDStream<String> words = lines.flatMap(new FlatMapFunction<String, String>()
@Override
public Iterable<String> call(String x)
return Lists.newArrayList(SPACE.split(x));
);
JavaPairDStream<String, Integer> wordCounts = words.mapToPair(
new PairFunction<String, String, Integer>()
@Override
public Tuple2<String, Integer> call(String s)
return new Tuple2<String, Integer>(s, 1);
).reduceByKey(new Function2<Integer, Integer, Integer>()
@Override
public Integer call(Integer i1, Integer i2)
return i1 + i2;
);
wordCounts.print();
ssc.start();
ssc.awaitTermination();
我之前是将Spark作业以yarn cluster模式提交到Yarn,由Yarn启动Spark作业,在某个子节点的Executor会监听该端口,接收数据。
参考技术A package org.apache.spark.examples.streamingimport org.apache.spark.SparkConf
import org.apache.spark.streaming.Seconds, StreamingContext
import org.apache.spark.storage.StorageLevel
object NetworkWordCount
def main(args: Array[String])
if (args.length < 2)
System.err.println("Usage: NetworkWordCount <hostname> <port>")
System.exit(1)
StreamingExamples.setStreamingLogLevels()
// Create the context with a 1 second batch size
val sparkConf = new SparkConf().setAppName("NetworkWordCount")
val ssc = new StreamingContext(sparkConf, Seconds(1))
// Create a socket stream on target ip:port and count the
// words in input stream of \n delimited text (eg. generated by 'nc')
// Note that no duplication in storage level only for running locally.
// Replication necessary in distributed scenario for fault tolerance.
val lines = ssc.socketTextStream(args(0), args(1).toInt, StorageLevel.MEMORY_AND_DISK_SER)
val words = lines.flatMap(_.split(" "))
val wordCounts = words.map(x => (x, 1)).reduceByKey(_ + _)
wordCounts.print()
ssc.start()
ssc.awaitTermination()
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package org.apache.spark.examples.streaming;
import scala.Tuple2;
import com.google.common.collect.Lists;
import org.apache.spark.SparkConf;
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 org.apache.spark.api.java.StorageLevels;
import org.apache.spark.streaming.Durations;
import org.apache.spark.streaming.api.java.JavaDStream;
import org.apache.spark.streaming.api.java.JavaPairDStream;
import org.apache.spark.streaming.api.java.JavaReceiverInputDStream;
import org.apache.spark.streaming.api.java.JavaStreamingContext;
import java.util.regex.Pattern;
public final class JavaNetworkWordCount
private static final Pattern SPACE = Pattern.compile(" ");
public static void main(String[] args)
if (args.length < 2)
System.err.println("Usage: JavaNetworkWordCount <hostname> <port>");
System.exit(1);
StreamingExamples.setStreamingLogLevels();
// Create the context with a 1 second batch size
SparkConf sparkConf = new SparkConf().setAppName("JavaNetworkWordCount");
JavaStreamingContext ssc = new JavaStreamingContext(sparkConf, Durations.seconds(1));
// Create a JavaReceiverInputDStream on target ip:port and count the
// words in input stream of \n delimited text (eg. generated by 'nc')
// Note that no duplication in storage level only for running locally.
// Replication necessary in distributed scenario for fault tolerance.
JavaReceiverInputDStream<String> lines = ssc.socketTextStream(
args[0], Integer.parseInt(args[1]), StorageLevels.MEMORY_AND_DISK_SER);
JavaDStream<String> words = lines.flatMap(new FlatMapFunction<String, String>()
@Override
public Iterable<String> call(String x)
return Lists.newArrayList(SPACE.split(x));
);
JavaPairDStream<String, Integer> wordCounts = words.mapToPair(
new PairFunction<String, String, Integer>()
@Override
public Tuple2<String, Integer> call(String s)
return new Tuple2<String, Integer>(s, 1);
).reduceByKey(new Function2<Integer, Integer, Integer>()
@Override
public Integer call(Integer i1, Integer i2)
return i1 + i2;
);
wordCounts.print();
ssc.start();
ssc.awaitTermination();
我之前是将Spark作业以yarn cluster模式提交到Yarn,由Yarn启动Spark作业,在某个子节点的Executor会监听该端口,接收数据。
以上是关于spark streaming是怎么接受socket数据的主要内容,如果未能解决你的问题,请参考以下文章
Ephemeral Spark Streaming..以编程方式关闭