-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema-registry.ts
More file actions
108 lines (92 loc) · 2.98 KB
/
Copy pathschema-registry.ts
File metadata and controls
108 lines (92 loc) · 2.98 KB
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
/**
* Schema Registry example demonstrating Avro schema management and
* validated produce/consume with the Streamline Node.js SDK.
*
* Ensure a Streamline server is running at localhost:9092 with the
* schema registry enabled on port 9094 before running:
* streamline --playground
*
* Run with:
* npx tsx examples/schema-registry.ts
*/
import { Streamline, SchemaRegistry } from 'streamline';
// Avro schema for a User record
const USER_SCHEMA = JSON.stringify({
type: 'record',
name: 'User',
namespace: 'com.streamline.examples',
fields: [
{ name: 'id', type: 'int' },
{ name: 'name', type: 'string' },
{ name: 'email', type: 'string' },
{ name: 'created_at', type: 'string' },
],
});
const SUBJECT = 'users-value';
const TOPIC = 'users';
interface User {
id: number;
name: string;
email: string;
created_at: string;
}
async function main() {
// === 1. Create the Streamline client and schema registry ===
const client = new Streamline(
process.env.STREAMLINE_BOOTSTRAP_SERVERS || 'localhost:9092',
{
httpEndpoint: 'http://localhost:9094',
clientId: 'node-schema-example',
},
);
const registry = new SchemaRegistry(
process.env.STREAMLINE_SCHEMA_REGISTRY_URL || 'http://localhost:9094',
);
await client.connect();
console.log('Connected to Streamline');
// === 2. Register an Avro schema ===
console.log('\n=== Registering Schema ===');
const schemaId = await registry.register(SUBJECT, USER_SCHEMA, 'AVRO');
console.log(`Registered schema with id=${schemaId} for subject=${SUBJECT}`);
// Retrieve the schema back by id
const retrieved = await registry.getSchema(schemaId);
console.log(`Retrieved schema: ${retrieved}`);
// === 3. Check schema compatibility ===
console.log('\n=== Checking Compatibility ===');
const compatible = await registry.checkCompatibility(SUBJECT, USER_SCHEMA, 'AVRO');
console.log(`Schema compatible: ${compatible}`);
// === 4. Produce messages with schema validation ===
console.log('\n=== Producing Messages with Schema ===');
for (let i = 0; i < 5; i++) {
const user: User = {
id: i,
name: `user-${i}`,
email: `user${i}@example.com`,
created_at: '2025-01-15T10:00:00Z',
};
const result = await client.produce(TOPIC, user, {
key: `user-${i}`,
schemaId,
});
console.log(
`Produced user-${i} at partition=${result.partition}, offset=${result.offset}`,
);
}
// === 5. Consume and deserialize with schema ===
console.log('\n=== Consuming Messages with Schema ===');
const messages = await client.consumeBatch(TOPIC, {
fromBeginning: true,
maxMessages: 10,
schemaRegistry: registry,
});
for (const msg of messages) {
const user = msg.value as User;
console.log(
`Received: partition=${msg.partition}, offset=${msg.offset}, ` +
`user={id:${user.id}, name:${user.name}, email:${user.email}}`,
);
}
await client.close();
console.log('\nDone!');
}
main().catch(console.error);