Search in sources :

Example 16 with AvroSerdeException

use of org.apache.hadoop.hive.serde2.avro.AvroSerdeException in project hive by apache.

the class AvroSerdeUtils method determineSchemaOrThrowException.

/**
 * Determine the schema to that's been provided for Avro serde work.
 * @param properties containing a key pointing to the schema, one way or another
 * @return schema to use while serdeing the avro file
 * @throws IOException if error while trying to read the schema from another location
 * @throws AvroSerdeException if unable to find a schema or pointer to it in the properties
 */
public static Schema determineSchemaOrThrowException(Configuration conf, Properties properties) throws IOException, AvroSerdeException {
    String schemaString = properties.getProperty(AvroTableProperties.SCHEMA_LITERAL.getPropName());
    if (schemaString != null && !schemaString.equals(SCHEMA_NONE))
        return AvroSerdeUtils.getSchemaFor(schemaString);
    // Try pulling directly from URL
    schemaString = properties.getProperty(AvroTableProperties.SCHEMA_URL.getPropName());
    if (schemaString == null) {
        final String columnNameProperty = properties.getProperty(serdeConstants.LIST_COLUMNS);
        final String columnTypeProperty = properties.getProperty(serdeConstants.LIST_COLUMN_TYPES);
        final String columnCommentProperty = properties.getProperty(AvroSerDe.LIST_COLUMN_COMMENTS);
        if (columnNameProperty == null || columnNameProperty.isEmpty() || columnTypeProperty == null || columnTypeProperty.isEmpty()) {
            throw new AvroSerdeException(EXCEPTION_MESSAGE);
        }
        final String columnNameDelimiter = properties.containsKey(serdeConstants.COLUMN_NAME_DELIMITER) ? properties.getProperty(serdeConstants.COLUMN_NAME_DELIMITER) : String.valueOf(SerDeUtils.COMMA);
        // Get column names and types
        List<String> columnNames = Arrays.asList(columnNameProperty.split(columnNameDelimiter));
        List<TypeInfo> columnTypes = TypeInfoUtils.getTypeInfosFromTypeString(columnTypeProperty);
        Schema schema = AvroSerDe.getSchemaFromCols(properties, columnNames, columnTypes, columnCommentProperty);
        properties.setProperty(AvroTableProperties.SCHEMA_LITERAL.getPropName(), schema.toString());
        if (conf != null)
            conf.set(AvroTableProperties.AVRO_SERDE_SCHEMA.getPropName(), schema.toString(false));
        return schema;
    } else if (schemaString.equals(SCHEMA_NONE)) {
        throw new AvroSerdeException(EXCEPTION_MESSAGE);
    }
    try {
        Schema s = getSchemaFromFS(schemaString, conf);
        if (s == null) {
            // in case schema is not a file system
            return AvroSerdeUtils.getSchemaFor(new URL(schemaString));
        }
        return s;
    } catch (IOException ioe) {
        throw new AvroSerdeException("Unable to read schema from given path: " + schemaString, ioe);
    } catch (URISyntaxException urie) {
        throw new AvroSerdeException("Unable to read schema from given path: " + schemaString, urie);
    }
}
Also used : Schema(org.apache.avro.Schema) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) URL(java.net.URL)

Aggregations

TypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)11 Schema (org.apache.avro.Schema)10 DecimalTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo)7 ListTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo)7 MapTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo)7 StructTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo)7 UnionTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.UnionTypeInfo)7 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)5 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)5 ListObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector)4 MapObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector)4 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)4 UnionObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.UnionObjectInspector)4 DateObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.DateObjectInspector)4 TimestampObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.TimestampObjectInspector)4 IOException (java.io.IOException)3 Properties (java.util.Properties)3 Configuration (org.apache.hadoop.conf.Configuration)3 AvroTableProperties (org.apache.hadoop.hive.serde2.avro.AvroSerdeUtils.AvroTableProperties)3 PrimitiveTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)3