<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                實現:接收TCPSocket的單詞,并統計單詞數量。 (1)編寫Streaming程序 ```scala import org.apache.spark.storage.StorageLevel import org.apache.spark.streaming.dstream.{DStream, ReceiverInputDStream} import org.apache.spark.{SparkConf} import org.apache.spark.streaming.{Seconds, StreamingContext} object NetworkWordCount { def main(args: Array[String]): Unit = { /** ************ 1. 創建StreamingContext **************/ // local[n]中的n要大于接收器的個數. 即要n大于JVM線程的個數,每個線程都會監聽下面的9999端口. val conf: SparkConf = new SparkConf().setMaster("local[4]").setAppName(this.getClass.getName) val ssc: StreamingContext = new StreamingContext(conf, Seconds(5)) /** ************ 2. 加載數據源 **************/ // socketTextStream(hostname, port, StorageLevel) val lines: ReceiverInputDStream[String] = ssc.socketTextStream("hadoop101", 9999, StorageLevel.MEMORY_AND_DISK_SER) /** ************ 3. 進行統計 **************/ val words: DStream[String] = lines.flatMap(_.split(" ")) val wordCounts: DStream[(String, Int)] = words.map(x => (x, 1)).reduceByKey(_ + _) wordCounts.print() // 打印輸出 /** ************ 4. 啟動Streaming程序 **************/ ssc.start() /** *********** 5. 等待應用程序終止 ****************/ // 或者調用ssc.stop()直接停止當前的Streaming程序 ssc.awaitTermination() } } ``` (2)先啟動`nc`,再啟動上面的Streaming程序。 在Linux終端啟動`nc`,并輸入一些單詞。 ```shell [root@hadoop101 /]# nc -lk 9999 hello spark hello scala kafka kafka ``` 如果在Linux中沒有安裝`nc`,運行如下命令安裝: ```shell yum -y install nc ``` (3)啟動上面的Streaming程序,控制臺輸出如下: ```txt ------------------------------------------- Time: 1611129350000 ms ------------------------------------------- (spark,1) (scala,1) (hello,2) (kafka,2) ``` (4)再次在`nc`中輸入單詞,Streaming程序會監控并收到數據進行統計 ```shell [root@hadoop101 /]# nc -lk 9999 hello spark hello scala kafka kafka sparkstreaming sparkstreaming wordcount ``` Streaming程序打印出如下信息: ```txt ------------------------------------------- Time: 1611129495000 ms ------------------------------------------- (wordcount,1) (sparkstreaming,2) ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看