服务器代码:
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.Random;
public class Socket_server {
public static void main(String[] args) throws Exception {
ServerSocket sc = new ServerSocket(9990);
while (true) {
Socket socket = sc.accept();
java.io.OutputStream out = socket.getOutputStream();
String message = getRandomIntegerBetweenRange(100, 120) + "";
byte b[] = message.getBytes(Charset.defaultCharset());
out.write(b);
out.close();
socket.close();
}
}
private static double getRandomIntegerBetweenRange(double max, double min) {
double x = (int) (Math.random() * ((max - min) + 1)) + min;
return x;
}
}
Spark代码:
import java.util.Collections;
import org.apache.avro.ipc.specific.Person;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.function.MapFunction;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Encoder;
import org.apache.spark.sql.Encoders;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.streaming.StreamingQuery;
import org.apache.spark.sql.streaming.Trigger;
import scala.Function1;
public class App1 {
public static void main(String[] args) throws Exception {
SparkConf conf = new SparkConf();
conf.setMaster("local[*]");
conf.setAppName("app");
SparkSession spark = SparkSession.builder().config(conf).getOrCreate();
Dataset<Row> lines = spark.readStream().format("socket").option("host", "localhost").option("port", 9990)
.load();
StreamingQuery query = lines.writeStream().format("console").start();
query.awaitTermination();
}
}
我正在运行生成随机值的服务器代码,之后我运行spark Structured Streaming代码来读取它并从中创建DataFrame。但是当我的火花代码启动时,它只是从服务器读取第一个值,之后它不再读取任何值。当我使用同一个服务器和火花流时,那就是连续读取值。所以任何人都可以帮助解决代码的问题。