使用samza消费远程kafka主题

时间:2017-07-05 21:10:06

标签: apache-kafka apache-samza

我正在尝试将hello-samza教程修改为:

(1)从远程代理上的kafka主题读取(即不是localhost) (2)将消息写入文件

我将WikipediaFeedStreamTask.java修改为如下所示:

public class WikipediaFeedStreamTask implements StreamTask {
  private static final SystemStream OUTPUT_STREAM = new SystemStream("kafka", "wikipedia-ra\
w");

  @Override
  public void process(IncomingMessageEnvelope envelope, MessageCollector collector, TaskCoo\
rdinator coordinator) {
      //System.out.println("Message Received!");
      //System.out.println(envelope.getMessage());
      try{
      PrintWriter writer = new PrintWriter("test.txt", "UTF-8");
      writer.println(envelope.getMessage());
      writer.println("The second line");
      writer.close();}
      catch(IOException e)
          {}
      Map<String, Object> outgoingMap = WikipediaFeedEvent.toMap((WikipediaFeedEvent) envel\
ope.getMessage());
    collector.send(new OutgoingMessageEnvelope(OUTPUT_STREAM, outgoingMap));
  }
}

这只是标准文件,另外还有将消息写入文件。

我将属性文件修改为:

# Job
job.factory.class=org.apache.samza.job.yarn.YarnJobFactory
job.name=wikipedia-feed

# YARN
yarn.package.path=file://${basedir}/target/${project.artifactId}-${pom.version}-dist.tar.gz

# Task
task.class=samza.examples.wikipedia.task.WikipediaFeedStreamTask
task.inputs=wikipedia.#en.wikipedia,wikipedia.#en.wiktionary,wikipedia.#en.wikinews

# Serializers
serializers.registry.json.class=org.apache.samza.serializers.JsonSerdeFactory

# Wikipedia System
systems.wikipedia.samza.factory=samza.examples.wikipedia.system.WikipediaSystemFactory
systems.wikipedia.host=irc.wikimedia.org
systems.wikipedia.port=6667

# Kafka System
systems.kafka.samza.factory=org.apache.samza.system.kafka.KafkaSystemFactory
systems.kafka.samza.msg.serde=json
systems.kafka.consumer.zookeeper.connect=REMOTE-ZOOKEEPER-IP:2181/
systems.kafka.producer.bootstrap.servers=REMOTE-BROKER-IP:9092

# Job Coordinator
job.coordinator.system=kafka

当我运行作业(like so)时,我会在test.txt中看到维基百科流中的数据。我在假设只是修改.properties文件中的kafka使用者值将迫使samza从该代理读取时,我显然是不正确的。那么我需要改变什么?

我在哪里指定samza应该监听的主题名称?

1 个答案:

答案 0 :(得分:1)

我看到你修改了kafka系统的连接字符串。但是,您的StreamTask输入仍然引用维基百科中的流: task.inputs=wikipedia.#en.wikipedia,wikipedia.#en.wiktionary,wikipedia.#en.wikinews

您应该将task.inputs的值更改为&#34; kafka。$ yourInputStreamName&#34;。请试一试。我认为应该解决你的问题。