• 大小: 18KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-12
  • 语言: Java
  • 标签: kafka-java  

资源简介

用java实现向kafka写数据以及从kafka消费数据,kafka版本支持0.10以上

资源截图

代码片段和文件信息

package com.kafka.example;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.util.Arrays;
import java.util.Properties;

/**
 * 1. 启动生产服务:bin/kafka-console-producer.sh --broker-list master:9092 --topic test-group
 */
public class SimpleComsumerGroup {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.put(“bootstrap.servers“ “master:9092“);
        props.put(“group.id“ “group1“);
        props.put(“key.deserializer“ “org.apache.kafka.common.serialization.StringDeserializer“);
        props.put(“value.deserializer“ “org.apache.kafka.common.serialization.StringDeserializer“);

        KafkaConsumer consumer = new KafkaConsumer(props);
        consumer.subscribe(Arrays.asList(“test2-group“));
        while (true) {
            ConsumerRecords records = consumer.poll(100);
            for (ConsumerRecord record : records) {
                System.out.printf(“offset = %d key = %s value = %s topic = %s partition = %d“
                        record.offset() record.key() record.value() record.topic() record.partition());
                System.out.println();
            }
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        634  2018-02-16 20:43  kafka\.idea\compiler.xml

     文件        510  2018-02-23 15:25  kafka\.idea\libraries\Maven__com_101tec_zkclient_0_8.xml

     文件        665  2018-02-23 15:25  kafka\.idea\libraries\Maven__com_fasterxml_jackson_core_jackson_annotations_2_6_0.xml

     文件        616  2018-02-23 15:25  kafka\.idea\libraries\Maven__com_fasterxml_jackson_core_jackson_core_2_6_3.xml

     文件        644  2018-02-23 15:25  kafka\.idea\libraries\Maven__com_fasterxml_jackson_core_jackson_databind_2_6_3.xml

     文件        537  2018-02-23 15:25  kafka\.idea\libraries\Maven__io_netty_netty_3_7_0_Final.xml

     文件        548  2018-02-23 15:25  kafka\.idea\libraries\Maven__javax_activation_activation_1_1.xml

     文件        482  2018-02-23 15:25  kafka\.idea\libraries\Maven__javax_mail_mail_1_4.xml

     文件        490  2018-02-23 15:25  kafka\.idea\libraries\Maven__jline_jline_0_9_94.xml

     文件        483  2018-02-23 15:25  kafka\.idea\libraries\Maven__junit_junit_3_8_1.xml

     文件        490  2018-02-23 15:25  kafka\.idea\libraries\Maven__log4j_log4j_1_2_15.xml

     文件        509  2018-02-23 15:25  kafka\.idea\libraries\Maven__net_jpountz_lz4_lz4_1_3_0.xml

     文件        590  2018-02-23 15:25  kafka\.idea\libraries\Maven__org_apache_kafka_connect_api_0_10_0_1.xml

     文件        597  2018-02-23 15:25  kafka\.idea\libraries\Maven__org_apache_kafka_connect_json_0_10_0_1.xml

     文件        604  2018-02-23 15:25  kafka\.idea\libraries\Maven__org_apache_kafka_kafka_clients_0_10_0_1.xml

     文件        604  2018-02-23 15:25  kafka\.idea\libraries\Maven__org_apache_kafka_kafka_streams_0_10_0_1.xml

     文件        571  2018-02-23 15:25  kafka\.idea\libraries\Maven__org_apache_zookeeper_zookeeper_3_4_6.xml

     文件        542  2018-02-23 15:25  kafka\.idea\libraries\Maven__org_rocksdb_rocksdbjni_4_8_0.xml

     文件        534  2018-02-23 15:25  kafka\.idea\libraries\Maven__org_slf4j_slf4j_api_1_7_21.xml

     文件        555  2018-02-23 15:25  kafka\.idea\libraries\Maven__org_slf4j_slf4j_log4j12_1_6_1.xml

     文件        587  2018-02-23 15:25  kafka\.idea\libraries\Maven__org_xerial_snappy_snappy_java_1_1_2_6.xml

     文件        443  2018-02-16 20:43  kafka\.idea\misc.xml

     文件        262  2018-02-16 20:43  kafka\.idea\modules.xml

     文件       8792  2018-02-26 15:30  kafka\.idea\uiDesigner.xml

     文件      46845  2018-02-26 15:41  kafka\.idea\workspace.xml

     文件       2762  2018-02-16 20:43  kafka\kafka-basic.iml

     文件       1870  2018-02-26 15:18  kafka\pom.xml

     文件       1378  2018-02-26 15:39  kafka\src\main\java\com\kafka\example\SimpleComsumerGroup.java

     文件       1272  2018-02-26 15:38  kafka\src\main\java\com\kafka\example\SimpleProducer.java

     文件       2698  2018-02-26 15:19  kafka\target\classes\com\kafka\example\SimpleComsumerGroup1.class

............此处省略22个文件信息

评论

共有 条评论

相关资源