当前位置: 首页 > news >正文

kafka基本命令

!!!!!先说一个容易失败的地方,如果是Windows必须要把下载下来的kafka改一下名字,去掉后面的版本号,不然后面运行命令的时候,他会出现命令过长导致不能运行,非常重要

配置kafka模式

生成集群id

bin\windows\kafka-storage.bat random-uuid

手动赋值

set KAFKA_CLUSTER_ID=这里粘贴你拿到的uuid字符串

格式化存储目录,自己替换一下集群id就可以f-z1egYyST0pH5ivw7xulQ替换掉

bin\windows\kafka-storage.bat format -t f-z1egYyST0pH5ivw7xulQ -c config\kraft\server.properties

启动kafka

bin\windows\kafka-server-start.bat config\kraft\server.properties

bin\windows\kafka-server-start.bat config\kraft\server.properties

配置脚本:Windows用ps1(一开始想用bat但是bat不好用该用ps1,bat文件你就不用管,复制下面代码粘进去)

# Kafka KRaft mode startup script for Windows PowerShell # Run this script from the Kafka root directory. $ErrorActionPreference = "Stop" $CurrentRoot = [System.IO.Path]::GetPathRoot((Get-Location).Path) $LogDir = Join-Path $CurrentRoot "tmp\kraft-combined-logs" $ConfigFile = "config\kraft\server.properties" $KafkaStorage = ".\bin\windows\kafka-storage.bat" $KafkaServerStart = ".\bin\windows\kafka-server-start.bat" Write-Host "=== Kafka KRaft Mode Startup Script ===" Write-Host "Current time: $(Get-Date)" Write-Host "Working directory: $(Get-Location)" Write-Host "" function Cleanup-Logs { if (Test-Path -LiteralPath $LogDir) { Write-Host "Existing log directory found. Cleaning..." Remove-Item -LiteralPath $LogDir -Recurse -Force Write-Host "Log directory cleaned: $LogDir" } else { Write-Host "Log directory does not exist. No cleanup needed." } } function Format-Storage { Write-Host "" Write-Host "=== Step 1: Generate cluster ID ===" $KafkaClusterId = (& $KafkaStorage random-uuid).Trim() Write-Host "Generated cluster ID: $KafkaClusterId" Write-Host "" Write-Host "=== Step 2: Format storage directory ===" & $KafkaStorage format -t $KafkaClusterId -c $ConfigFile if ($LASTEXITCODE -eq 0) { Write-Host "Storage formatted successfully." return $true } Write-Host "Storage format failed. This may be caused by a cluster ID conflict." return $false } function Start-Kafka { Write-Host "" Write-Host "=== Step 3: Start Kafka server ===" Write-Host "Config file: $ConfigFile" Write-Host "Starting Kafka..." Write-Host "Tip: Press Ctrl+C to stop the server." Write-Host "" & $KafkaServerStart $ConfigFile } function Main { if (-not (Format-Storage)) { Write-Host "" Write-Host "Cluster ID conflict detected. Cleaning logs and retrying..." Cleanup-Logs Write-Host "" Write-Host "=== Retry storage format ===" if (-not (Format-Storage)) { Write-Host "Still failed after retry. Please check your Kafka config." exit 1 } } Start-Kafka } if (-not (Test-Path -LiteralPath $ConfigFile)) { Write-Host "Config file does not exist: $ConfigFile" exit 1 } if (-not (Test-Path -LiteralPath $KafkaStorage)) { Write-Host "kafka-storage.bat does not exist. Please run this script from the Kafka root directory." exit 1 } if (-not (Test-Path -LiteralPath $KafkaServerStart)) { Write-Host "kafka-server-start.bat does not exist. Please check your Kafka installation." exit 1 } Main

注意:运用脚本启动的时候就不要用前面的命令启动了,前面命令启动的也要关掉,否则第三步会失败

验证安装:

