Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(source-generator): add keyField #97

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 13 additions & 34 deletions source-generator/app/src/main/java/source/GeneratorSourceTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class GeneratorSourceTask implements SourceTask {
KvStore kv;
String stream;
Supplier<Record> generator;
String keyField;

@SneakyThrows
@Override
Expand All @@ -35,33 +36,19 @@ public void run(HRecord cfg, SourceTaskContext ctx) {
if (cfg.contains("schema")) {
schema = cfg.getString("schema");
}
log.info("schema:{}", schema);
if (cfg.contains("keyField")) {
keyField = cfg.getString("keyField");
}
log.info("schema:{}, keyField:{}", schema, keyField);
assert batchSize > 0;
assert period > 0;
var dataType = DataType.JSON;
if (cfg.contains("type") && cfg.getString("type").equalsIgnoreCase("sequence")) {
dataType = DataType.SEQUENCE;
}
var seq = 0;
if (dataType.equals(DataType.SEQUENCE)) {
var seqStr = kv.get("sequence").get();
if (seqStr != null && !seqStr.isEmpty()) {
seq = Integer.parseInt(seqStr);
}
generator = getSequenceGenerator(seq);
} else {
generator = getJsonGeneratorFromSchema(schema);
}
generator = getJsonGeneratorFromSchema(schema, keyField);
while (true) {
if (needStop) {
return;
}
Thread.sleep(period * 1000L);
writeRecords(batchSize);
if (dataType.equals(DataType.SEQUENCE)) {
seq += batchSize;
kv.set("sequence", String.valueOf(seq));
}
}
}

Expand All @@ -77,24 +64,16 @@ public JsonNode spec() {
}

@SneakyThrows
Supplier<Record> getJsonGeneratorFromSchema(String schemaStr) {
Supplier<Record> getJsonGeneratorFromSchema(String schemaStr, String keyField) {
var jsonFaker = new JsonFaker(schemaStr);
return () -> {
var jsonData = jsonFaker.generate();
return Record.newBuilder().hRecord(HRecord.newBuilder().merge(jsonData).build()).build();
};
}

Supplier<Record> getSequenceGenerator(int start) {
AtomicInteger id = new AtomicInteger(start);
return () -> {
var jsonData = mapper.createObjectNode()
.put("id", id.getAndIncrement())
.put("randomInt", rnd.nextInt(1000))
.put("randomSmallInt", rnd.nextInt(10))
.put("randomDate", Instant.now().toString())
.toString();
return Record.newBuilder().hRecord(HRecord.newBuilder().merge(jsonData).build()).build();
var hRecord = HRecord.newBuilder().merge(jsonData).build();
String key = null;
if (keyField != null) {
key = Utils.pbValueToObject(hRecord.getDelegate().getFieldsMap().get(keyField)).toString();
}
return Record.newBuilder().partitionKey(key).hRecord(hRecord).build();
};
}

Expand Down
18 changes: 11 additions & 7 deletions source-generator/app/src/main/resources/spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,38 @@
"type": "object",
"properties": {
"stream": {
"title": "stream",
"ui:showName": "stream",
"description": "result stream",
"type": "string"
},
"keyField": {
"ui:showName": "key field name",
"type": "string"
},
"schema": {
"title": "schema",
"ui:showName": "schema",
"type": "string",
"default": "{\n \"type\": \"object\",\n \"properties\": {\n \"k1\": {\n \"type\": \"integer\",\n \"minimum\": 10,\n \"maximum\": 20\n },\n \"v1\": {\n \"type\": \"string\"\n }\n },\n \"required\": [\"k1\", \"v1\"]\n}",
"ui:type": "editor",
"description": "json schema for generating random data"
},
"batchSize": {
"title": "batchSize",
"description": "batch size",
"ui:showName": "batchSize(records)",
"description": "write records/per interval",
"type": "integer",
"default": 1,
"minimum": 1,
"maximum": 10
},
"period": {
"title": "period",
"description": "write period",
"ui:showName": "period(Seconds)",
"description": "write period(interval), unit:seconds",
"type": "integer",
"default": 5,
"minimum": 1,
"maximum": 100
}
},
"required": ["stream", "batchSize", "period"],
"ui:order": ["stream", "schema", "batchSize", "period"]
"ui:order": ["stream", "keyField", "schema", "batchSize", "period"]
}
Loading