Search in sources :

Example 1 with SchemaParseException

use of org.apache.avro.SchemaParseException in project nifi by apache.

the class RestSchemaRegistryClient method createRecordSchema.

private RecordSchema createRecordSchema(final JsonNode schemaNode) throws SchemaNotFoundException {
    final String subject = schemaNode.get(SUBJECT_FIELD_NAME).asText();
    final int version = schemaNode.get(VERSION_FIELD_NAME).asInt();
    final int id = schemaNode.get(ID_FIELD_NAME).asInt();
    final String schemaText = schemaNode.get(SCHEMA_TEXT_FIELD_NAME).asText();
    try {
        final Schema avroSchema = new Schema.Parser().parse(schemaText);
        final SchemaIdentifier schemaId = SchemaIdentifier.builder().name(subject).id(Long.valueOf(id)).version(version).build();
        final RecordSchema recordSchema = AvroTypeUtil.createSchema(avroSchema, schemaText, schemaId);
        return recordSchema;
    } catch (final SchemaParseException spe) {
        throw new SchemaNotFoundException("Obtained Schema with id " + id + " and name " + subject + " from Confluent Schema Registry but the Schema Text that was returned is not a valid Avro Schema");
    }
}
Also used : SchemaParseException(org.apache.avro.SchemaParseException) Schema(org.apache.avro.Schema) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SchemaNotFoundException(org.apache.nifi.schema.access.SchemaNotFoundException) SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier) RecordSchema(org.apache.nifi.serialization.record.RecordSchema)

Example 2 with SchemaParseException

use of org.apache.avro.SchemaParseException in project flink by apache.

the class AvroSchemaConverter method convertToDataType.

/**
 * Converts an Avro schema string into a nested row structure with deterministic field order and
 * data types that are compatible with Flink's Table & SQL API.
 *
 * @param avroSchemaString Avro schema definition string
 * @return data type matching the schema
 */
public static DataType convertToDataType(String avroSchemaString) {
    Preconditions.checkNotNull(avroSchemaString, "Avro schema must not be null.");
    final Schema schema;
    try {
        schema = new Schema.Parser().parse(avroSchemaString);
    } catch (SchemaParseException e) {
        throw new IllegalArgumentException("Could not parse Avro schema string.", e);
    }
    return convertToDataType(schema);
}
Also used : SchemaParseException(org.apache.avro.SchemaParseException) Schema(org.apache.avro.Schema) AvroRowDeserializationSchema(org.apache.flink.formats.avro.AvroRowDeserializationSchema) AvroRowSerializationSchema(org.apache.flink.formats.avro.AvroRowSerializationSchema)

Example 3 with SchemaParseException

use of org.apache.avro.SchemaParseException in project flink by apache.

the class AvroSchemaConverter method convertToTypeInfo.

/**
 * Converts an Avro schema string into a nested row structure with deterministic field order and
 * data types that are compatible with Flink's Table & SQL API.
 *
 * @param avroSchemaString Avro schema definition string
 * @return type information matching the schema
 */
@SuppressWarnings("unchecked")
public static <T> TypeInformation<T> convertToTypeInfo(String avroSchemaString) {
    Preconditions.checkNotNull(avroSchemaString, "Avro schema must not be null.");
    final Schema schema;
    try {
        schema = new Schema.Parser().parse(avroSchemaString);
    } catch (SchemaParseException e) {
        throw new IllegalArgumentException("Could not parse Avro schema string.", e);
    }
    return (TypeInformation<T>) convertToTypeInfo(schema);
}
Also used : SchemaParseException(org.apache.avro.SchemaParseException) Schema(org.apache.avro.Schema) AvroRowDeserializationSchema(org.apache.flink.formats.avro.AvroRowDeserializationSchema) AvroRowSerializationSchema(org.apache.flink.formats.avro.AvroRowSerializationSchema) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation)

Example 4 with SchemaParseException

