作者:扯淡的青春0707 | 来源:互联网 | 2023-08-11 09:45
基本前几篇文章是StormTopology集成Kafka是Storm基本入门知识传送门:http:blog.csdn.netyl3395017articledetails7747771
基本
前几篇文章是StormTopology集成Kafka 是Storm基本入门知识 传送门:http://blog.csdn.net/yl3395017/article/details/77477716
这篇介绍TridentTopology集成Kafka 话不多说 上代码
Topology构建
package com.storm.topology;
import com.storm.trident.LogAggr;
import com.storm.trident.OutPrint;
import org.apache.storm.Config;
import org.apache.storm.LocalCluster;
import org.apache.storm.StormSubmitter;
import org.apache.storm.generated.AlreadyAliveException;
import org.apache.storm.generated.AuthorizationException;
import org.apache.storm.generated.InvalidTopologyException;
import org.apache.storm.generated.StormTopology;
import org.apache.storm.kafka.BrokerHosts;
import org.apache.storm.kafka.StringScheme;
import org.apache.storm.kafka.ZkHosts;
import org.apache.storm.kafka.trident.OpaqueTridentKafkaSpout;
import org.apache.storm.kafka.trident.TridentKafkaConfig;
import org.apache.storm.spout.SchemeAsMultiScheme;
import org.apache.storm.trident.TridentTopology;
import org.apache.storm.tuple.Fields;
public class TridentKafkaTopology {
public static void main(String[] args) throws AlreadyAliveException,InvalidTopologyException, AuthorizationException {
TridentTopology topology = new TridentTopology();
BrokerHosts brokerHosts = new ZkHosts("10.2.4.12:2181,10.2.4.13:2181,10.2.4.14:2181");
TridentKafkaConfig kafkaCOnfig= new TridentKafkaConfig(brokerHosts, "test_rce_yjd");
kafkaConfig.scheme = new SchemeAsMultiScheme(new StringScheme());
OpaqueTridentKafkaSpout opaqueTridentKafkaSpout = new OpaqueTridentKafkaSpout(kafkaConfig);
topology.newStream("kafka-trident", opaqueTridentKafkaSpout)
.parallelismHint(3)
.aggregate(new Fields("str"),new LogAggr(),new Fields("sysout"));
StormTopology stormTopology = topology.build();
LocalCluster cluster = new LocalCluster();
Config cOnfig= new Config();
config.setDebug(false);
cluster.submitTopology("test", config,stormTopology);
}
}
LogAggr聚合类
package com.storm.trident;
import org.apache.storm.trident.operation.BaseAggregator;
import org.apache.storm.trident.operation.TridentCollector;
import org.apache.storm.trident.tuple.TridentTuple;
import org.apache.storm.tuple.Values;
import java.util.HashMap;
import java.util.Map;
/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 17-9-1
* Time: 下午2:26
* To change this template use File | Settings | File Templates.
*/
public class LogAggr extends BaseAggregator