Search in sources :

Example 6 with FullColumnInfo

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;
}
Also used : FullColumnInfo(com.teradata.jaqy.schema.FullColumnInfo) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList)

Example 7 with FullColumnInfo

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;
}
Also used : FullColumnInfo(com.teradata.jaqy.schema.FullColumnInfo) Schema(org.apache.avro.Schema) GenericRecord(org.apache.avro.generic.GenericRecord) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 8 with FullColumnInfo

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);
}
Also used : FullColumnInfo(com.teradata.jaqy.schema.FullColumnInfo) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList)

Example 9 with FullColumnInfo

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);
}
Also used : FullColumnInfo(com.teradata.jaqy.schema.FullColumnInfo) Schema(org.apache.avro.Schema) GenericRecord(org.apache.avro.generic.GenericRecord) ByteBuffer(java.nio.ByteBuffer) SchemaInfo(com.teradata.jaqy.schema.SchemaInfo)

Aggregations

FullColumnInfo (com.teradata.jaqy.schema.FullColumnInfo)9 Schema (org.apache.avro.Schema)4 InMemoryResultSetMetaData (com.teradata.jaqy.resultset.InMemoryResultSetMetaData)2 SchemaInfo (com.teradata.jaqy.schema.SchemaInfo)2 ArrayList (java.util.ArrayList)2 GenericRecord (org.apache.avro.generic.GenericRecord)2 JaqyResultSetMetaData (com.teradata.jaqy.connection.JaqyResultSetMetaData)1 JaqyHelper (com.teradata.jaqy.interfaces.JaqyHelper)1 ColumnNode (com.teradata.jaqy.utils.exp.ColumnNode)1 ExpNode (com.teradata.jaqy.utils.exp.ExpNode)1 BigDecimal (java.math.BigDecimal)1 ByteBuffer (java.nio.ByteBuffer)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 CSVRecord (org.apache.commons.csv.CSVRecord)1