Event Stream System(1)Basic Setup

本文介绍如何安装配置ZooKeeper 3.4.10及Kafka 0.10.2.0,并演示创建主题、发送接收消息等基本操作。此外,还提供了一个使用Spark Streaming进行实时数据流处理的应用示例。
Event Stream System(1)Basic Setup

Install zookeeper 3.4.10 latest version
>wget http://apache.claz.org/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz
>tar zxvf zookeeper-3.4.10.tar.gz
>sudo ln -s ~/tool/zookeeper-3.4.10 /opt/zookeeper-3.4.10
>sudo ln -s /opt/zookeeper-3.4.10 /opt/zookeeper

Add to Path

>cp conf/zoo_sample.cfg conf/zoo.cfg

Start the Service
>zkServer.sh start conf/zoo.cfg

Connect to the Server
>zkCli.sh -server localhost:2181

Install latest stable Kafka version 2.10-10.20
>wget http://mirror.olnevhost.net/pub/apache/kafka/0.10.2.0/kafka_2.10-0.10.2.0.tgz
>tar zxvf kafka_2.10-0.10.2.0.tgz
Add to Path

Start the Service
>kafka-server-start.sh config/server.properties

Or start in the background
>nohup kafka-server-start.sh config/server.properties &

Create a topic test
>bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test

List all the topic
>bin/kafka-topics.sh --list --zookeeper localhost:2181

Produce some messages
>bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test

Consume the messages
>bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning

Spark stream
I use the latest package and I try to use the sample
<?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.sillycat</groupId>
<artifactId>sillycat-eventstream</artifactId>
<version>1.0</version>
<description>sillycat spark stream</description>
<name>event stream spark</name>
<packaging>jar</packaging>

<properties>
<springframework.version>4.3.7.RELEASE</springframework.version>
<logging.version>1.7.25</logging.version>
</properties>

