kafka(六):java API消费数据

说明

kafka消费消息记录。

分享

kafka消费实现

maven

<dependency>  
    <groupId>org.apache.kafka</groupId>  
    <artifactId>kafka-clients</artifactId>  
    <version>2.3.0</version>  
</dependency>
<!-- 日志 -->
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.11</version>
</dependency>

连接

  • 连接并消费kafka
private Logger log= LoggerFactory.getLogger(this.getClass());
	
	/**
	 * kafka 连接地址
	 */
	public String bootstrapServer ="localhost:9092";
	
	/**
	 * topic
	 */
	public String topic="test1";
	
	
	public void kafkaDemo() {
	       Properties properties = new Properties();
	        //borker地址
	        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,bootstrapServer);
	        //反序列化方式
	        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class.getName());
	        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class.getName());
	        
	        //------------------消费组id必须指定----------------------------
	        //指定消费者组id
	        properties.put(ConsumerConfig.GROUP_ID_CONFIG,"group1");
	        
	        //earliest:offset偏移至最早时候开始消费;latest:偏移到从最新开始消费(默认) earliest 从最早位置消费消息
	        properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");		
	        
	        //每批次最小拉取数据大小,默认1byte
	        properties.put(ConsumerConfig.FETCH_MIN_BYTES_CONFIG,1);
	        
	        //每批次最大拉取数据大小,默认50M
	        properties.put(ConsumerConfig.FETCH_MAX_BYTES_CONFIG,50 * 1024 * 1024);
	        
	        //一批次数据,未达到最小数据大小时候,最大等待时间.默认500ms
	        properties.put(ConsumerConfig.FETCH_MAX_WAIT_MS_CONFIG,500);
	        
	        //单次调用 poll() 返回的最大记录数,默认500
	        properties.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG,500);
	        
	        KafkaConsumer<String,String> kafkaConsumer = new KafkaConsumer<>(properties);
	        
	        //A. 设置订阅的topic 列表,从所有数据分区读取数据
	        List<String> topicList = new ArrayList<>();
	        topicList.add(topic);
	        kafkaConsumer.subscribe(topicList);
	        
	        //B. 设置订阅topic,并指定分区
//	        List<TopicPartition> topicPartitions = new ArrayList<>();
//	        topicPartitions.add(new TopicPartition(topic,0));
//	        kafkaConsumer.assign(topicPartitions);
	        
	        try {
		        while (true){
		            ConsumerRecords<String,String> records = kafkaConsumer.poll(Duration.ofSeconds(2L));
		            for (ConsumerRecord<String,String> record : records) {
		                System.out.println("topic:"+record.topic()+"|partition:"+record.partition()+"|数据:"+record.value());
		            }
		        }
		        
	        }catch(Exception e){
	        	log.error(e.toString(),e);
	        }finally {
	        	kafkaConsumer.close();
	        }
	}
	
	
	public static void main(String[] args) {
		new KafkaCustomer().kafkaDemo();;
	}

说明

  • 注意设置消费位置,请按需选择
    • earliest 偏移至最早时候开始消费,每次软件启动会从最开始位置消费,会有重复数据
    • latest:偏移到从最新开始消费(默认),软件启动前kafka中数据不会被接收处理。
  • 设置Topic支持分区指定,默认消费所有分区。
  • 如需kerberos认证,参照 kafka(四):kafka javaAPI入库程序

总结

  • 无论境遇如何,积累好技术,水滴石穿。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值