Spark Learning six: Spark streaming

Source: Internet
Author: User
Tags hdfs dfs

Spark Learning six: Spark streaming

tags (space delimited): Spark

    • Spark learning six spark streaming
      • An overview
      • Case study of two enterprises
      • How the three spark streaming works
      • Application of four textfilestreaming
      • How to develop in four enterprises
      • Five summary

One, overview

A simple example
1, installing NC

-lk9999

2. Start the application

./bin/run-example streaming.NeworkWordCount9999
Second, enterprise case analysis

Demand:
Real-time statistics on the last two hours of website access,
PV,UV, Region

5 minutes to execute once

10:00
8:00-10:00 Window 24
10:05
8:05-10:05 Window 24

StreamingContext(sc, Mintuns(5))DStream.window(Mintuns(24 * 5), Mintuns(2 * 5))

Code implementation

<dependency>    <groupId>org.apache.spark</groupId>    <artifactId>spark-streaming_2.10</artifactId>    <version>1.3.0</version></dependency>

How to execute a Scala code in Spark-shell

> :load /opt/cdh5.3.6/spark-1.3.0-bin-2.5.0-cdh5.3.6/HdfsWordCount.scala
Third, how Spark streaming works

Four, the application of textfilestreaming

1, prepare the data

bin/hdfs dfs -put wordcount.txt /spark/streaming

2. Launch the Spark app

bin/spark-shell--master local[2]

3, writing code

import org. Apache. Spark. _import org. Apache. Spark. Streaming. _import org. Apache. Spark. Streaming. StreamingContext. _val SSC = new StreamingContext (SC, Seconds ( -)) Val lines = SSC. Textfilestream("Hdfs://study.com.cn:8020/myspark") Val words = lines. FlatMap(_. Split(",")) Val pairs = words. Map(Word = Word,1)) Val wordcounts = pairs. Reducebykey(_ + _) wordcounts. Print() SSC. Start() SSC. Awaittermination()

4, test results

Four, the development of enterprises in the way:

1,idea encoding, playing into jar package execution
2, put it in the script and execute it.

touch test.scala
import org. Apache. Spark. _import org. Apache. Spark. Streaming. _import org. Apache. Spark. Streaming. StreamingContext. _val SSC = new StreamingContext (SC, Seconds ( -)) Val lines = SSC. Textfilestream("Hdfs://study.com.cn:8020/myspark") Val words = lines. FlatMap(_. Split(",")) Val pairs = words. Map(Word = Word,1)) Val wordcounts = pairs. Reducebykey(_ + _) wordcounts. Print() SSC. Start() SSC. Awaittermination()
:load /opt/app/spark-1.3.0-bin-2.5.0/test/test.scala
Five, summary

Spark Learning six: Spark streaming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.