-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkafka-tool.js
291 lines (270 loc) · 9.49 KB
/
kafka-tool.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
'use strict';
const async = require('async');
const fs = require('fs');
const program = require('commander');
const kafka = require('kafka-node');
const topics = [];
const groups = [];
function collectTopics(topic) {
topics.push(topic);
}
function collectGroups(group) {
groups.push(group);
}
function dumpTopics() {
const dumperVars = topics.map(topic => ({
topic,
outFile: `./${topic}.log`,
nbConsumedMessages: 0,
}));
console.log('initializing consumers...');
async.each(dumperVars, (vars, done) => {
vars.stream = fs.createWriteStream(vars.outFile, { flags: 'a' });
vars.consumer = new kafka.ConsumerGroup({
host: program.zookeeper,
groupId: 'debugging-consumer',
fromOffset: 'latest',
autoCommit: true,
fetchMaxBytes: 100000,
}, vars.topic);
vars.consumer.on('error', err => {
console.error(`error in consumer of ${vars.topic} topic: ${err.message}`);
});
vars.consumer.once('connect', () => {
console.log(` ${vars.topic} => ${vars.outFile}`);
vars.consumer.on('message', entry => {
vars.stream.write(`${Date.now()},${entry.value}\n`, 'utf8');
++vars.nbConsumedMessages;
});
done();
});
}, () => {
process.on('SIGINT', () => {
console.log('closing consumers and log files...');
async.each(dumperVars, (vars, done) => {
vars.consumer.close(true, () => {
console.log(` ${vars.outFile}: ${vars.nbConsumedMessages} messages dumped`);
vars.stream.on('finish', done);
vars.stream.end();
});
}, () => {
process.exit(0);
});
});
});
}
function getLatestOffset() {
let client;
let offset;
let topicsMd;
let topicsOffsets;
async.series([
next => {
client = new kafka.Client(program.zookeeper);
client.on('connect', next);
},
next => client.loadMetadataForTopics(topics, (err, res) => {
if (err) {
console.error('error loading metadata for topics:', err);
return next(err);
}
topicsMd = res[1].metadata;
return next();
}),
next => {
offset = new kafka.Offset(client);
offset.fetchLatestOffsets(topics, (err, offsetRes) => {
if (err) {
console.error('error fetching topic offsets:', err);
return next(err);
}
topicsOffsets = offsetRes;
return next();
});
},
next => {
topics.forEach(topic => {
const topicOffsets = topicsOffsets[topic];
console.log(`latest offsets for topic ${topic}:`);
Object.keys(topicOffsets).forEach(partNum => {
const partOffset = topicOffsets[partNum];
console.log(` partition ${partNum}: offset=${partOffset}`);
});
});
client.close();
return next();
},
], err => {
process.exit(err ? 1 : 0);
});
}
function getConsumerGroupOffset(options) {
if (!options.group) {
console.log('you need to specify a consumer group with --group');
process.exit(1);
}
async.each(topics, (topic, done) => {
let consumer;
async.series([
next => {
consumer = new kafka.ConsumerGroup({
host: program.zookeeper,
groupId: options.group,
fromOffset: 'none',
autoCommit: false,
fetchMaxBytes: 100000,
}, topic);
consumer.on('error', err => {
console.error(`error in consumer of ${topic} topic: ` +
`${err.message}`);
return next(err);
});
return consumer.once('connect', next);
},
next => {
const payloads = consumer.getTopicPayloads();
if (payloads) {
payloads.forEach(payload => {
console.log(` topic ${payload.topic}, ` +
`partition ${payload.partition}: ` +
`offset=${payload.offset}`);
});
}
consumer.close(next);
},
], done);
}, () => {});
}
function setConsumerGroupOffset(options) {
if (!options.group) {
console.log('you need to specify a consumer group with --group');
process.exit(1);
}
let client;
let offset;
let topicsMd;
let topicsOffsets;
let consumer;
async.series([
next => {
client = new kafka.Client(program.zookeeper);
client.on('connect', next);
},
next => client.loadMetadataForTopics(topics, (err, res) => {
if (err) {
console.error('error loading metadata for topics:', err);
return next(err);
}
topicsMd = res[1].metadata;
return next();
}),
next => {
offset = new kafka.Offset(client);
offset.fetchLatestOffsets(topics, (err, offsetRes) => {
if (err) {
console.error('error fetching topic offsets:', err);
return next(err);
}
topicsOffsets = offsetRes;
return next();
});
},
next => {
consumer = new kafka.ConsumerGroup({
host: program.zookeeper,
groupId: options.group,
fromOffset: 'none',
autoCommit: false,
fetchMaxBytes: 100000,
}, topics);
consumer.on('error', err => {
console.error(`error in consumer of ${topics} topics: ` +
`${err.message}`);
return next(err);
});
return consumer.once('connect', next);
},
next => {
topics.forEach(topic => {
const topicOffsets = topicsOffsets[topic];
console.log('setting latest offsets on group ' +
`${options.group} for topic ${topic}:`);
Object.keys(topicOffsets).forEach(partNum => {
const partOffset = topicOffsets[partNum];
console.log(` partition ${partNum}: offset=${partOffset}`);
consumer.setOffset(topic, partNum, partOffset);
});
});
consumer.commit(next);
},
next => consumer.close(true, next),
next => client.close(next),
], err => {
process.exit(err ? 1 : 0);
});
}
function describeGroups(options) {
let client;
let admin;
async.series([
next => {
client = new kafka.KafkaClient({ kafkaHost: options.kafka });
client.once('connect', next);
},
next => {
admin = new kafka.Admin(client);
admin.describeGroups(groups, (err, res) => {
if (err) {
console.error('error describing groups', groups,
`: ${err.message}`);
return next(err);
}
groups.forEach(group => {
console.log(`description of group ${group}:`);
const groupDesc = res[group];
['error', 'state', 'protocolType',
'protocol', 'brokerId'].forEach(attr => {
console.log(` ${attr}: ${groupDesc[attr]}`);
});
console.log(' members:');
groupDesc.members.forEach(member => {
console.log(` memberId: ${member.memberId}`);
['clientId', 'clientHost',
'memberMetadata', 'memberAssignment'].forEach(
attr => console.log(` ${attr}: ` +
`${JSON.stringify(member[attr])}`));
});
});
return next();
});
},
], err => {
process.exit(err ? 1 : 0);
});
}
program
.option('--zookeeper <connectString>',
'zookeeper connect string (e.g. "localhost:2181")')
.option('--topic [topic]', 'consume topic', collectTopics, []);
program
.command('dump-topics')
.description('dump kafka topic messages into separate local log files')
.action(dumpTopics);
program
.command('get-latest-offset')
.description('get latest offset of topic partitions')
.action(getLatestOffset);
program
.command('get-consumer-group-offset')
.option('--group <groupId>', 'set group Id')
.action(getConsumerGroupOffset);
program
.command('set-consumer-group-offset')
.option('--group <groupId>', 'set group Id')
.action(setConsumerGroupOffset);
program
.command('describe-groups')
.option('--kafka <kafka>', 'set kafka broker ip/port (e.g. "localhost:9092")')
.option('--group <groupId>', 'set group Id', collectGroups, [])
.action(describeGroups);
program.parse(process.argv);