转换Storm - wordCount拓扑以使用Kafka Spout

时间:2014-07-11 20:36:33

标签: java apache-storm topology

我是Storm和Kafka的新手,我可以设法在一段时间后在本地虚拟机上安装它们。我目前有一个工作wordCount拓扑从dropBox文本文件中获取句子:

public void nextTuple() {


 final String APP_KEY = "XXXX";
final String APP_SECRET = "XXXX";
DbxAppInfo appInfo = new DbxAppInfo(APP_KEY, APP_SECRET);
DbxRequestConfig config = new DbxRequestConfig("StormTopology/1.0", Locale.getDefault().toString());
String accessToken = "XXXXXXXX";
DbxClient client = new DbxClient(config, accessToken);
String sentence="";
try {FileOutputStream outputStream = new FileOutputStream("fromSpout.txt"); 
try {
    //client.delete("/*.txt");
   DbxEntry.File downloadedFile = client.getFile("/spout.txt", null,outputStream);

   sentence= readFile("fromSpout.txt");          
   if (sentence==null || sentence == "" || sentence == " " || sentence == "/t") {
           Utils.sleep(1000);
           return;
           }                    
        } 
catch (DbxException ex) {  } 
catch (IOException ex) { }       
        //return 1;
finally {
      outputStream.close();
         }
    }
catch (FileNotFoundException ex){}
catch (IOException ex) {}       


if (sentence.length()<2) {  Utils.sleep(10000);  return; }
try { client.delete("/spout.txt");}
 catch (DbxException ex) {  } 
_collector.emit(new Values(sentence)); 
Utils.sleep(1000);      

现在我想升级我的喷口以使用Kafka中的文本,以便提交到拓扑中的下一个螺栓。我试图在git中关注许多文章和代码而没有任何成功。 例如:this kafka spout。 有谁可以帮助并给我一些指导,以实现新的spout.java文件? 谢谢!

1 个答案:

答案 0 :(得分:0)

从风暴0.9.2版本开始,有一个外部storm-kafka包可以做到这一点。实际上,这个软件包是从storm-kafka-0.8-plus向风暴社区提供的。并且有test project显示其用法。

详细信息,首先向maven(或lein / gradle)添加依赖项:

groupId: org.apache.storm
artifactId: storm-kafka
version: 0.9.2-incubating

然后像这样创建拓扑和spout:

import storm.kafka

TridentTopology topology = new TridentTopology();
BrokerHosts zk = new ZkHosts("localhost");
TridentKafkaConfig spoutConf = new TridentKafkaConfig(zk, "test-topic");
spoutConf.scheme = new SchemeAsMultiScheme(new StringScheme());
OpaqueTridentKafkaSpout spout = new OpaqueTridentKafkaSpout(spoutConf);