Search in sources :

Example 6 with SchemaInfo

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);
}
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

SchemaInfo (com.teradata.jaqy.schema.SchemaInfo)6 JaqyHelper (com.teradata.jaqy.interfaces.JaqyHelper)3 Session (com.teradata.jaqy.Session)2 FullColumnInfo (com.teradata.jaqy.schema.FullColumnInfo)2 Schema (org.apache.avro.Schema)2 GenericRecord (org.apache.avro.generic.GenericRecord)2 JaqyConnection (com.teradata.jaqy.connection.JaqyConnection)1 JaqyResultSet (com.teradata.jaqy.interfaces.JaqyResultSet)1 BigDecimal (java.math.BigDecimal)1 ByteBuffer (java.nio.ByteBuffer)1 DataFileWriter (org.apache.avro.file.DataFileWriter)1 GenericDatumWriter (org.apache.avro.generic.GenericDatumWriter)1 CommandLine (org.apache.commons.cli.CommandLine)1 Option (org.apache.commons.cli.Option)1 CSVRecord (org.apache.commons.csv.CSVRecord)1