Spark Stream怎么使用

发布时间:2021-12-17 10:30:49 作者:iii
来源:亿速云 阅读:156

这篇文章主要讲解了“Spark Stream怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spark Stream怎么使用”吧!

pom.xml
<dependency>

  1.     <groupId>org.apache.spark</groupId>

  2.     <artifactId>spark-streaming_2.11</artifactId>

  3.     <version>2.1.0</version>

  4. </dependency>

  1. import java.io.OutputStream;

  2. import java.net.ServerSocket;

  3. import java.net.Socket;

  4. import java.util.Arrays;

  5. import java.util.UUID;


  6. import org.apache.spark.SparkConf;

  7. import org.apache.spark.streaming.Durations;

  8. import org.apache.spark.streaming.api.java.JavaDStream;

  9. import org.apache.spark.streaming.api.java.JavaPairDStream;

  10. import org.apache.spark.streaming.api.java.JavaStreamingContext;


  11. import scala.Tuple2;


  12. public class SparkStreamTest {


  13.     public static void main(String[] args) throws Exception {

  14.         startSockerServer(9999);

  15.         

  16.         SparkConf conf = new SparkConf().setMaster("local[2]").setAppName("NetworkWordCount");

  17.         JavaStreamingContext jssc = new JavaStreamingContext(conf, Durations.seconds(1));

  18.         JavaDStream<String> lines = jssc.socketTextStream("localhost", 9999);


  19.         JavaDStream<String> words = lines.flatMap(x -> Arrays.asList(x.split(" ")).iterator());

  20.         JavaPairDStream<String, Integer> pairs = words.mapToPair(s -> new Tuple2<>(s, 1));

  21.         JavaPairDStream<String, Integer> counts = pairs.reduceByKey((x, y) -> x + y);

  22.         counts.print();

  23.         

  24.         jssc.start();

  25.         jssc.awaitTermination();

  26.         jssc.close();

  27.     }


  28.     private static void startSockerServer(int port) throws Exception {

  29.         new Thread(){

  30.             public void run() {

  31.                 try {

  32.                     ServerSocket server = new ServerSocket(port);

  33.                     Socket socket = server.accept();

  34.                     OutputStream os = socket.getOutputStream();

  35.                     for (int i = 0; i < 1000; i++) {

  36.                         String txt = UUID.randomUUID().toString().replaceAll("-", " ");

  37.                         txt = "Hello test\n";

  38.                         os.write(txt.getBytes());

  39.                         Thread.sleep(10);

  40.                     }

  41.                     socket.close();

  42.                     server.close();

  43.                 } catch (Exception e) {

  44.                     e.printStackTrace();

  45.                 }

  46.             }

  47.         }.start();

  48.     }

  49. }

会不断地打印出类似的结果:
-------------------------------------------
Time: 1490947248000 ms
-------------------------------------------
(Hello,99)
(test,99)

感谢各位的阅读,以上就是“Spark Stream怎么使用”的内容了,经过本文的学习后,相信大家对Spark Stream怎么使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. Spark Streaming反压机制探秘
  2. Spark Streaming 技术点汇总

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spark stream

上一篇:Ceph软件架构是怎么样的

下一篇:python匿名函数怎么创建

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》