use of com.teradata.jaqy.schema.SchemaInfo in project jaqy by Teradata.
the class AvroUtils method getSchema.
/**
* Get the database schema from AVRO schema
* @param avroSchema
* the AVRO schema
* @return database schema
*/
public static SchemaInfo getSchema(Schema avroSchema, Iterator<GenericRecord> iter) {
List<Schema.Field> fields = avroSchema.getFields();
int numCols = fields.size();
FullColumnInfo[] columnInfos = new FullColumnInfo[numCols];
int i = 0;
boolean doScan = false;
for (Schema.Field field : fields) {
FullColumnInfo typeInfo = new FullColumnInfo();
columnInfos[i++] = typeInfo;
typeInfo.name = field.name();
typeInfo.label = typeInfo.name;
doScan |= updateType(field.schema(), typeInfo);
}
int numFields = i;
if (doScan) {
// We have variable length data to check the maximum length.
while (iter.hasNext()) {
GenericRecord record = iter.next();
for (i = 0; i < numFields; ++i) {
FullColumnInfo typeInfo = columnInfos[i];
switch(typeInfo.type) {
case Types.NVARCHAR:
{
CharSequence o = (CharSequence) record.get(i);
if (o == null)
continue;
int len = o.length();
if (typeInfo.precision < len)
typeInfo.precision = len;
break;
}
case Types.VARBINARY:
{
ByteBuffer bb = (ByteBuffer) record.get(i);
if (bb == null)
continue;
int len = bb.remaining();
if (typeInfo.precision < len)
typeInfo.precision = len;
break;
}
default:
{
break;
}
}
}
}
}
return new SchemaInfo(columnInfos);
}
Aggregations