gpt4 book ai didi

java - 如何使用套接字实现 Spark 流式输出

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:01:16 25 4
gpt4 key购买 nike

我一直在尝试用 Java 实现这个:

dstream.foreachRDD { rdd =>
rdd.foreachPartition { partitionOfRecords =>
val connection = createNewConnection()
partitionOfRecords.foreach(record => connection.send(record))
connection.close()
}
}

Spark 文档提供的示例类型。以下按预期工作:

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 org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.streaming.Time;

import java.util.regex.Pattern;
import java.io.IOException;

/**
* Counts words in UTF8 encoded, '\n' delimited text received from the network every second.
*
* Usage: JavaNetworkWordCount <hostname> <port>
* <hostname> and <port> describe the TCP server that Spark Streaming would connect to receive data.
*
* To run this on your local machine, you need to first run a Netcat server
* `$ nc -lk 9999`
* and then run the example
* `$ bin/run-example org.apache.spark.examples.streaming.JavaNetworkWordCount localhost 9999`
*/
public final class SocketWriter {
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);
}

// 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.foreachRDD(new Function2<JavaPairRDD<String, Integer>, Time, Void>() {
@Override
public Void call(JavaPairRDD<String, Integer> rdd, Time time) throws IOException {
String counts = "Counts at time " + time + " " + rdd.collect();
System.out.println(counts);
return null;
}
});

ssc.start();
ssc.awaitTermination();
}
}

但我需要能够通过修改此部分以使用此问题顶部在 Scala 中指定的“设计模式”来将数据输出到套接字。

wordCounts.foreachRDD(new Function2<JavaPairRDD<String, Integer>, Time, Void>() {
@Override
public Void call(JavaPairRDD<String, Integer> rdd, Time time) throws IOException {
String counts = "Counts at time " + time + " " + rdd.collect();
System.out.println(counts);
return null;
}
});

我尝试在这里使用 Socket 和 PrintWriter 对象,但无法使其工作,而且我找不到任何人这样做的例子。感谢您的帮助。

最佳答案

我只是向您展示问题,因为我正在尝试做同样的事情,最后我做到了!对你来说可能为时已晚,但希望对许多其他人来说不会。

作为here in the official documentation说,我没有以最佳方式完成它,这是使用连接池,因此 Spark 不必为每个 RDD 打开和关闭连接,但仍在工作,这是我的代码:

wordCounts.foreachRDD(new VoidFunction<JavaRDD<String>>() {
public void call(JavaRDD<String> rdd) throws Exception {
rdd.foreachPartition(new VoidFunction<Iterator<String>>() {
public void call(Iterator<String> partitionOfRecords) throws Exception {
Socket mySocket = new Socket("localhost", 9998);
final PrintWriter out = new PrintWriter(mySocket.getOutputStream(), true);
while(partitionOfRecords.hasNext()) {
out.println(partitionOfRecords.next());
}
mySocket.close();
}
});
}
});

关于java - 如何使用套接字实现 Spark 流式输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28921329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com