繁体   English   中英

在Java,Maven中导入路径

[英]Import path in Java, Maven

以下是关于Kafka Streams的教程: https//github.com/confluentinc/kafka-streams-examples/blob/4.0.0-post/src/main/java/io/confluent/examples/streams/WikipediaFeedAvroExample.java

有一条线:

import io.confluent.examples.streams.avro.WikiFeed

我认为它与此文件有关: https//github.com/confluentinc/kafka-streams-examples/blob/4.0.0-post/src/main/resources/avro/io/confluent/examples/streams/wikifeed .avsc

  • Maven如何知道它在resource而不是java文件夹中?
  • 为什么io / confluent / examples / streams / avro /wikifeed.avsc而不是avro /io/confluent/examples/streams/wikifeed.avsc

另一个进口更加精彩:

import io.confluent.kafka.serializers.AbstractKafkaAvroSerDeConfig;
  • java/io/confluent文件夹中没有kafka文件夹。

https://github.com/confluentinc/kafka-streams-examples/tree/4.0.0-post/src/main/resources/avro/io/confluent

所有这些魔法如何起作用?

魔术是由avro-maven-plugin制作的 ,您可以在pom.xml中找到它:

<plugin>
    <groupId>org.apache.avro</groupId>
    <artifactId>avro-maven-plugin</artifactId>
    <version>${avro.version}</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>schema</goal>
            </goals>
            <configuration>
                <sourceDirectory>src/main/resources/avro/io/confluent/examples/streams</sourceDirectory>
                <outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
                <stringType>String</stringType>
            </configuration>
        </execution>
    </executions>
</plugin>

引用插件的文档:

与动态语言的简单集成。 不需要代码生成来读取或写入数据文件,也不需要使用或实现RPC协议。 代码生成作为可选优化,仅值得为静态类型语言实现。

这是在预编译时,插件读取avsc文件的内容并生成二进制源(对于这种情况,Java类),然后可以在代码中使用。

您可以在target/generated-sources看到插件target/generated-sources 那里将有一个文件夹结构和适当的java(非类)文件。

WikiFeed类是在构建时使用您链接到的.avsc文件中的avro-maven-plugin动态创建的。 您可以在pom.xml的<plugins>部分查看它的配置方式。

AbstractKafkaAvroSerDeConfig类来自kafka-avro-serializer依赖项。 Eclipse有一种很好的方法可以从Editor视图中的单个类导航回包含Maven依赖项的Package Explorer,如下所示:

package explorer:maven依赖项

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM