Java Code Examples for org.apache.avro.Schema#getFullName()

The following examples show how to use org.apache.avro.Schema#getFullName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AvroSchemaWithTypeVisitor.java    From iceberg with Apache License 2.0 6 votes vote down vote up
private static <T> T visitRecord(Types.StructType struct, Schema record, AvroSchemaWithTypeVisitor<T> visitor) {
  // check to make sure this hasn't been visited before
  String name = record.getFullName();
  Preconditions.checkState(!visitor.recordLevels.contains(name),
      "Cannot process recursive Avro record %s", name);

  visitor.recordLevels.push(name);

  List<Schema.Field> fields = record.getFields();
  List<String> names = Lists.newArrayListWithExpectedSize(fields.size());
  List<T> results = Lists.newArrayListWithExpectedSize(fields.size());
  for (Schema.Field field : fields) {
    int fieldId = AvroSchemaUtil.getFieldId(field);
    Types.NestedField iField = struct != null ? struct.field(fieldId) : null;
    names.add(field.name());
    results.add(visit(iField != null ? iField.type() : null, field.schema(), visitor));
  }

  visitor.recordLevels.pop();

  return visitor.record(struct, record, names, results);
}
 
Example 2
Source File: SchemaValidator.java    From avro-util with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void visitField(Schema parent, Schema.Field field) {
  if (grandfathered.contains(parent)) {
    return;
  }
  if (validationSpec.validateNames()) {
    String fieldName = field.name();
    validateName(fieldName, " in field " + parent.getFullName() + "." + fieldName);
  }
  JsonNode defaultValue = field.defaultValue();
  if (validationSpec.validateDefaultValues() && defaultValue != null) {
    Schema fieldSchema = field.schema();
    boolean validDefault = isValidDefault(fieldSchema, defaultValue);
    if (!validDefault) {
      //throw ~the same exception avro would
      String message = "Invalid default for field " + parent.getFullName() + "." + field.name() + ": "
          + defaultValue + " not a " + fieldSchema;
      throw new AvroTypeException(message);
    }
  }
}
 
Example 3
Source File: AvroEntityComposer.java    From kite with Apache License 2.0 6 votes vote down vote up
/**
 * Build the appropriate AvroRecordBuilderFactory for this instance. Avro has
 * many different record types, of which we support two: Specific and Generic.
 * 
 * @param schema
 *          The Avro schema needed to construct the AvroRecordBuilderFactory.
 * @return The constructed AvroRecordBuilderFactory.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private AvroRecordBuilderFactory<E> buildAvroRecordBuilderFactory(
    Schema schema) {
  if (specific) {
    Class<E> specificClass;
    String className = schema.getFullName();
    try {
      specificClass = (Class<E>) Class.forName(className);
    } catch (ClassNotFoundException e) {
      throw new DatasetException("Could not get Class instance for "
          + className);
    }
    return new SpecificAvroRecordBuilderFactory(specificClass);
  } else {
    return (AvroRecordBuilderFactory<E>) new GenericAvroRecordBuilderFactory(
        schema);
  }
}
 
Example 4
Source File: FastDeserializerGenerator.java    From avro-util with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private JMethod createMethod(final Schema schema, boolean read) {
  if (!Schema.Type.RECORD.equals(schema.getType())) {
    throw new FastDeserializerGeneratorException("Methods are defined only for records, not for " + schema.getType());
  }
  if (methodAlreadyDefined(schema, read)) {
    throw new FastDeserializerGeneratorException("Method already exists for: " + schema.getFullName());
  }

  JClass schemaClass = schemaAssistant.classFromSchema(schema);
  JMethod method = generatedClass.method(JMod.PUBLIC, read ? schemaClass : codeModel.VOID,
      getUniqueName("deserialize" + schema.getName()));

  method._throws(IOException.class);
  method.param(Object.class, VAR_NAME_FOR_REUSE);
  method.param(Decoder.class, DECODER);

  (read ? deserializeMethodMap : skipMethodMap).put(schema.getFullName(), method);

  return method;
}
 
Example 5
Source File: FastSerializerGenerator.java    From avro-fastserde with Apache License 2.0 6 votes vote down vote up
private JMethod createMethod(final Schema schema) {
    if (Schema.Type.RECORD.equals(schema.getType())) {
        if (!methodAlreadyDefined(schema)) {
            JMethod method = serializerClass.method(JMod.PUBLIC, codeModel.VOID,
                    "serialize" + schema.getName() + nextRandomInt());
            method._throws(IOException.class);
            method.param(schemaAssistant.classFromSchema(schema), "data");
            method.param(Encoder.class, ENCODER);

            method.annotate(SuppressWarnings.class).param("value", "unchecked");
            serializeMethodMap.put(schema.getFullName(), method);

            return method;
        } else {
            throw new FastSerializerGeneratorException("Method already exists for: " + schema.getFullName());
        }
    }
    throw new FastSerializerGeneratorException("No method for schema type: " + schema.getType());
}
 
Example 6
Source File: AvroCustomOrderSchemaVisitor.java    From iceberg with Apache License 2.0 5 votes vote down vote up
public static <T, F> T visit(Schema schema, AvroCustomOrderSchemaVisitor<T, F> visitor) {
  switch (schema.getType()) {
    case RECORD:
      // check to make sure this hasn't been visited before
      String name = schema.getFullName();
      Preconditions.checkState(!visitor.recordLevels.contains(name),
          "Cannot process recursive Avro record %s", name);

      visitor.recordLevels.push(name);

      List<Schema.Field> fields = schema.getFields();
      List<String> names = Lists.newArrayListWithExpectedSize(fields.size());
      List<Supplier<F>> results = Lists.newArrayListWithExpectedSize(fields.size());
      for (Schema.Field field : schema.getFields()) {
        names.add(field.name());
        results.add(new VisitFieldFuture<>(field, visitor));
      }

      visitor.recordLevels.pop();

      return visitor.record(schema, names, Iterables.transform(results, Supplier::get));

    case UNION:
      List<Schema> types = schema.getTypes();
      List<Supplier<T>> options = Lists.newArrayListWithExpectedSize(types.size());
      for (Schema type : types) {
        options.add(new VisitFuture<>(type, visitor));
      }
      return visitor.union(schema, Iterables.transform(options, Supplier::get));

    case ARRAY:
      return visitor.array(schema, new VisitFuture<>(schema.getElementType(), visitor));

    case MAP:
      return visitor.map(schema, new VisitFuture<>(schema.getValueType(), visitor));

    default:
      return visitor.primitive(schema);
  }
}
 
Example 7
Source File: AvroFieldsGenerator.java    From registry with Apache License 2.0 5 votes vote down vote up
private void parse(Schema schema, List<SchemaFieldInfo> schemaFieldInfos) {
    if (schema.getType() != Schema.Type.RECORD) {
        LOG.info("Given schema type [{}] is not record", schema.getType());
    } else {
        String fullName = schema.getFullName();
        LOG.debug("Schema full name: [{}]", fullName);

        List<Schema.Field> fields = schema.getFields();
        Set<String> visitedRecords = new HashSet<>();
        visitedRecords.add(schema.getFullName());
        for (Schema.Field field : fields) {
            parseField(field, schemaFieldInfos, visitedRecords);
        }
    }
}
 
Example 8
Source File: AvroSchemaConverterLogicalTypesPre19.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public MessageType convert(Schema avroSchema) {
  LOG.info("Using customized AvroSchemaConverter utility to convert: " + avroSchema.toString());
  if (!avroSchema.getType().equals(Schema.Type.RECORD)) {
    throw new IllegalArgumentException("Avro schema must be a record.");
  }

  return new MessageType(avroSchema.getFullName(), convertFields(avroSchema.getFields()));
}
 
Example 9
Source File: AvroCodecFormat.java    From funcj with MIT License 5 votes vote down vote up
protected static Schema checkSchemaType(Schema schema, Schema.Type type) {
    final Schema match = findType(schema, type);
    if (match != null) {
        return match;
    } else {
        throw new CodecException(
                "Expecting a schema of type " + type + " but got " + schema.getType() +
                        ", from schema '" + schema.getFullName() + "'"
        );
    }
}
 
Example 10
Source File: FastSerializerGenerator.java    From avro-fastserde with Apache License 2.0 5 votes vote down vote up
private JMethod getMethod(final Schema schema) {
    if (Schema.Type.RECORD.equals(schema.getType())) {
        if (methodAlreadyDefined(schema)) {
            return serializeMethodMap.get(schema.getFullName());
        }
        throw new FastSerializerGeneratorException("No method for schema: " + schema.getFullName());
    }
    throw new FastSerializerGeneratorException("No method for schema type: " + schema.getType());
}
 
Example 11
Source File: AvroSchemaConverter190Int96Avro17.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public MessageType convert(Schema avroSchema) {
  if (!avroSchema.getType().equals(Schema.Type.RECORD)) {
    throw new IllegalArgumentException("Avro schema must be a record.");
  }
  LOG.debug("Converting avro schema to parquet");
  return new MessageType(avroSchema.getFullName(), convertFields(avroSchema.getFields()));
}
 
Example 12
Source File: AvroSchemaVisitor.java    From iceberg with Apache License 2.0 5 votes vote down vote up
public static <T> T visit(Schema schema, AvroSchemaVisitor<T> visitor) {
  switch (schema.getType()) {
    case RECORD:
      // check to make sure this hasn't been visited before
      String name = schema.getFullName();
      Preconditions.checkState(!visitor.recordLevels.contains(name),
          "Cannot process recursive Avro record %s", name);

      visitor.recordLevels.push(name);

      List<Schema.Field> fields = schema.getFields();
      List<String> names = Lists.newArrayListWithExpectedSize(fields.size());
      List<T> results = Lists.newArrayListWithExpectedSize(fields.size());
      for (Schema.Field field : schema.getFields()) {
        names.add(field.name());
        results.add(visit(field.schema(), visitor));
      }

      visitor.recordLevels.pop();

      return visitor.record(schema, names, results);

    case UNION:
      List<Schema> types = schema.getTypes();
      List<T> options = Lists.newArrayListWithExpectedSize(types.size());
      for (Schema type : types) {
        options.add(visit(type, visitor));
      }
      return visitor.union(schema, options);

    case ARRAY:
      return visitor.array(schema, visit(schema.getElementType(), visitor));

    case MAP:
      return visitor.map(schema, visit(schema.getValueType(), visitor));

    default:
      return visitor.primitive(schema);
  }
}
 
Example 13
Source File: RecordIdStrategy.java    From apicurio-registry with Apache License 2.0 5 votes vote down vote up
@Override
public String artifactId(String topic, boolean isKey, Schema schema) {
    if (schema != null && schema.getType() == Schema.Type.RECORD) {
        return schema.getFullName();
    }
    throw new SerializationException("The message must only be an Avro record schema!");
}
 
Example 14
Source File: SchemaAssistant.java    From avro-util with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static String getSchemaFullName(Schema schema) {
  Schema.Type type = schema.getType();
  boolean isNamedType = type.equals(Schema.Type.ENUM) || type.equals(Schema.Type.FIXED) || type.equals(Schema.Type.RECORD);
  /**
   * Avro-1.4 doesn't support {@link Schema#getFullName()} if the Schema is not a NamedSchema.
   */
  return isNamedType ? schema.getFullName() : type.name();
}
 
Example 15
Source File: AvroSchemaConverter190Int96Avro18.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public MessageType convert(Schema avroSchema) {
  if (!avroSchema.getType().equals(Schema.Type.RECORD)) {
    throw new IllegalArgumentException("Avro schema must be a record.");
  }
  LOG.debug("Converting avro schema to parquet");
  return new MessageType(avroSchema.getFullName(), convertFields(avroSchema.getFields()));
}
 
Example 16
Source File: FastSerializerGenerator.java    From avro-util with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private JMethod getMethod(final Schema schema) {
  if (Schema.Type.RECORD.equals(schema.getType())) {
    if (methodAlreadyDefined(schema)) {
      return serializeMethodMap.get(schema.getFullName());
    }
    throw new FastSerdeGeneratorException("No method for schema: " + schema.getFullName());
  }
  throw new FastSerdeGeneratorException("No method for schema type: " + schema.getType());
}
 
Example 17
Source File: AvroToJsonBytesWithMetadataConverter.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
@Override
public String convertSchema(Schema inputSchema, WorkUnitState workUnit)
    throws SchemaConversionException {
  String schema = super.convertSchema(inputSchema, workUnit);
  contentType = inputSchema.getFullName() + "+json";

  return schema;
}
 
Example 18
Source File: AvroSchemaConverter.java    From parquet-mr with Apache License 2.0 4 votes vote down vote up
public MessageType convert(Schema avroSchema) {
  if (!avroSchema.getType().equals(Schema.Type.RECORD)) {
    throw new IllegalArgumentException("Avro schema must be a record.");
  }
  return new MessageType(avroSchema.getFullName(), convertFields(avroSchema.getFields()));
}
 
