use of com.teradata.jaqy.schema.FullColumnInfo in project jaqy by Teradata.
the class AvroUtils method getSchema.
public static Schema getSchema(SchemaInfo schemaInfo, JaqyHelper helper) throws SQLException {
FullColumnInfo[] columnInfos = schemaInfo.columns;
int columns = columnInfos.length;
ArrayList<Schema.Field> fields = new ArrayList<Schema.Field>(columns);
for (int i = 0; i < columns; ++i) {
String header = columnInfos[i].label;
int type = columnInfos[i].type;
Schema.Type avroType = getAvroType(type);
Schema fieldSchema;
if (avroType == Schema.Type.ARRAY) {
fieldSchema = getArraySchema(columnInfos[i]);
} else {
fieldSchema = Schema.create(getAvroType(type));
}
if (columnInfos[i].nullable != ResultSetMetaData.columnNoNulls) {
// In order to include NULLs in the record, we have to include
// NULL as part of field schema for the column.
ArrayList<Schema> list = new ArrayList<Schema>();
list.add(Schema.create(Schema.Type.NULL));
list.add(fieldSchema);
fieldSchema = Schema.createUnion(list);
}
Schema.Field field = new Schema.Field(header, fieldSchema, null, (Object) null);
fields.add(field);
}
// create a dummy record schema name.
String schemaName = "rsmd" + columnInfos.hashCode();
Schema schema = Schema.createRecord(schemaName, null, null, false);
schema.setFields(fields);
return schema;
}
use of com.teradata.jaqy.schema.FullColumnInfo in project jaqy by Teradata.
the class AvroUtils method print.
public static long print(DataFileWriter<GenericRecord> writer, Schema schema, JaqyResultSet rs, SchemaInfo schemaInfo) throws Exception {
FullColumnInfo[] columnInfos = schemaInfo.columns;
int columns = columnInfos.length;
Schema.Type[] avroTypes = new Schema.Type[columns];
Schema[] avroSchemas = new Schema[columns];
for (int i = 0; i < columns; ++i) {
avroTypes[i] = getAvroType(columnInfos[i].type);
if (avroTypes[i] == Schema.Type.ARRAY) {
avroSchemas[i] = getArraySchema(columnInfos[i]);
}
}
long count = 0;
while (rs.next()) {
++count;
GenericRecord r = new GenericData.Record(schema);
for (int i = 0; i < columns; ++i) {
Object obj = rs.getObject(i + 1);
if (obj == null) {
r.put(i, null);
} else {
r.put(i, getAvroObject(obj, avroTypes[i], columnInfos[i], avroSchemas[i]));
}
}
writer.append(r);
}
return count;
}
use of com.teradata.jaqy.schema.FullColumnInfo in project jaqy by Teradata.
the class AvroUtils method getArraySchema.
private static Schema getArraySchema(FullColumnInfo columnInfo) {
FullColumnInfo[] childrenInfos = columnInfo.children;
if (childrenInfos == null || childrenInfos.length != 1) {
// We cannot handle this case.
throw new RuntimeException("Cannot handle column " + columnInfo.label + " type.");
}
Schema.Type childAvroType = getAvroType(childrenInfos[0].type);
if (childAvroType == Schema.Type.ARRAY) {
// We cannot handle this case.
throw new RuntimeException("Cannot handle column " + columnInfo.label + " type.");
}
Schema childSchema = Schema.create(childAvroType);
if (childrenInfos[0].nullable != ResultSetMetaData.columnNoNulls) {
// In order to include NULLs in the record, we have to include
// NULL as part of field schema for the column.
ArrayList<Schema> list = new ArrayList<Schema>();
list.add(Schema.create(Schema.Type.NULL));
list.add(childSchema);
childSchema = Schema.createUnion(list);
}
return Schema.createArray(childSchema);
}
use of com.teradata.jaqy.schema.FullColumnInfo 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