• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

littlesuccess

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

Spark Streaming之旅

1. 打开spark-shell

2. 建立StreamingContext
import org.apache.spark.streaming._
import org.apache.spark.streaming.StreamingContext._
import org.apache.spark.api.java.function._
import org.apache.spark.streaming._
import org.apache.spark.streaming.api._
// Create a StreamingContext with a local master
val ssc = new StreamingContext(sc, Seconds(1))

3.用StreamingContext建立一个DStream

val lines = ssc.socketTextStream("localhost", 9999)

4.运算

// Split each line into words
val words = lines.flatMap(_.split(" "))

import org.apache.spark.streaming.StreamingContext._
// Count each word in each batch
val pairs = words.map(word => (word, 1))
val wordCounts = pairs.reduceByKey(_ + _)

// Print a few of the counts to the console
wordCounts.print()

5.启动DStream

ssc.start()             // Start the computation
ssc.awaitTermination()  // Wait for the computation to terminate

6.启动一个新控制台,并执行如下命令

$ nc -lk 9999

7.在新控制台上输入任意语句,并在spark-shell窗口观察输出

hello world how are you hello are you there

you are the son of a bitch

 



posted on 2014-06-18 11:15  littlesuccess  阅读(345)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3