-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmultiple.ts
71 lines (62 loc) · 1.76 KB
/
multiple.ts
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
import { Schema } from 'avsc';
import { ObjectReadableMock } from 'stream-mock';
import { createCastleStream } from '@ovotech/castle-stream';
import { CastleStreamConsumerConfig } from '../src';
export interface Event1 {
field1: string;
}
export interface Event2 {
field2: number;
}
export const Event1Schema: Schema = {
type: 'record',
name: 'Event',
fields: [{ name: 'field1', type: 'string' }],
};
export const Event2Schema: Schema = {
type: 'record',
name: 'Event',
fields: [{ name: 'field2', type: 'int' }],
};
const consumer1: CastleStreamConsumerConfig<string, Event1> = {
topic: 'test1',
source: new ObjectReadableMock(['test1', 'test2', 'test3']),
eachMessage: async ({ message }) => {
console.log(message);
},
toKafkaMessage: (message) => ({
key: Buffer.from(''),
value: { field1: message },
schema: Event1Schema,
}),
};
/**
* We can configure additional details of each message, like partition, offset, keys or other metadata and consume it in batches
*/
const consumer2: CastleStreamConsumerConfig<{ p: number; m: number; o: number }, Event2> = {
topic: 'test2',
source: new ObjectReadableMock([
{ p: 0, m: 5, o: 0 },
{ p: 1, m: 6, o: 1 },
{ p: 0, m: 7, o: 0 },
]),
eachBatch: async ({ batch: { partition, messages } }) => {
console.log(`P: ${partition}`, messages);
},
toKafkaMessage: (message) => ({
partition: message.p,
key: Buffer.from(''),
value: { field2: message.m },
offset: String(message.o),
schema: Event1Schema,
}),
};
const main = async () => {
const castleStream = createCastleStream({
schemaRegistry: { uri: 'http://localhost:8081' },
kafka: { brokers: ['localhost:29092'] },
consumers: [consumer1, consumer2],
});
await castleStream.start();
};
main();