Defining a recordV2 schema
You can use the API to specify a schema that defines the content of a recordV2 topic.
Publishing clients can define an optional schema for a recordV2 topic. The topic value must conform to the schema.
No client session is required to create a schema.
The
Diffusion™
API for the following platforms provides builder
methods that enable you to define a schema:
- Java™
- Android™
The following example demonstrates how to create a schema using the Java API.
Example: A class that shows how to create different schemas.
package com.pushtechnology.client.sdk.manual; import com.pushtechnology.diffusion.client.Diffusion; import com.pushtechnology.diffusion.datatype.recordv2.RecordV2DataType; import com.pushtechnology.diffusion.datatype.recordv2.schema.Schema; import com.pushtechnology.diffusion.datatype.recordv2.schema.SchemaBuilder; /** * Code snippets for DITA Developer Guide. * * @author DiffusionData Limited */ public final class CreatingSchemaDefinition { private final RecordV2DataType dataType = Diffusion.dataTypes().recordV2(); /** * Constructor. */ public CreatingSchemaDefinition() { } /** * Example of a schema consisting of a single record with three fields each * of s different data type. * * @return a schema */ public Schema createSimpleSchema() { final SchemaBuilder builder = dataType.schemaBuilder(); return builder .record("Record") .string("string").integer("integer").decimal("decimal", 3) .build(); } /** * Example of a schema consisting of multiple records, each record with a * single field of a specific type. * * @return a schema */ public Schema createMultipleRecordsSchema() { final SchemaBuilder builder = dataType.schemaBuilder(); return builder .record("StringRecord").string("string") .record("IntegerRecord").integer("integer") .record("DecimalRecord").decimal("decimal", 3) .build(); } /** * Example of a schema consisting of a record (with a single string field) * repeating exactly 10 times. * * @return a schema */ public Schema createFixedRepeatingRecordsSchema() { final SchemaBuilder builder = dataType.schemaBuilder(); return builder .record("RepeatingRecord", 10).string("string") .build(); } /** * Example of a schema consisting of 2 record types. "FixedRecord" is a * record that occurs 5 times. "RepeatingRecord" is an optional record that * can be repeated as many times as required (unlimited). * * @return a schema */ public Schema createVariableRepeatingRecordsSchema() { final SchemaBuilder builder = dataType.schemaBuilder(); return builder .record("FixedRecord", 5).string("a") .record("RepeatingRecord", 0, -1).string("b") .build(); } /** * Example of a schema consisting of a single record with a string field * that occurs exactly 10 times. * * @return a schema */ public Schema createFixedRepeatingFieldsSchema() { final SchemaBuilder builder = dataType.schemaBuilder(); return builder .record("Record").string("repeatingString", 10) .build(); } /** * Example of a schema consisting of two records. The first record (A) has a * field, "repeatingField", which can occur between 2 and 5 times. The * second record (B) has a field, "repeatingFieldUnlimited", which can occur * as many times as required but at least once. * * @return a schema */ public Schema createVariableRepeatingFieldsSchema() { final SchemaBuilder builder = dataType.schemaBuilder(); return builder .record("A").string("repeatingField", 2, 5) .record("B").string("repeatingFieldUnlimited", 1, -1) .build(); } /** * Example of a schema consisting of a single record and multiple fields * encapsulating a person's name and address. * * @return a schema */ public Schema createNameAndAddressSchema() { final SchemaBuilder builder = dataType.schemaBuilder(); return builder .record("nameAndAddress") .string("firstName") .string("surname") .integer("houseNumber") .string("street") .string("town") .string("state") .string("postCode") .build(); } }
Note: RecordV2 topics only exist to aid the migration of users of
legacy record topics, and will be deprecated in the future. It is highly recommended
that JSON topics are used in preference.