Formats and Schemas : Schemas : Schema Construction
 
Share this page                  
Schema Construction
Constructing a text schema is similar to constructing a record token type.
There are several ways to create a schema. If the schema is statically known, it can be declared using static convenience methods in the SchemaBuilder class.
Many of these methods collide with the methods in TokenTypeConstant, so take care when using them in conjunction with static imports.
Statically defining a text schema
import static com.pervasive.datarush.schema.SchemaBuilder.define;
import static com.pervasive.datarush.schema.SchemaBuilder.INT;
import static com.pervasive.datarush.schema.SchemaBuilder.STRING;

// File contains employee records with three fields: numeric id, first name, last name
TextRecord schema = define(INT("id"), STRING("firstName"), STRING("lastName"));
Alternatively, a schema can be constructed dynamically using a TextRecord (or FixedWidthTextRecord if using fixed-width fields).
Dynamically building a schema
// Create an employee record type with fields from input attributes
TextRecord schema = new TextRecord();
schema.defineField("id", TextTypes.JAVA_INT);
for (int i = 0; i < 20; i++ ) {
   schema.defineField("attr" + i, TextTypes.STRING);
}
It is also possible to convert record token types directly into schemas. Default text types will be used for each field.
Converting a record type to a schema
import static com.pervasive.datarush.types.TokenTypeConstant.record;
import static com.pervasive.datarush.types.TokenTypeConstant.INT;
import static com.pervasive.datarush.types.TokenTypeConstant.STRING;

RecordTokenType employee = record(INT("id"), STRING("firstName"), STRING("lastName"));
TextRecord schema = TextRecord.convert(type);
Schema Defaults
Schemas permit specifying both a default null indicator and a default string conversion behavior. These settings are applied to any field whose text type does not explicitly define values for these properties. This makes working with a large schema easier as these values do not need to be specified for all fields, only those diverging from the norm for the schema.
The standard default settings for a schema are:
The empty string is used as the null indicator.
String values are not trimmed and can be null (NULLABLE_RAW).
If other defaults are desired, they must be specified when creating the schema, either by providing a TextConversionDefaults object or using one of the convenience signatures for this purpose.