Example 19
Source File: AvroSchemaProvider.java    From registry with Apache License 2.0 4 votes vote down vote up
private static Appendable build(Map<String, String> env,
                                Schema schema,
                                Appendable appendable) throws IOException {
    boolean firstTime = true;
    Schema.Type schemaType = schema.getType();
    String fullName = schema.getFullName();
    switch (schemaType) {
        default: // boolean, bytes, double, float, int, long, null, string
            return appendable.append('"').append(schemaType.getName()).append('"');

        case UNION:
            appendable.append('[');
            for (Schema b : schema.getTypes()) {
                if (!firstTime) appendable.append(',');
                else firstTime = false;
                build(env, b, appendable);
            }
            return appendable.append(']');

        case ARRAY:
        case MAP:
            appendable.append("{\"type\":\"").append(schemaType.getName()).append("\"");
            if (schemaType == Schema.Type.ARRAY)
                build(env, schema.getElementType(), appendable.append(",\"items\":"));
            else build(env, schema.getValueType(), appendable.append(",\"values\":"));
            return appendable.append("}");

        case ENUM:
            if (env.get(fullName) != null) {
                return appendable.append(env.get(fullName));
            }
            addNameType(env, appendable, schemaType, fullName);

            appendable.append(",\"symbols\":[");
            for (String enumSymbol : schema.getEnumSymbols()) {
                if (!firstTime) appendable.append(',');
                else firstTime = false;
                appendable.append('"').append(enumSymbol).append('"');
            }
            return appendable.append("]").append("}");

        case FIXED:
            if (env.get(fullName) != null) {
                return appendable.append(env.get(fullName));
            }
            addNameType(env, appendable, schemaType, fullName);

            return appendable.append(",\"size\":").append(Integer.toString(schema.getFixedSize())).append("}");

        case RECORD:
            if (env.get(fullName) != null) {
                return appendable.append(env.get(fullName));
            }
            addNameType(env, appendable, schemaType, fullName);

            // avro resolution parsing does not handle aliases and default attributes
            // handle aliases
            Set<String> aliases = schema.getAliases();
            if (aliases != null && !aliases.isEmpty()) {
                appendable.append("\"aliases\":")
                        .append("[")
                        .append(Joiner.on(",").join(aliases.stream()
                                                            .map(x -> "\"" + x + "\"")
                                                            .collect(Collectors.toList())))
                        .append("]")
                        .append(",");
            }

            appendable.append(",\"fields\":[");
            for (Schema.Field field : schema.getFields()) {
                if (!firstTime) {
                    appendable.append(',');
                } else {
                    firstTime = false;
                }
                appendable.append("{\"name\":\"").append(field.name()).append("\"").append(",\"type\":");

                // handle default value
                Object defaultValue = field.defaultVal();
                if (defaultValue != null) {
                    appendable.append(defaultValue.toString());
                }

                build(env, field.schema(), appendable).append("}");
            }
            return appendable.append("]").append("}");
    }
}
 
Example 20
Source File: AvroSchemaVisitor.java    From iceberg with Apache License 2.0 4 votes vote down vote up
public static <T> T visit(Schema schema, AvroSchemaVisitor<T> visitor) {
  switch (schema.getType()) {
    case RECORD:
      // check to make sure this hasn't been visited before
      String name = schema.getFullName();
      Preconditions.checkState(!visitor.recordLevels.contains(name),
          "Cannot process recursive Avro record %s", name);

      visitor.recordLevels.push(name);

      List<Schema.Field> fields = schema.getFields();
      List<String> names = Lists.newArrayListWithExpectedSize(fields.size());
      List<T> results = Lists.newArrayListWithExpectedSize(fields.size());
      for (Schema.Field field : schema.getFields()) {
        names.add(field.name());
        T result = visitWithName(field.name(), field.schema(), visitor);
        results.add(result);
      }

      visitor.recordLevels.pop();

      return visitor.record(schema, names, results);

    case UNION:
      List<Schema> types = schema.getTypes();
      List<T> options = Lists.newArrayListWithExpectedSize(types.size());
      for (Schema type : types) {
        options.add(visit(type, visitor));
      }
      return visitor.union(schema, options);

    case ARRAY:
      if (schema.getLogicalType() instanceof LogicalMap) {
        return visitor.array(schema, visit(schema.getElementType(), visitor));
      } else {
        return visitor.array(schema, visitWithName("element", schema.getElementType(), visitor));
      }

    case MAP:
      return visitor.map(schema, visitWithName("value", schema.getValueType(), visitor));

    default:
      return visitor.primitive(schema);
  }
}