use of org.apache.avro.SchemaParseException in project gora by apache.

the class AbstractGoraMojo method compile.

protected void compile() throws IOException {
    File sourceDirectory = getSourcesDirectory();
    File outputDirectory = getOutputDirectory();
    if (!outputDirectory.exists()) {
        outputDirectory.mkdirs();
    }
    Scanner fileScanner = context.newScanner(sourceDirectory, true);
    fileScanner.setIncludes(includes);
    fileScanner.setExcludes(excludes);
    fileScanner.scan();
    File basedir = fileScanner.getBasedir();
    List<File> changedFiles = new ArrayList<File>();
    for (String fileName : fileScanner.getIncludedFiles()) {
        File file = new File(basedir, fileName);
        changedFiles.add(file);
        context.removeMessages(file);
    }
    if (!changedFiles.isEmpty()) {
        try {
            File[] schemaFile = changedFiles.toArray(new File[changedFiles.size()]);
            GoraCompiler.compileSchema(schemaFile, outputDirectory);
        } catch (SchemaParseException e) {
            if (e.getCause() != null && e.getCause() instanceof JsonParseException) {
                attachErrorMessage((JsonParseException) e.getCause());
            } else {
                throw e;
            }
        }
    }
    context.refresh(outputDirectory);
}
Also used : Scanner(org.codehaus.plexus.util.Scanner) SchemaParseException(org.apache.avro.SchemaParseException) ArrayList(java.util.ArrayList) JsonParseException(org.codehaus.jackson.JsonParseException) File(java.io.File)

Example 5 with SchemaParseException

use of org.apache.avro.SchemaParseException in project cdap by caskdata.

the class AvroRecordFormat method validateSchema.

@Override
protected void validateSchema(Schema desiredSchema) throws UnsupportedTypeException {
    try {
        // rather than check for all inconsistencies, just try to read the schema string as an Avro schema.
        avroFormatSchema = new org.apache.avro.Schema.Parser().parse(desiredSchema.toString());
        formatSchema = desiredSchema;
    } catch (SchemaParseException e) {
        throw new UnsupportedTypeException("Schema is not a valid avro schema.", e);
    } catch (Exception e) {
        throw new UnsupportedTypeException("Exception parsing schema as an avro schema.", e);
    }
}
Also used : SchemaParseException(org.apache.avro.SchemaParseException) UnsupportedTypeException(co.cask.cdap.api.data.schema.UnsupportedTypeException) UnsupportedTypeException(co.cask.cdap.api.data.schema.UnsupportedTypeException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnexpectedFormatException(co.cask.cdap.api.data.format.UnexpectedFormatException) SchemaParseException(org.apache.avro.SchemaParseException)

Aggregations

SchemaParseException (org.apache.avro.SchemaParseException)7 Schema (org.apache.avro.Schema)4 AvroRowDeserializationSchema (org.apache.flink.formats.avro.AvroRowDeserializationSchema)2 AvroRowSerializationSchema (org.apache.flink.formats.avro.AvroRowSerializationSchema)2 UnexpectedFormatException (co.cask.cdap.api.data.format.UnexpectedFormatException)1 UnsupportedTypeException (co.cask.cdap.api.data.schema.UnsupportedTypeException)1 AWSSchemaRegistryException (com.amazonaws.services.schemaregistry.exception.AWSSchemaRegistryException)1 File (java.io.File)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 Parser (org.apache.avro.Schema.Parser)1 TypeInformation (org.apache.flink.api.common.typeinfo.TypeInformation)1 MutableByteArrayInputStream (org.apache.flink.formats.avro.utils.MutableByteArrayInputStream)1 Configuration (org.apache.hadoop.conf.Configuration)1 HiveConf (org.apache.hadoop.hive.conf.HiveConf)1 Deserializer (org.apache.hadoop.hive.serde2.Deserializer)1 SerDeException (org.apache.hadoop.hive.serde2.SerDeException)1