1. 程式人生 > >The method iterator() is ambiguous for the type KafkaStream

The method iterator() is ambiguous for the type KafkaStream

slf4j 後來 pac upd 資料 keep bubuko 分享圖片 sse

最近在學習消息總線Kafka的時候,在IDEA中寫了一個簡單的生產者和消費者demo。但是消費者端一直報錯。錯誤信息如下:

  1  ConsumerIterator<byte[],byte[]> it =stream.iterator();
  2  這句代碼老是報錯,The method iterator() is ambiguous for the type KafkaStream<byte[],byte[]>

後來博主在網上找了一些資料,解決方法記錄一下:

poom.xml文件如下:

  1 <dependencies>
  2
<dependency> 3 <groupId>org.apache.kafka</groupId> 4 <artifactId>kafka_2.9.2</artifactId> 5 <version>0.8.1.1</version> 6 <exclusions> 7 <exclusion> 8 <artifactId>jmxtools</artifactId> 9
<groupId>com.sun.jdmk</groupId> 10 </exclusion> 11 <exclusion> 12 <artifactId>jmxri</artifactId> 13 <groupId>com.sun.jmx</groupId> 14 </exclusion> 15
<exclusion> 16 <artifactId>jms</artifactId> 17 <groupId>javax.jms</groupId> 18 </exclusion> 19 <exclusion> 20 <groupId>org.apache.zookeeper</groupId> 21 <artifactId>zookeeper</artifactId> 22 </exclusion> 23 <exclusion> 24 <groupId>org.slf4j</groupId> 25 <artifactId>slf4j-log4j12</artifactId> 26 </exclusion> 27 <exclusion> 28 <groupId>org.slf4j</groupId> 29 <artifactId>slf4j-api</artifactId> 30 </exclusion> 31 </exclusions> 32 </dependency> 33 </dependencies> 34 35 <build> 36 <plugins> 37 <plugin> 38 <artifactId>maven-assembly-plugin</artifactId> 39 <configuration> 40 <descriptorRefs> 41 <descriptorRef>jar-with-dependencies</descriptorRef> 42 </descriptorRefs> 43 <archive> 44 <manifest> 45 <mainClass></mainClass> 46 </manifest> 47 </archive> 48 </configuration> 49 <executions> 50 <execution> 51 <id>make-assembly</id> 52 <phase>package</phase> 53 <goals> 54 <goal>single</goal> 55 </goals> 56 </execution> 57 </executions> 58 </plugin> 59 <!--<plugin>--> 60 <!--<groupId>org.apache.maven.plugins</groupId>--> 61 <!--<artifactId>maven-compiler-plugin</artifactId>--> 62 <!--<version>3.1</version>--> 63 <!--<configuration>--> 64 <!--<source>1.8</source>--> 65 <!--<target>1.8</target>--> 66 <!--<showWarnings>true</showWarnings>--> 67 <!--</configuration>--> 68 <!--</plugin>--> 69 </plugins> 70 </build>

首先將maven中的poom.xml文件中以下內容註釋掉:

  1 <plugin>
  2   <groupId>org.apache.maven.plugins</groupId>
  3   <artifactId>maven-compiler-plugin</artifactId>
  4   <version>3.1</version>
  5   <configuration>
  6     <source>${jdk.version}</source>
  7     <target>${jdk.version}</target>
  8     <showWarnings>true</showWarnings>
  9   </configuration>
 10 </plugin>

然後在自己的IDEA MAVEN項目中配置默認jdk版本:

配置方法如下:

第一種:在maven的安裝目錄找到settings.xml文件,在裏面添加如下代碼

  1 <profile>
  2     <id>jdk-1.8</id>
  3      <activation>
  4           <activeByDefault>true</activeByDefault>
  5           <jdk>1.8</jdk>
  6       </activation>
  7 <properties>
  8 <maven.compiler.source>1.8</maven.compiler.source>
  9 <maven.compiler.target>1.8</maven.compiler.target>
 10 <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
 11 </properties>
 12 </profile>

添加完後,在對IDEA進行設置。file->setting->maven->user settings file,user settings file那裏選擇maven安裝目錄下的settings.xml文件.

技術分享圖片

第二種方法是在第一種的基礎之上:user settings的默認settigs.xml文件路徑為:C:\Users\Administrator\.m2。只要把設置好的settings.xml文件復制到該目錄下,然後update project就好了。

The method iterator() is ambiguous for the type KafkaStream