-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavro.js
86 lines (60 loc) · 1.57 KB
/
avro.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
const path = require('path')
const { Kafka, logLevel } = require('kafkajs')
const { SchemaRegistry, SchemaType, avdlToAVSCAsync } = require('@kafkajs/confluent-schema-registry')
const registry = new SchemaRegistry({
host: 'https://xxx.eu-west-2.aws.confluent.cloud',
auth: {
username: 'xxx',
password: 'xxx',
},
})
const kafka = new Kafka({
clientId: 'example-consumer',
brokers: ['xxx.eu-west-2.aws.confluent.cloud:9092'],
ssl: true,
sasl: {
mechanism: 'plain',
username: 'xxx',
password: 'xxx'
},
//logLevel: logLevel.DEBUG
})
const run = async () => {
const schema = `
{
"type": "record",
"name": "new",
"namespace": "mysubject",
"fields": [{ "type": "string", "name": "fullName" }]
}
`
// Register new schema
const { id } = registry.register({
type: SchemaType.AVRO,
schema
})
// Sleep to complete the registration
console.log("waiting for the registration to complete")
await sleep(5000)
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
// Get schmea ID with subject and version
const subject = 'mysubject.new'
const version = 1
const id3 = await registry.getRegistryId(subject, version)
console.log(id3)
// Get schmea latest ID with subject
const subject2 = 'mysubject.new'
const id2 = await registry.getLatestSchemaId(subject2)
console.log(id2)
// Get schmea with ID
const schemaout = await registry.getSchema(id2)
console.log(schemaout)
}
run().catch(async e => {
console.error(e)
process.exit(1)
})