【发布时间】:2015-10-24 03:50:20
【问题描述】:
我正在编写一个简单的 twitter 程序,我正在使用 Kafka 阅读 Tweets,并希望使用 Avro 进行序列化。到目前为止,我刚刚在 Scala 中设置了 twitter 配置,现在想使用此配置来阅读推文。
如何在我的程序中导入文件 tweets.avsc 中定义的以下 avro 架构?
{
"namespace": "tweetavro",
"type": "record",
"name": "Tweet",
"fields": [
{"name": "name", "type": "string"},
{"name": "text", "type": "string"}
]
}
我在网上遵循了一些示例,其中显示了类似 import tweetavro.Tweet 的内容,以便在 Scala 中导入架构,以便我们可以像使用它一样使用它
def main (args: Array[String]) {
val twitterStream = TwitterStream.getStream
twitterStream.addListener(new OnTweetPosted(s => sendToKafka(toTweet(s))))
twitterStream.filter(filterUsOnly)
}
private def toTweet(s: Status): Tweet = {
new Tweet(s.getUser.getName, s.getText)
}
private def sendToKafka(t:Tweet) {
println(toJson(t.getSchema).apply(t))
val tweetEnc = toBinary[Tweet].apply(t)
val msg = new KeyedMessage[String, Array[Byte]](KafkaTopic, tweetEnc)
kafkaProducer.send(msg)
}
我在pom.xml 中遵循相同并使用以下插件
<!-- AVRO MAVEN PLUGIN -->
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<version>1.7.7</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>schema</goal>
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/avro/</sourceDirectory>
<outputDirectory>${project.basedir}/src/main/scala/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- MAVEN COMPILER PLUGIN -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
做完这一切,我还是做不到import tweetavro.Tweet
有人可以帮忙吗?
谢谢!
【问题讨论】:
标签: scala maven twitter avro apache-kafka