kafka_2.11 简单使用

本文介绍了如何在Kafka 2.11中创建topic、producer、consumer,并通过API进行操作。首先,使用kafka.topics.sh脚本创建topic。接着,通过命令行创建producer和consumer,理解其工作原理。最后,通过Maven项目引入Kafka依赖,并展示了简单的producer和consumer的API使用示例。

进过上一篇的引导,相信大家应该都已经安装好了。这次我们就来简单使用一下。

1 创建 topic

像官网上写的那样,我们可以使用 kafka.topics.sh 脚本来创建一个 topic 。不过,前提是我们需要启动zookeeper 和 kafka 服务。
这里写图片描述

那我们就用官网上例子好了:

[root@master config]# kafka-topics.sh --create --zookeeper localhost:2181 --topic first                                      

Missing required argument "[partitions]"

Option                                   Description                            
------                                   -----------                            
--alter                                  Alter the number of partitions,        
                                           replica assignment, and/or           
                                           configuration for the topic.         
--config <name=value>                    A topic configuration override for the 
                                           topic being created or altered.The   
                                           following is a list of valid         
                                           configurations:                      
                                                cleanup.policy                        
                                                compression.type                      
                                                delete.retention.ms                   
                                                file.delete.delay.ms                  
                                                flush.messages                        
                                                flush.ms                              
                                                follower.replication.throttled.       
                                           replicas                             
                                                index.interval.bytes                  
                                                leader.replication.throttled.replicas 
                                                max.message.bytes                     
                                                message.format.version                
                                                message.timestamp.difference.max.ms   
                                                message.timestamp.type                
                                                min.cleanable.dirty.ratio             
                                                min.compaction.lag.ms                 
                                                min.insync.replicas                   
                                                preallocate                           
                                                retention.bytes                       
                                                retention.ms                          
                                                segment.bytes                         
                                                segment.index.bytes                   
                                                segment.jitter.ms                     
                                                segment.ms                            
                                                unclean.leader.election.enable        
                                         See the Kafka documentation for full   
                                           details on the topic configs.        
--create                                 Create a new topic.                    
--delete                                 Delete a topic                         
--delete-config <name>                   A topic configuration override to be   
                                           removed for an existing topic (see   
                                           the list of configurations under the 
                                           --config option).                    
--describe                               List details for the given topics.     
--disable-rack-aware                     Disable rack aware replica assignment  
--force                                  Suppress console prompts               
--help                                   Print usage information.               
--if-exists                              if set when altering or deleting       
                                           topics, the action will only execute 
                                           if the topic exists                  
--if-not-exists                          if set when creating topics, the       
                                           action will only execute if the      
                                           topic does not already exist         
--list                                   List all available topics.             
--partitions <Integer: # of partitions>  The number of partitions for the topic 
                                           being created or altered (WARNING:   
                                           If partitions are increased for a    
                                           topic that has a key, the partition  
                                           logic or ordering of the messages    
                                           will be affected                     
--replica-assignment                     A list
### Kafka 2.11-2.4.0 安装与配置教程 #### 下载安装包 对于Kafka版本`2.11-2.4.0`,其对应的Scala版本为2.11,而具体的Kafka版本号则为2.4.0[^1]。可以从官方提供的下载页面获取该特定版本的压缩文件,通常命名为类似于`kafka_2.11-2.4.0.tgz`的形式。 #### 文件传输至服务器 利用XFTP或其他类似的SFTP客户端工具可以方便地将本地计算机上的Kafka安装包上传到远程Linux系统的指定目录下[^2]。这一步骤确保了后续可以在目标机器上执行解压以及进一步的操作。 #### 解压并设置环境变量 一旦完成了上传操作,则需登录到相应的Linux实例,在命令行界面中切换到存储有`.tgz`文件的位置,并运行如下指令来完成解压缩工作: ```bash tar -xzf kafka_2.11-2.4.0.tgz cd kafka_2.11-2.4.0/ ``` 为了简化之后调用Kafka相关脚本的过程,建议添加路径到用户的PATH环境中去。可以通过编辑个人shell配置文件(比如~/.bashrc),加入下面这一行实现此目的: ```bash export PATH=$HOME/kafka_2.11-2.4.0/bin:$PATH ``` 接着使更改生效: ```bash source ~/.bashrc ``` #### 配置Zookeeper服务 由于Kafka依赖于ZooKeeper来进行协调管理,因此在启动之前还需要先确认已经正确设置了ZooKeeper的服务端口,默认情况下监听的是2181端口。如果尚未部署ZooKeeper集群的话,可以选择从同一套件内附带的一个单节点模式作为临时解决方案。 创建一个新的会话窗口用于持续保持ZooKeeper进程处于活动状态: ```bash bin/zookeeper-server-start.sh config/zookeeper.properties & ``` #### 启动Kafka Broker 当上述准备工作完成后就可以尝试开启Broker节点了。同样地,新开一个终端标签页以便单独监控这个过程中的日志输出情况: ```bash bin/kafka-server-start.sh config/server.properties & ``` 此时理论上就已经成功搭建起了基于单一主机的小型测试平台;当然实际生产环境下往往涉及多台物理机组成的分布式架构设计,这就需要更加复杂的参数调整和网络规划了。 #### 创建主题Topic 最后介绍如何定义消息传递的主题结构——即所谓的Topics。假设想要建立名为test-topic的数据通道供应用程序间通信之用,那么只需简单输入下列语句即可达成所愿: ```bash bin/kafka-topics.sh --create --topic test-topic --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1 ``` 以上就是关于Kafka `2.11-2.4.0` 版本的基础安装流程概述及其初步使用的指导说明[^4]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值