jstorm kafka外掛使用案例
本文用的是jstorm 2.2.1
一、pom引用
<?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>jiankunking</groupId>
<artifactId>kafkajstorm</artifactId>
<version>1.0-SNAPSHOT</version>
<url>http://blog.csdn.net/jiankunking</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<!--<scope>test</scope>-->
</dependency>
<!--jstorm begin-->
<dependency>
<groupId>com.alibaba.jstorm</groupId>
<artifactId>jstorm-core</artifactId>
<version>2.2.1</version>
<!--<scope>provided</scope>-->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.9.2</artifactId>
<version>0.8.1</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.5</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.5.0</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
<!--jstorm end-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.3</version>
</dependency>
<!--logback begin-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>1.7.10</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
</dependency>
<!--log end-->
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
二、自定義bolt
package jiankunking.kafkajstorm.bolts;
import backtype.storm.topology.BasicOutputCollector;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.base.BaseBasicBolt;
import backtype.storm.tuple.Tuple;
import backtype.storm.tuple.TupleImplExt;
import jiankunking.kafkajstorm.util.ByteUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.UnsupportedEncodingException;
/**
* Created by jiankunking on 2017/4/19 16:47.
*/
public class CustomBolt extends BaseBasicBolt {
protected final Logger logger = LoggerFactory.getLogger(CustomBolt.class);
public void execute(Tuple input, BasicOutputCollector collector) {
try {
String ss=ByteUtil.getStringFromByteArray((byte[]) ((TupleImplExt) input).get("bytes"));
System.out.println(ss);
logger.info(ss);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public void declareOutputFields(OutputFieldsDeclarer declarer) {
System.out.println("declareOutputFields");
}
}
三、自定義拓撲圖入口類
package jiankunking.kafkajstorm.topologies;
import backtype.storm.Config;
import backtype.storm.LocalCluster;
import backtype.storm.StormSubmitter;
import backtype.storm.generated.AlreadyAliveException;
import backtype.storm.generated.InvalidTopologyException;
import backtype.storm.topology.TopologyBuilder;
import com.alibaba.jstorm.client.ConfigExtension;
import jiankunking.kafkajstorm.bolts.CustomBolt;
import jiankunking.kafkajstorm.kafka.KafkaSpout;
import jiankunking.kafkajstorm.kafka.KafkaSpoutConfig;
import jiankunking.kafkajstorm.util.PropertiesUtil;
import java.util.Map;
/**
* Created by jiankunking on 2017/4/19 16:27.
* 拓撲圖 入口類
*/
public class CustomCounterTopology {
/**
* 入口類,即提交任務的類
*
* @throws InterruptedException
* @throws AlreadyAliveException
* @throws InvalidTopologyException
*/
public static void main(String[] args) throws AlreadyAliveException, InvalidTopologyException {
System.out.println("11111");
PropertiesUtil propertiesUtil = new PropertiesUtil("/application.properties", false);
Map propsMap = propertiesUtil.getAllProperty();
KafkaSpoutConfig spoutConfig = new KafkaSpoutConfig(propertiesUtil.getProps());
spoutConfig.configure(propsMap);
TopologyBuilder builder = new TopologyBuilder();
builder.setSpout("kafkaSpout", new KafkaSpout(spoutConfig));
builder.setBolt("customBolt", new CustomBolt(), 1).shuffleGrouping("kafkaSpout");
//Configuration
Config conf = new Config();
conf.setDebug(false);
//指定使用logback.xml
//需要把logback.xml檔案放到jstorm conf目錄下
ConfigExtension.setUserDefinedLogbackConf(conf, "%JSTORM_HOME%/conf/logback.xml");
if (args != null && args.length > 0) {
//提交到叢集執行
StormSubmitter.submitTopologyWithProgressBar("customCounterTopology", conf, builder.createTopology());
} else {
conf.setMaxTaskParallelism(3);
//本地模式執行
LocalCluster cluster = new LocalCluster();
cluster.submitTopology("CustomCounterTopology", conf, builder.createTopology());
}
}
}
四、配置檔案application.properties
# kafka
# kafka 消費組
kafka.client.id=kafkaspoutid
kafka.broker.partitions=4
kafka.fetch.from.beginning=false
kafka.topic=test_one
kafka.broker.hosts=10.10.10.10:9092
kafka.zookeeper.hosts=10.10.10.10:2181
storm.zookeeper.root=/kafka
小注:
1、jstorm kafka外掛原始碼整合
需要到jstorm的github官網:https://github.com/alibaba/jstorm/releases中找到你需要使用的release版本,下載原始碼,將其中的外掛原始碼整合到你自己的專案中,外掛原始碼位置如下圖:
2、logback的使用
jstorm 2.1.1之後,jstorm預設使用了logback作為日誌框架,logback在一般使用時是相容log4j的,也就是說log4j可以直接橋接到logback,具體為:
a. 新增slf4j-api, log4j-over-slf4j和logback依賴(其實加了logback依賴之後就不需要加slf4j-api依賴了),具體:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>1.7.10</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
</dependency>
b. 排除pom中所有的slf4j-log4j12的依賴,因為slf4j-log4j12跟log4j-over-slf4j是衝突的:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
<scope>provided</scope>
</dependency>
這裡版本一般是1.7.5,但是還要具體看你的應用pom仲裁出的版本。
理論上,這樣就能夠把log4j橋接到slf4j。
demo下載地址:
http://download.csdn.net/detail/xunzaosiyecao/9829079
https://github.com/JianKunKing/jstorm-kafka-plugin-demo
作者:jiankunking 出處:http://blog.csdn.net/jiankunking
相關文章
- storm kafka外掛使用案例ORMKafka
- Eclipse安裝lombok外掛及外掛使用案例EclipseLombok
- easyui下面layout外掛的使用案例UI
- 安裝nginx-kafka外掛NginxKafka
- kafka+jstorm版本升級安裝(三)KafkaJSORM
- babel 外掛開發案例Babel
- apisix~kafka-logger外掛APIKafka
- PHP外掛系統的實現(七):外掛案例PHP
- 使用PlupLoad js外掛進行檔案上傳案例JS
- Headshot外掛如何使用?Headshot外掛使用教程
- fastadmin的【外掛管理】外掛使用教程AST
- alpakka-kafka(6)-kafka應用案例,使用者介面Kafka
- JQuery外掛:圖片上傳本地預覽外掛,改進案例一則。jQuery
- HtmlWebpackPlugin外掛和HtmlWebpackInlineSourcePlugin外掛的使用HTMLWebPlugininline
- Head 外掛使用
- PhotoSwipe外掛的使用
- 個人使用外掛合集
- 使用Autodesk Vault外掛嚮導輕鬆建立Vault外掛
- kafka入門案例Kafka
- Flutter個人外掛使用整理Flutter
- videojs外掛使用IDEJS
- IDEA使用lombok外掛IdeaLombok
- 使用podspec建立iOS外掛iOS
- Vue-router外掛使用Vue
- burp外掛的使用
- Sublime使用,安裝外掛
- 我使用的brackets外掛Racket
- flask 外掛使用方法Flask
- apisix~限流外掛的使用API
- Chrome 外掛特性及實戰場景案例分析Chrome
- [外掛擴充套件]違規資訊使用者舉報外掛套件
- [需求建議]外掛外部url控制器直接使用外掛配置
- 前端外掛之Datatables使用--下篇前端
- 前端外掛之Datatables使用--上篇前端
- 使用 VSCode Remote 外掛VSCodeREM
- Windows下使用ideagit外掛WindowsIdeaGit
- vscode外掛使用包VSCode
- vim外掛 NERDTree安裝使用