KafKa Java程式設計例項
KafKa Java程式設計例項
編寫一個能傳送訊息,接收訊息的例項
(1)編寫係數配置:KafkaProperties.java
package com.imooc.spark.kafka; /** * Kafka常用配置檔案 */ public class KafkaProperties { public static final String ZK = "140.143.236.169:2181"; //此處要和kafka配置檔案中的 advertised.host.name保持一致,要麼都是IP,要麼都是HostName public static final String TOPIC = "hello_topic"; public static final String BROKER_LIST = "140.143.236.169:9092"; public static final String GROUP_ID = "test_group1"; } |
(2)編寫生產者:KafkaProducer.java
package com.imooc.spark.kafka; import kafka.javaapi.producer.Producer; import kafka.producer.KeyedMessage; import kafka.producer.ProducerConfig; import java.util.Properties; /** * Kafka生產者 */ public class KafkaProducer extends Thread{ private String topic; private Producer<Integer, String> producer; public KafkaProducer(String topic) { this.topic = topic; Properties properties = new Properties(); properties.put("metadata.broker.list",KafkaProperties.BROKER_LIST); properties.put("serializer.class","kafka.serializer.StringEncoder"); properties.put("request.required.acks","1"); producer = new Producer<Integer, String>(new ProducerConfig(properties)); } @Override public void run() { int messageNo = 1; while(true) { String message = "message_" + messageNo; producer.send(new KeyedMessage<Integer, String>(topic, message)); System.out.println("Sent: " + message); messageNo ++ ; try{ Thread.sleep(2000); } catch (Exception e){ e.printStackTrace(); } } } } |
(3)編寫消費者:KafkaConsumer.java
package com.imooc.spark.kafka; import kafka.consumer.Consumer; import kafka.consumer.ConsumerConfig; import kafka.consumer.ConsumerIterator; import kafka.consumer.KafkaStream; import kafka.javaapi.consumer.ConsumerConnector; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; /** * Kafka消費者 */ public class KafkaConsumer extends Thread{ private String topic; public KafkaConsumer(String topic) { this.topic = topic; } private ConsumerConnector createConnector(){ Properties properties = new Properties(); properties.put("zookeeper.connect", KafkaProperties.ZK); properties.put("group.id",KafkaProperties.GROUP_ID); return Consumer.createJavaConsumerConnector(new ConsumerConfig(properties)); } @Override public void run() { ConsumerConnector consumer = createConnector(); Map<String, Integer> topicCountMap = new HashMap<String, Integer>(); topicCountMap.put(topic, 1); // topicCountMap.put(topic2, 1); // topicCountMap.put(topic3, 1); // String: topic // List<KafkaStream<byte[], byte[]>> 對應的資料流 Map<String, List<KafkaStream<byte[], byte[]>>> messageStream = consumer.createMessageStreams(topicCountMap); KafkaStream<byte[], byte[]> stream = messageStream.get(topic).get(0); //獲取我們每次接收到的暑假 ConsumerIterator<byte[], byte[]> iterator = stream.iterator(); while (iterator.hasNext()) { String message = new String(iterator.next().message()); System.out.println("rec: " + message); } } } |
(4)編寫啟動例項:KafkaClientApp.java
package com.imooc.spark.kafka; /** * Kafka Java API測試 */ public class KafkaClientApp { public static void main(String[] args) { new KafkaProducer(KafkaProperties.TOPIC).start(); new KafkaConsumer(KafkaProperties.TOPIC).start(); } } |
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.imooc.spark</groupId> <artifactId>sparktrain</artifactId> <version>1.0</version> <inceptionYear>2008</inceptionYear> <properties> <scala.version>2.11.8</scala.version> <kafka.version>0.9.0.0</kafka.version> <spark.version>2.2.0</spark.version> <hadoop.version>2.6.0-cdh5.7.0</hadoop.version> <hbase.version>1.2.0-cdh5.7.0</hbase.version> </properties> <!--新增cloudera的repository--> <repositories> <repository> <id>cloudera</id> </repository> </repositories> <dependencies> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>${scala.version}</version> </dependency> <!-- Kafka 依賴--> <!-- <dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka_2.11</artifactId> <version>${kafka.version}</version> </dependency> --> <!-- Hadoop 依賴--> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>${hadoop.version}</version> </dependency> <!-- HBase 依賴--> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>${hbase.version}</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>${hbase.version}</version> </dependency> <!-- Spark Streaming 依賴--> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-streaming_2.11</artifactId> <version>${spark.version}</version> </dependency> <!-- Spark Streaming整合Flume 依賴--> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-streaming-flume_2.11</artifactId> <version>${spark.version}</version> </dependency> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-streaming-flume-sink_2.11</artifactId> <version>${spark.version}</version> </dependency> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-streaming-kafka-0-8_2.11</artifactId> <version>${spark.version}</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.5</version> </dependency> <!-- Spark SQL 依賴--> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-sql_2.11</artifactId> <version>${spark.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.module</groupId> <artifactId>jackson-module-scala_2.11</artifactId> <version>2.6.5</version> </dependency> <dependency> <groupId>net.jpountz.lz4</groupId> <artifactId>lz4</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>org.apache.flume.flume-ng-clients</groupId> <artifactId>flume-ng-log4jappender</artifactId> <version>1.6.0</version> </dependency> </dependencies> <build> <sourceDirectory>src/main/scala</sourceDirectory> <testSourceDirectory>src/test/scala</testSourceDirectory> <plugins> <plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> <configuration> <scalaVersion>${scala.version}</scalaVersion> <args> <arg>-target:jvm-1.5</arg> </args> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-eclipse-plugin</artifactId> <configuration> <downloadSources>true</downloadSources> <buildcommands> <buildcommand>ch.epfl.lamp.sdt.core.scalabuilder</buildcommand> </buildcommands> <additionalProjectnatures> <projectnature>ch.epfl.lamp.sdt.core.scalanature</projectnature> </additionalProjectnatures> <classpathContainers> <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer> <classpathContainer>ch.epfl.lamp.sdt.launching.SCALA_CONTAINER</classpathContainer> </classpathContainers> </configuration> </plugin> </plugins> </build> <reporting> <plugins> <plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <configuration> <scalaVersion>${scala.version}</scalaVersion> </configuration> </plugin> </plugins> </reporting> </project> |
相關文章
- 設計模式例項程式碼設計模式
- Jmeter beanshell程式設計例項JMeterBean程式設計
- Kafka效能測試例項Kafka
- 好程式設計師Java培訓之類的例項化步驟程式設計師Java
- shell script程式設計小結——附帶例項程式設計
- Spark程式設計環境搭建及WordCount例項Spark程式設計
- 《java程式設計基礎》例題5.6Java程式設計
- JAVA程式設計注意事項(效能篇)Java程式設計
- VC++視覺化程式設計第一個程式設計例項出錯C++視覺化程式設計
- [翻譯]返回導向程式設計例項入門程式設計
- 例項九— 除法器設計
- Java程式設計師的兩項通用能力Java程式設計師
- LINUX Shell指令碼程式設計例項詳解(一)上Linux指令碼程式設計
- 微控制器C程式設計例項指導pdfC程式程式設計
- makefile--偽目標語法與程式設計例項程式設計
- Python - 物件導向程式設計 - 類變數、例項變數/類屬性、例項屬性Python物件程式設計變數
- [java設計模式]單例設計模式,程式也要進行計劃生育。Java設計模式單例
- 解析JavaScript設計模型Iterator例項JavaScript模型
- java_隨機數(統計例項)Java隨機
- Java例項教程Java
- Apache Kafka 程式設計實戰ApacheKafka程式設計
- java單例設計模式Java單例設計模式
- 詳解Python物件導向程式設計之類、例項、方法Python物件程式設計
- Spring程式設計式和宣告式事務例項講解Spring程式設計
- JavaScript倒數計時程式碼例項JavaScript
- Verilog設計技巧例項及實現
- 好程式設計師web前端分享在HTML中使用JavaScript例項程式碼程式設計師Web前端HTMLJavaScript
- Java--例項化Java
- Rust 程式設計影片教程(進階)——010_2Box 使用例項Rust程式設計
- Python - 物件導向程式設計 - 例項方法、靜態方法、類方法Python物件程式設計
- Matlab簡介與程式設計例項(一)(西北工業大MOOC)Matlab程式設計
- Java設計模式-單例模式Java設計模式單例
- Java設計模式【單例模式】Java設計模式單例
- Java設計模式 | 單例模式Java設計模式單例
- Java設計模式——單例模式Java設計模式單例
- Java設計模式--單例模式Java設計模式單例
- Java設計模式–單例模式Java設計模式單例
- Java設計模式系列之單例設計模式Java設計模式單例