我目前正在从事一个项目,从 TI-SensorTag CC2650 收集传感器值,使用 python 脚本通过 HTTP(通过 Apache-TomCat-Servlet)将这些值发送到 Apache-Kafka,并将 Kafka 与 Apache- Storm 来处理数据。
此数据将通过拓扑中的 bolt 写入本地系统(Apache-Storm-Cluster-Folder 的目录)上的 .txt 文件。
几周前我刚刚开始使用 Storm 和 Kafka,我对以下内容感到困惑:
如果我在本地集群上运行拓扑,一切正常。但是如果我将它提交到一个“普通”集群,在 localhost:8888 上运行,那么它什么都不做。
Storm-UI 确实显示了拓扑,但似乎对来自 kafka 的传入消息没有任何反应。
本地集群上的测试和真实集群上的功能应该差不多吧?或者集群只是没有权限在我的本地系统上写入/修改文件?
附加信息:
它应该如何运作?
When I write an message to an topic in Kafka, the Kafka-Spout of the topology should grab that message and write it down into an .txt-file on my local filesystem.
我的代码 (jar-with-dependencies) 位于:
"/home/tobias/storm/apache-storm-0.9.2-incubating/mycode/StormKafkaTopology/target/"
我正在尝试写入位于以下位置的 output.txt:
"/home/tobias/storm/apache-storm-0.9.2-incubating/mycode/StromKafkaTopology"/tmp/"
拓扑代码:
public class StormKafkaTopology {
public static void main(String[] args) throws Exception {
Config config = new Config();
config.setDebug(true);
config.put(Config.TOPOLOGY_MAX_SPOUT_PENDING, 1);
String zkConnString = "localhost:2181";
String topic = "mytopic";
BrokerHosts hosts = new ZkHosts(zkConnString);
SpoutConfig kafkaSpoutConfig = new SpoutConfig(hosts, topic, "/" +topic, UUID.randomUUID().toString());
kafkaSpoutConfig.bufferSizeBytes = 1024 * 1024 * 4;
kafkaSpoutConfig.fetchSizeBytes = 1024 * 1024 * 4;
//kafkaSpoutConfig.forceFromStart = true;
kafkaSpoutConfig.scheme = new SchemeAsMultiScheme(new StringScheme());
TopologyBuilder builder = new TopologyBuilder();
builder.setSpout("kafka-spout", new KafkaSpout(kafkaSpoutConfig));
builder.setBolt("printer-bolt", new PrinterBolt()).shuffleGrouping("kafka-spout");
if (args != null && args.length >0) {
config.setNumWorkers(6);
config.setNumAckers(6);
//config.setMaxSpoutPending(100);
//config.setMessageTimeoutSecs(20);
StormSubmitter.submitTopology("StormKafkaTopology", config, builder.createTopology());
} else {
LocalCluster cluster = new LocalCluster();
cluster.submitTopology("StormKafkaTopology", config, builder.createTopology());
Utils.sleep(10000);
cluster.killTopology("StormKafkaTopology");
cluster.shutdown();
}
}}
PrinterBolt 代码:
public class PrinterBolt extends BaseBasicBolt {
/*
* execute-method will be opened if tuples are processed
*/
@Override
public void execute(Tuple tuple, BasicOutputCollector collector) {
String msg = tuple.getString(0);
System.out.println("======before write file======");
try {
// set file directory:
File file = new File("/home/tobias/storm/apache-storm-0.9.2-incubating/mycode/StormKafkaTopology/tmp/output.txt");
if(!file.exists()) {
file.createNewFile();
}
//create a FileWriter
FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
//create a BufferedWriter
BufferedWriter bw = new BufferedWriter(fw);
//write into the file
bw.write(msg + "\n");
//close the BufferedWriter (IMPORTANT)
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("======after write file======");
//you could emit some Date here for further processing:
//collector.emit(new Values(msg));
}
@Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declare(new Fields("output"));
}}
如果有人能够指出我的错误并提供一些建议,我将不胜感激。
最佳答案
如果您能够“本地”运行它,那么这是很好的第一步。此外,听起来您可以访问 Storm UI,这很好。提交拓扑后,它应该会显示在 Storm UI 中,然后您可以单击它来查看拓扑中的 spouts 和 bolts。单击每个 spout/bolt,然后单击端口(每个工作人员一个)以在 UI 中查看日志。
我的猜测是某处有错误。是时候开始挖掘 storm/kafka 日志以找出它是什么了。
Q:如何识别哪个worker创建了哪个log?每个 worker 都被分配了一个端口。每个工作日志都是拓扑名称 + 端口的组合。对于您来说,只需找到最新的日志并查看其中的内容即可。
一些事情:
- 从 1 个 worker 开始,比较简单
- > Update the logging in your PrinterBolt to use SLF4J这样您就可以在 storm 的日志和 UI 中看到消息
- 添加 try/catch 并在发生异常时使用
collector.reportError(e);
报告错误。然后它会在 Storm ui 中显示为红色! - 在你的图表上稍微澄清一下,Kafka 代理端口是 9092 而不是 2181 ... 2181 仅用于 zookeeper
关于java - 集群中的拓扑是否可以写入本地文件系统上的 txt 文件? (在同一系统上运行的集群),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40973098/