Skip to content

Instantly share code, notes, and snippets.

@brachi-wernick
Created November 20, 2018 20:21
Show Gist options
  • Save brachi-wernick/3a5bc50c27e2b861e0910ae279a4ba5d to your computer and use it in GitHub Desktop.
Save brachi-wernick/3a5bc50c27e2b861e0910ae279a4ba5d to your computer and use it in GitHub Desktop.
buildRecordWithSchema.java
private static GenericRecord buildRecord(LoginAttackCount loginAttackCount) throws IOException {
// avro schema avsc file path.
String schemaPath = "src/main/java/loginStream/login-attack-count.avsc";
String schemaString;
try (FileInputStream inputStream = new FileInputStream(schemaPath)) {
schemaString = org.apache.commons.io.IOUtils.toString(inputStream);
} catch (Exception e) {
throw new RuntimeException(e);
}
Schema schema = new Schema.Parser().parse(schemaString);
GenericData.Record record = new GenericData.Record(schema);
// put the elements according to the avro schema.
record.put("userName", loginAttackCount.userName);
record.put("counter", loginAttackCount.counter);
record.put("to", loginAttackCount.to);
record.put("start", loginAttackCount.start);
return record;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment