我的大数据之旅-Spark Streaming wordcount

本文介绍了如何使用Spark Streaming创建一个简单的WordCount应用程序。首先,创建了一个Maven项目,并在打包时调整了项目包名。接着,详细讲解了WordCount类的实现。然后,将打包后的jar文件上传到服务器,并在服务器上运行该jar。最后,在终端通过nc命令发送数据,观察WordCount的实时处理效果。
摘要由CSDN通过智能技术生成

创建maven项目:

打包的时候注意修改成自己项目的包名。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.fengling</groupId>
    <artifactId>streaming</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <spark.version>2.1.1</spark.version>
        <scala.version>2.11.12</scala.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-streaming_2.11</artifactId>
            <version>${spark.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.11</artifactId>
            <version>${spark.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>${scala.version}</version>
            <!--<scope>provided</scope>-->
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <!--添加编译支持,都编译成java1.8版本-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <!--添加Scala编译支持-->
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.2.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>com.fengling.WordCount</mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

创建WordCount类

package com.fengling

import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Seconds, StreamingContext}

/**
  * @author fengchengliang@126.com
  * @date 2019-10-26
  */
object WordCount extends App {

  // 创建StreamingContext
  val conf = new SparkConf().setMaster("local[4]").setAppName("StreamingWordCount ;)")
  val ssc = new StreamingContext(conf, Seconds(1))

  // 创建DStream 连接到hadoop129:9999
  val lines = ssc.socketTextStream("hadoop129", 9999)

  // 每行输入按空格切分成一个个单词
  val words = lines.flatMap(_.split(" "))


  val pairs = words.map(word => (word, 1))
  val wordCount = pairs.reduceByKey((_ + _))

  wordCount.print()

  ssc.start()
  ssc.awaitTermination()

}

打jar包并上传到服务器。

运行jar

[fengling@hadoop129 spark-2.4.4-bin-hadoop2.7]$ bin/spark-submit --class com.fengling.WordCount ~/streaming-1.0-SNAPSHOT-jar-with-dependencies.jar

终端执行nc命令:

[fengling@hadoop129 ~]$ nc -lk 9999

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值