热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

STORM入门之(TridentTopology集成Kafka)

基本前几篇文章是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> {
private int i = 0;
@Override
public Map init(Object o, TridentCollector tridentCollector) {
return new HashMap();
}

@Override
public void aggregate(Map stringIntegerMap, TridentTuple objects, TridentCollector tridentCollector) {
String location = objects.getString(0);
System.out.println(location);
}

@Override
public void complete(Map stringIntegerMap, TridentCollector collector) {
collector.emit(new Values(stringIntegerMap));
}
}




推荐阅读
author-avatar
扯淡的青春0707
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有