Java Code Examples for org.apache.pulsar.client.api.Schema#generic()
The following examples show how to use
org.apache.pulsar.client.api.Schema#generic() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SchemaBuilderTest.java From pulsar with Apache License 2.0 | 4 votes |
@Test public void testGenericRecordBuilderAvroByFieldname() { RecordSchemaBuilder people1SchemaBuilder = SchemaBuilder.record("People1"); people1SchemaBuilder.field("age").type(SchemaType.INT32); people1SchemaBuilder.field("height").type(SchemaType.INT32); people1SchemaBuilder.field("name").type(SchemaType.STRING); SchemaInfo people1SchemaInfo = people1SchemaBuilder.build(SchemaType.AVRO); GenericSchema people1Schema = Schema.generic(people1SchemaInfo); GenericRecordBuilder people1RecordBuilder = people1Schema.newRecordBuilder(); people1RecordBuilder.set("age", 20); people1RecordBuilder.set("height", 180); people1RecordBuilder.set("name", "people1"); GenericRecord people1GenericRecord = people1RecordBuilder.build(); RecordSchemaBuilder people2SchemaBuilder = SchemaBuilder.record("People2"); people2SchemaBuilder.field("age").type(SchemaType.INT32); people2SchemaBuilder.field("height").type(SchemaType.INT32); people2SchemaBuilder.field("name").type(SchemaType.STRING); SchemaInfo people2SchemaInfo = people2SchemaBuilder.build(SchemaType.AVRO); GenericSchema people2Schema = Schema.generic(people2SchemaInfo); GenericRecordBuilder people2RecordBuilder = people2Schema.newRecordBuilder(); people2RecordBuilder.set("age", 20); people2RecordBuilder.set("height", 180); people2RecordBuilder.set("name", "people2"); GenericRecord people2GenericRecord = people2RecordBuilder.build(); RecordSchemaBuilder peopleSchemaBuilder = SchemaBuilder.record("People"); peopleSchemaBuilder.field("people1", people1Schema).type(SchemaType.AVRO); peopleSchemaBuilder.field("people2", people2Schema).type(SchemaType.AVRO); peopleSchemaBuilder.field("name").type(SchemaType.STRING); SchemaInfo schemaInfo = peopleSchemaBuilder.build(SchemaType.AVRO); GenericSchema peopleSchema = Schema.generic(schemaInfo); GenericRecordBuilder peopleRecordBuilder = peopleSchema.newRecordBuilder(); peopleRecordBuilder.set("people1", people1GenericRecord); peopleRecordBuilder.set("people2", people2GenericRecord); peopleRecordBuilder.set("name", "people"); GenericRecord peopleRecord = peopleRecordBuilder.build(); byte[] peopleEncode = peopleSchema.encode(peopleRecord); GenericRecord people = (GenericRecord) peopleSchema.decode(peopleEncode); assertEquals(people.getFields(), peopleRecord.getFields()); assertEquals((people.getField("name")), peopleRecord.getField("name")); assertEquals(((GenericRecord)people.getField("people1")).getField("age"), people1GenericRecord.getField("age")); assertEquals(((GenericRecord)people.getField("people1")).getField("heigth"), people1GenericRecord.getField("heigth")); assertEquals(((GenericRecord)people.getField("people1")).getField("name"), people1GenericRecord.getField("name")); assertEquals(((GenericRecord)people.getField("people2")).getField("age"), people2GenericRecord.getField("age")); assertEquals(((GenericRecord)people.getField("people2")).getField("height"), people2GenericRecord.getField("height")); assertEquals(((GenericRecord)people.getField("people2")).getField("name"), people2GenericRecord.getField("name")); }
Example 2
Source File: SchemaBuilderTest.java From pulsar with Apache License 2.0 | 4 votes |
@Test public void testGenericRecordBuilderAvroByFieldnamePojo() { RecordSchemaBuilder people1SchemaBuilder = SchemaBuilder.record("People1"); people1SchemaBuilder.field("age").type(SchemaType.INT32); people1SchemaBuilder.field("height").type(SchemaType.INT32); people1SchemaBuilder.field("name").type(SchemaType.STRING); SchemaInfo people1SchemaInfo = people1SchemaBuilder.build(SchemaType.AVRO); GenericSchema people1Schema = Schema.generic(people1SchemaInfo); GenericRecordBuilder people1RecordBuilder = people1Schema.newRecordBuilder(); people1RecordBuilder.set("age", 20); people1RecordBuilder.set("height", 180); people1RecordBuilder.set("name", "people1"); GenericRecord people1GenericRecord = people1RecordBuilder.build(); RecordSchemaBuilder people2SchemaBuilder = SchemaBuilder.record("People2"); people2SchemaBuilder.field("age").type(SchemaType.INT32); people2SchemaBuilder.field("height").type(SchemaType.INT32); people2SchemaBuilder.field("name").type(SchemaType.STRING); SchemaInfo people2SchemaInfo = people2SchemaBuilder.build(SchemaType.AVRO); GenericSchema people2Schema = Schema.generic(people2SchemaInfo); GenericRecordBuilder people2RecordBuilder = people2Schema.newRecordBuilder(); people2RecordBuilder.set("age", 20); people2RecordBuilder.set("height", 180); people2RecordBuilder.set("name", "people2"); GenericRecord people2GenericRecord = people2RecordBuilder.build(); RecordSchemaBuilder peopleSchemaBuilder = SchemaBuilder.record("People"); peopleSchemaBuilder.field("people1", people1Schema).type(SchemaType.AVRO); peopleSchemaBuilder.field("people2", people2Schema).type(SchemaType.AVRO); peopleSchemaBuilder.field("name").type(SchemaType.STRING); SchemaInfo schemaInfo = peopleSchemaBuilder.build(SchemaType.AVRO); GenericSchema peopleSchema = Schema.generic(schemaInfo); GenericRecordBuilder peopleRecordBuilder = peopleSchema.newRecordBuilder(); peopleRecordBuilder.set("people1", people1GenericRecord); peopleRecordBuilder.set("people2", people2GenericRecord); peopleRecordBuilder.set("name", "people"); GenericRecord peopleRecord = peopleRecordBuilder.build(); byte[] peopleEncode = peopleSchema.encode(peopleRecord); Schema<People> peopleDecodeSchema = Schema.AVRO( SchemaDefinition.<People>builder().withPojo(People.class).withAlwaysAllowNull(false).build()); People people = peopleDecodeSchema.decode(peopleEncode); assertEquals(people.name, peopleRecord.getField("name")); assertEquals(people.getPeople1().age, people1GenericRecord.getField("age")); assertEquals(people.getPeople1().height, people1GenericRecord.getField("height")); assertEquals(people.getPeople1().name, people1GenericRecord.getField("name")); assertEquals(people.getPeople2().age, people2GenericRecord.getField("age")); assertEquals(people.getPeople2().height, people2GenericRecord.getField("height")); assertEquals(people.getPeople2().name, people2GenericRecord.getField("name")); }
Example 3
Source File: SchemaBuilderTest.java From pulsar with Apache License 2.0 | 4 votes |
@Test public void testGenericRecordBuilderAvroByFieldIndex() { RecordSchemaBuilder people1SchemaBuilder = SchemaBuilder.record("People1"); people1SchemaBuilder.field("age").type(SchemaType.INT32); people1SchemaBuilder.field("height").type(SchemaType.INT32); people1SchemaBuilder.field("name").type(SchemaType.STRING); SchemaInfo people1SchemaInfo = people1SchemaBuilder.build(SchemaType.AVRO); GenericSchema<GenericRecord> people1Schema = Schema.generic(people1SchemaInfo); GenericRecordBuilder people1RecordBuilder = people1Schema.newRecordBuilder(); people1RecordBuilder.set(people1Schema.getFields().get(0), 20); people1RecordBuilder.set(people1Schema.getFields().get(1), 180); people1RecordBuilder.set(people1Schema.getFields().get(2), "people1"); GenericRecord people1GenericRecord = people1RecordBuilder.build(); RecordSchemaBuilder people2SchemaBuilder = SchemaBuilder.record("People2"); people2SchemaBuilder.field("age").type(SchemaType.INT32); people2SchemaBuilder.field("height").type(SchemaType.INT32); people2SchemaBuilder.field("name").type(SchemaType.STRING); SchemaInfo people2SchemaInfo = people2SchemaBuilder.build(SchemaType.AVRO); GenericSchema<GenericRecord> people2Schema = Schema.generic(people2SchemaInfo); GenericRecordBuilder people2RecordBuilder = people2Schema.newRecordBuilder(); people2RecordBuilder.set(people2Schema.getFields().get(0), 20); people2RecordBuilder.set(people2Schema.getFields().get(1), 180); people2RecordBuilder.set(people2Schema.getFields().get(2), "people2"); GenericRecord people2GenericRecord = people2RecordBuilder.build(); RecordSchemaBuilder peopleSchemaBuilder = SchemaBuilder.record("People"); peopleSchemaBuilder.field("people1", people1Schema).type(SchemaType.AVRO); peopleSchemaBuilder.field("people2", people2Schema).type(SchemaType.AVRO); peopleSchemaBuilder.field("name").type(SchemaType.STRING); SchemaInfo schemaInfo = peopleSchemaBuilder.build(SchemaType.AVRO); GenericSchema<GenericRecord> peopleSchema = Schema.generic(schemaInfo); GenericRecordBuilder peopleRecordBuilder = peopleSchema.newRecordBuilder(); peopleRecordBuilder.set(peopleSchema.getFields().get(0), people1GenericRecord); peopleRecordBuilder.set(peopleSchema.getFields().get(1), people2GenericRecord); peopleRecordBuilder.set(peopleSchema.getFields().get(2), "people"); GenericRecord peopleRecord = peopleRecordBuilder.build(); byte[] peopleEncode = peopleSchema.encode(peopleRecord); GenericRecord people = (GenericRecord) peopleSchema.decode(peopleEncode); assertEquals(people.getFields(), peopleRecord.getFields()); assertEquals((people.getField("name")), peopleRecord.getField("name")); assertEquals(((GenericRecord)people.getField("people1")).getField("age"), people1GenericRecord.getField("age")); assertEquals(((GenericRecord)people.getField("people1")).getField("heigth"), people1GenericRecord.getField("heigth")); assertEquals(((GenericRecord)people.getField("people1")).getField("name"), people1GenericRecord.getField("name")); assertEquals(((GenericRecord)people.getField("people2")).getField("age"), people2GenericRecord.getField("age")); assertEquals(((GenericRecord)people.getField("people2")).getField("height"), people2GenericRecord.getField("height")); assertEquals(((GenericRecord)people.getField("people2")).getField("name"), people2GenericRecord.getField("name")); }
Example 4
Source File: SchemaBuilderTest.java From pulsar with Apache License 2.0 | 4 votes |
@Test public void testGenericRecordBuilderAvroByFieldIndexPojo() { RecordSchemaBuilder people1SchemaBuilder = SchemaBuilder.record("People1"); people1SchemaBuilder.field("age").type(SchemaType.INT32); people1SchemaBuilder.field("height").type(SchemaType.INT32); people1SchemaBuilder.field("name").type(SchemaType.STRING); SchemaInfo people1SchemaInfo = people1SchemaBuilder.build(SchemaType.AVRO); GenericSchema<GenericRecord> people1Schema = Schema.generic(people1SchemaInfo); GenericRecordBuilder people1RecordBuilder = people1Schema.newRecordBuilder(); people1RecordBuilder.set(people1Schema.getFields().get(0), 20); people1RecordBuilder.set(people1Schema.getFields().get(1), 180); people1RecordBuilder.set(people1Schema.getFields().get(2), "people1"); GenericRecord people1GenericRecord = people1RecordBuilder.build(); RecordSchemaBuilder people2SchemaBuilder = SchemaBuilder.record("People2"); people2SchemaBuilder.field("age").type(SchemaType.INT32); people2SchemaBuilder.field("height").type(SchemaType.INT32); people2SchemaBuilder.field("name").type(SchemaType.STRING); SchemaInfo people2SchemaInfo = people2SchemaBuilder.build(SchemaType.AVRO); GenericSchema<GenericRecord> people2Schema = Schema.generic(people2SchemaInfo); GenericRecordBuilder people2RecordBuilder = people2Schema.newRecordBuilder(); people2RecordBuilder.set(people2Schema.getFields().get(0), 20); people2RecordBuilder.set(people2Schema.getFields().get(1), 180); people2RecordBuilder.set(people2Schema.getFields().get(2), "people2"); GenericRecord people2GenericRecord = people2RecordBuilder.build(); RecordSchemaBuilder peopleSchemaBuilder = SchemaBuilder.record("People"); peopleSchemaBuilder.field("people1", people1Schema).type(SchemaType.AVRO); peopleSchemaBuilder.field("people2", people2Schema).type(SchemaType.AVRO); peopleSchemaBuilder.field("name").type(SchemaType.STRING); SchemaInfo schemaInfo = peopleSchemaBuilder.build(SchemaType.AVRO); GenericSchema<GenericRecord> peopleSchema = Schema.generic(schemaInfo); GenericRecordBuilder peopleRecordBuilder = peopleSchema.newRecordBuilder(); peopleRecordBuilder.set(peopleSchema.getFields().get(0), people1GenericRecord); peopleRecordBuilder.set(peopleSchema.getFields().get(1), people2GenericRecord); peopleRecordBuilder.set(peopleSchema.getFields().get(2), "people"); GenericRecord peopleRecord = peopleRecordBuilder.build(); byte[] peopleEncode = peopleSchema.encode(peopleRecord); Schema<People> peopleDecodeSchema = Schema.AVRO( SchemaDefinition.<People>builder().withPojo(People.class).withAlwaysAllowNull(false).build()); People people = peopleDecodeSchema.decode(peopleEncode); assertEquals(people.name, peopleRecord.getField("name")); assertEquals(people.getPeople1().age, people1GenericRecord.getField("age")); assertEquals(people.getPeople1().height, people1GenericRecord.getField("height")); assertEquals(people.getPeople1().name, people1GenericRecord.getField("name")); assertEquals(people.getPeople2().age, people2GenericRecord.getField("age")); assertEquals(people.getPeople2().height, people2GenericRecord.getField("height")); assertEquals(people.getPeople2().name, people2GenericRecord.getField("name")); }