<dependencies>
<!-- spark -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_2.11</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming-kafka-0-8_2.11</artifactId>
<version>2.1.0</version>
</dependency>
<!-- cache -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
<!-- logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${logging.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${logging.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- spring framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- apache -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<!-- testing -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${springframework.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.sillycat.sillycateventstream.ExecutorApp</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Sample Codes from WordCount
package com.sillycat.sillycateventstream.apps;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.spark.SparkConf;
import org.apache.spark.streaming.Duration;
import org.apache.spark.streaming.api.java.JavaDStream;
import org.apache.spark.streaming.api.java.JavaPairDStream;
import org.apache.spark.streaming.api.java.JavaPairReceiverInputDStream;
import org.apache.spark.streaming.api.java.JavaStreamingContext;
import org.apache.spark.streaming.kafka.KafkaUtils;

import scala.Tuple2;

/**
* Consumes messages from one or more topics in Kafka and does wordcount.
*
* Usage: JavaKafkaWordCount <zkQuorum> <group> <topics> <numThreads> <zkQuorum>
* is a list of one or more zookeeper servers that make quorum <group> is the
* name of kafka consumer group <topics> is a list of one or more kafka topics
* to consume from <numThreads> is the number of threads the kafka consumer
* should use
*
* To run this example: `$ bin/run-example
* org.apache.spark.examples.streaming.JavaKafkaWordCount zoo01,zoo02, \ zoo03
* my-consumer-group topic1,topic2 1`
*/
public class JavaKafkaWordCount implements Serializable{

private static final long serialVersionUID = -4598672873749563084L;

private static final Pattern SPACE = Pattern.compile(" ");

private JavaKafkaWordCount() {
}

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

SparkConf sparkConf = new SparkConf().setAppName("JavaKafkaWordCount").setMaster("local[2]");
// Create the context with 2 seconds batch size
JavaStreamingContext jssc = new JavaStreamingContext(sparkConf, new Duration(10 * 1000));

int numThreads = 1;
Map<String, Integer> topicMap = new HashMap<>();
topicMap.put("test", numThreads);

JavaPairReceiverInputDStream<String, String> messages = KafkaUtils.createStream(jssc, "fr-stage-consumer:2181",
"spark-group", topicMap);

JavaDStream<String> lines = messages.map( x->{
String value = x._2;
return value;
});


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

JavaPairDStream<String, Integer> wordCounts = words.mapToPair(s -> new Tuple2<>(s, 1)).reduceByKey((i1, i2) -> i1 + i2);

wordCounts.print();

jssc.start();
jssc.awaitTermination();
}

}


Cassandra
>wget http://www.gtlib.gatech.edu/pub/apache/cassandra/3.10/apache-cassandra-3.10-bin.tar.gz
Place and add to PATH

Command to start
>cassandra -Dcassandra.config="file:///opt/cassandra/conf/cassandra.yaml"


References:
http://sillycat.iteye.com/blog/2170328
http://docs.datastax.com/en/archived/cassandra/2.0/cassandra/tools/toolsCUtility_t.html
## crash date:2025-08-22 17:30:51 ## exe: MediaServer ## signal: 11 ## version: ZLMediaKit(git hash:/,branch:,build time:2025-08-14T16:01:23) ## stack: [0]: ./MediaServer() [0x596f94] sig_crash(int) System.cpp:? [1]: /usr/lib64/libc.so.6(+0x394f0) [0x7f143b81c4f0] ?? ??:0 [2]: ./MediaServer(_ZN8mediakit9RtpServer7getPortEv+0x1c) [0x70338c] mediakit::RtpServer::getPort() ??:? [3]: ./MediaServer(_Z13openRtpServertRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiS6_bj+0x1f6) [0x5b3566] openRtpServer(unsigned short, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool, unsigned int) ??:? [4]: ./MediaServer() [0x5b38bf] installWebApi()::{lambda(toolkit::SockInfo&, mediakit::StrCaseMap&, HttpAllArgs<std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, mediakit::StrCaseCompare, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > const&, Json::Value&)#29}::operator()(toolkit::SockInfo&, mediakit::StrCaseMap&, HttpAllArgs<std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, mediakit::StrCaseCompare, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > const&, Json::Value&) const [clone .isra.1917] WebApi.cpp:? [5]: ./MediaServer() [0x59b516] std::_Function_handler<void (toolkit::SockInfo&, mediakit::StrCaseMap&, HttpAllArgs<std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, mediakit::StrCaseCompare, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > const&, Json::Value&, mediakit::HttpResponseInvokerImp const&), toApi(std::function<void (toolkit::SockInfo&, mediakit::StrCaseMap&, HttpAllArgs<std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, mediakit::StrCaseCompare, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > const&, Json::Value&)> const&)::{lambda(toolkit::SockInfo&, mediakit::StrCaseMap&, HttpAllArgs<std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, mediakit::StrCaseCompare, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > const&, Json::Value&, mediakit::HttpResponseInvokerImp const&)#1}>::_M_invoke(std::_Any_data const&, toolkit::SockInfo&, mediakit::StrCaseMap&, HttpAllArgs<std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, mediakit::StrCaseCompare, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > const&, Json::Value&, mediakit::HttpResponseInvokerImp const&) WebApi.cpp:? [6]: ./MediaServer() [0x5b756c] toApi(std::function<void (toolkit::SockInfo&, mediakit::StrCaseMap&, HttpAllArgs<std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, mediakit::StrCaseCompare, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > const&, Json::Value&, mediakit::HttpResponseInvokerImp const&)> const&)::{lambda(mediakit::Parser const&, mediakit::HttpResponseInvokerImp const&, toolkit::SockInfo&)#1}::operator()(mediakit::Parser const&, mediakit::HttpResponseInvokerImp const&, toolkit::SockInfo&) const WebApi.cpp:? [7]: ./MediaServer() [0x5b6cb7] addHttpListener()::{lambda(mediakit::Parser const&, mediakit::HttpResponseInvokerImp const&, bool&, toolkit::SockInfo&)#2}::operator()(mediakit::Parser const&, mediakit::HttpResponseInvokerImp const&, bool&, toolkit::SockInfo&) const [clone .isra.2033] WebApi.cpp:? [8]: ./MediaServer(_ZN8mediakit11HttpSession13emitHttpEventEb+0x2c1) [0x68cb71] mediakit::HttpSession::emitHttpEvent(bool) ??:? [9]: ./MediaServer() [0x68cea2] std::_Function_handler<bool (char const*, unsigned long), mediakit::HttpSession::Handle_Req_POST(long&)::{lambda(char const*, unsigned long)#2}>::_M_invoke(std::_Any_data const&, char const*&&, unsigned long&&) HttpSession.cpp:? [10]: ./MediaServer(_ZN8mediakit11HttpSession13onRecvContentEPKcm+0x34) [0x688494] mediakit::HttpSession::onRecvContent(char const*, unsigned long) ??:? [11]: ./MediaServer(_ZN8mediakit19HttpRequestSplitter5inputEPKcm+0x248) [0x687bd8] mediakit::HttpRequestSplitter::input(char const*, unsigned long) ??:? [12]: ./MediaServer() [0x60e2a8] std::_Function_handler<void (std::shared_ptr<toolkit::Buffer> const&, sockaddr*, int), toolkit::TcpServer::onAcceptConnection(std::shared_ptr<toolkit::Socket> const&)::{lambda(std::shared_ptr<toolkit::Buffer> const&, sockaddr*, int)#1}>::_M_invoke(std::_Any_data const&, std::shared_ptr<toolkit::Buffer> const&, sockaddr*&&, int&&) TcpServer.cpp:? [13]: ./MediaServer(_ZN7toolkit6Socket6onReadERKSt10shared_ptrINS_6SockFDEEb+0x1a8) [0x603298] toolkit::Socket::onRead(std::shared_ptr<toolkit::SockFD> const&, bool) ??:? [14]: ./MediaServer() [0x60533f] std::_Function_handler<void (int), toolkit::Socket::attachEvent(std::shared_ptr<toolkit::SockFD> const&)::{lambda(int)#1}>::_M_invoke(std::_Any_data const&, int&&) Socket.cpp:? [15]: ./MediaServer(_ZN7toolkit11EventPoller7runLoopEbb+0x2b7) [0x621b07] toolkit::EventPoller::runLoop(bool, bool) ??:? [16]: /usr/lib64/libstdc++.so.6(+0xbc5ff) [0x7f143bbf55ff] ?? ??:0 [17]: /usr/lib64/libpthread.so.0(+0x8f1b) [0x7f143bccaf1b] ?? ??:0 [18]: /usr/lib64/libc.so.6(clone+0x40) [0x7f143b8db1c0] ?? ??:0 2025-08-22 17:30:52.570 W MediaServer[1270120-MediaServer] System.cpp:137 startDaemon | 子进程退出 2025-08-22 17:30:55.570 D MediaServer[1270120-MediaServer] System.cpp:127 startDaemon | 启动子进程:1272485 2025-08-22 17:30:55.571 I MediaServer[1272485-MediaServer] System.cpp:159 systemSetup | core文件大小设置为:18446744073709551615 2025-08-22 17:30:55.571 I MediaServer[1272485-MediaServer] System.cpp:168 systemSetup | 文件最大描述符个数设置为:524288 2025-08-22 17:30:55.571 I MediaServer[1272485-MediaServer] main.cpp:230 start_main | ZLMediaKit(git hash:/,branch:,build time:2025-08-14T16:01:23) 2025-08-22 17:30:55.604 D MediaServer[1272485-MediaServer] SSLBox.cpp:174 setContext | Add certificate of: default.zlmediakit.com 2025-08-22 17:30:55.604 D MediaServer[1272485-stamp thread] util.cpp:366 operator() | Stamp thread started 2025-08-22 17:30:55.620 I MediaServer[1272485-MediaServer] EventPoller.cpp:466 EventPollerPool | EventPoller created size: 32 2025-08-22 17:30:55.621 I MediaServer[1272485-MediaServer] TcpServer.cpp:200 start_l | TCP server listening on [::]: 554 2025-08-22 17:30:55.621 I MediaServer[1272485-MediaServer] TcpServer.cpp:200 start_l | TCP server listening on [::]: 1935 2025-08-22 17:30:55.622 I MediaServer[1272485-MediaServer] TcpServer.cpp:200 start_l | TCP server listening on [::]: 19350 2025-08-22 17:30:55.622 I MediaServer[1272485-MediaServer] TcpServer.cpp:200 start_l | TCP server listening on [::]: 8003 2025-08-22 17:30:55.623 I MediaServer[1272485-MediaServer] TcpServer.cpp:200 start_l | TCP server listening on [::]: 1443 2025-08-22 17:30:55.624 I MediaServer[1272485-MediaServer] UdpServer.cpp:104 start_l | UDP server bind to [::]: 8000 2025-08-22 17:30:55.625 I MediaServer[1272485-MediaServer] TcpServer.cpp:200 start_l | TCP server listening on [::]: 8000 2025-08-22 17:30:55.625 I MediaServer[1272485-MediaServer] UdpServer.cpp:104 start_l | UDP server bind to [::]: 9000 2025-08-22 17:30:55.625 I MediaServer[1272485-MediaServer] main.cpp:370 start_main | 已启动http api 接口 2025-08-22 17:30:55.626 I MediaServer[1272485-MediaServer] main.cpp:372 start_main | 已启动http hook 接口 2025-08-22 17:30:55.636 T MediaServer[1272485-event poller 1] HttpSession.cpp:27 HttpSession | 1-106(172.20.8.7:50610) 2025-08-22 17:30:55.637 D MediaServer[1272485-event poller 1] WebApi.cpp:250 http api debug | # request: POST /index/api/setServerConfig # header: Accept-Encoding : gzip Connection : close Content-Length : 1137 Content-Type : application/x-www-form-urlencoded Host : 172.20.8.7:8003 User-Agent : okhttp/4.9.0 # content: secret=035c73f7-bb6b-4889-a715-d9eb2d1925jl&hook.on_stream_changed=http%3A%2F%2F172.20.8.7%3A18087%2Fstream-server%2Findex%2Fhook%2Fon_stream_changed&hook.on_flow_report=&hook.on_publish=http%3A%2F%2F172.20.8.7%3A18087%2Fstream-server%2Findex%2Fhook%2Fon_publish&ffmpeg.cmd=%25s%20-fflags%20nobuffer%20-i%20%25s%20-c%3Aa%20aac%20-strict%20-2%20-ar%2044100%20-ab%2048k%20-c%3Av%20libx264%20%20-f%20flv%20%25s&hook.enable=1&hook.on_http_access=&hook.on_stream_not_found=http%3A%2F%2F172.20.8.7%3A18087%2Fstream-server%2Findex%2Fhook%2Fon_stream_not_found&rtp_proxy.port_range=30000-38700&hook.on_record_mp4=http%3A%2F%2F127.0.0.1%3A18081%2Fstream-storage%2Fapi%2Frecord%2Fon_record_mp4&hook.on_stream_none_reader=http%3A%2F%2F172.20.8.7%3A18087%2Fstream-server%2Findex%2Fhook%2Fon_stream_none_reader&hook.on_shell_login=http%3A%2F%2F172.20.8.7%3A18087%2Fstream-server%2Findex%2Fhook%2Fon_shell_login&hook.on_play=http%3A%2F%2F172.20.8.7%3A18087%2Fstream-server%2Findex%2Fhook%2Fon_play&api.secret=035c73f7-bb6b-4889-a715-d9eb2d1925jl&hook.on_server_started=http%3A%2F%2F172.20.8.7%3A18087%2Fstream-server%2Findex%2Fhook%2Fon_server_started # response: { "changed" : 0, "code" : 0 } 2025-08-22 17:30:55.637 T MediaServer[1272485-event poller 1] HttpSession.cpp:119 onError | 1-106(172.20.8.7:50610) close connection after send http body completed. 2025-08-22 17:30:55.637 T MediaServer[1272485-event poller 1] HttpSession.cpp:33 ~HttpSession | 1-106(172.20.8.7:50610) 2025-08-22 17:31:00.001 T MediaServer[1272485-event poller 1] HttpSession.cpp:27 HttpSession | 2-105(172.20.8.7:50624) 分析获取直播流时报错
08-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值