bin\windows\kafka-topics.bat --create --topic quickstart-events --bootstrap-server localhost:9092

创建主题:

bin\windows\kafka-topics.bat --create --topic quickstart-events --bootstrap-server localhost:9092

查看全部主题列表

bin\windows\kafka-topics.bat --list --bootstrap-server localhost:9092

查看指定主题详细

bin\windows\kafka-topics.bat --describe --topic quickstart-events --bootstrap-server localhost:9092

测试生产者和消费者(中文传输会乱码)

bin\windows\kafka-console-producer.bat --topic quickstart-events --bootstrap-server localhost:9092
bin\windows\kafka-console-consumer.bat --topic quickstart-events --from-beginning --bootstrap-server localhost:9092

创建聪明需要的主题

创建文件处理主题

bin\windows\kafka-topics.bat --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic file-processing

创建向量化主题

bin\windows\kafka-topics.bat --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic vectorization

创建死信队列主题

bin\windows\kafka-topics.bat --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic file-processing-dlt

关闭的时候最好ctrl+c关闭,异常关闭下次启动会异常

如果异常关闭了:

删掉,重新来一次就重新生成了tmp里面的文件

http://www.jsqmd.com/news/1289825/

相关文章:

  • Java 23 种设计模式:从踩坑到精通 | 番外:享元模式 —— 物流包装材料管理实战
  • Apicurio Registry健康检查:监控系统状态的终极指南
  • AG Kit性能分析工具:识别AI Agent系统瓶颈的方法
  • 东北的夏天已经长期维持在35摄氏度以上,这正常吗
  • PotatoNV终极指南:华为麒麟设备Bootloader解锁深度剖析
  • 军工六性设计:可靠性、维修性、测试性、保障性、安全性、环境适应性全解析
  • 第二周 题目练习2(stack综合 单调栈)牛客 14326. 14666. 15029
  • 2026教育行业招生增效优选:AI搜索优化服务商精选解析与合作避坑指南 - 商业大观
  • EventBus事件模型详解:id、topic、data与可选属性的最佳配置
  • 毕业救命神器✨Paperxie一站式论文工具|写论文再也不用熬夜内耗啦!
  • 提示词拆解失败率高达82%?分步骤执行的7个原子级动作,资深Prompt工程师私藏清单
  • 3分钟解锁Windows新玩法:用APK Installer直接运行Android应用
  • 2026上海日本语言学校申请机构排名优选推荐 - 谁都没有我好看
  • 亚太地区全球名义雇主服务的出海新机遇
  • FFTformer-GoPro-fp32常见问题解答:解决你的部署、性能与精度困惑
  • 嗜神经性病毒RABV
  • 快速上手PoseEstimation-CoreML:零基础实现手机端姿态检测
  • 2026 武汉专升本正规机构大盘点!靠谱备考优先选武汉初阳教育专升本 - Luckyone王
  • Tanker SDK-js未来路线图:即将发布的5大令人期待的新功能
  • ubantu 24.04.4离线安装k3s
  • PhotoRec数据恢复工具:开源免费的硬盘数据拯救专家
  • 3分钟免费解锁鸣潮120帧:WaveTools工具箱完整使用指南
  • AC自动机 学习笔记
  • 拯救消失的网页记忆:Wayback Machine浏览器扩展完全指南
  • 为什么需要人在回路?达尔文.skill独特的三层守关机制详解
  • 如何快速部署MetaTube插件:Jellyfin智能元数据刮削终极指南
  • 芯聆CLD6255(4 x 190W, 2 x 380W 或 2.1 模式 (2x190W + 1x380W ) @1% THD 数字输入 D 类音频播放器)
  • 命令行生成BIN文件并修改内容后与固件合并烧录至芯片
  • 软件测试工程师到底是做什么的?一文讲清职责、技能与成长路径
  • 揭秘XStreaming背后的WebRTC技术:打造稳定流畅的串流体验