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

The following examples show how to use org.apache.avro.Schema#getEnumSymbols() . 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: SchemaValidator.java    From avro-util with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void visitSchema(Schema schema) {
  if (grandfathered.contains(schema)) {
    return;
  }
  if (!validationSpec.validateNames()) {
    return;
  }
  Schema.Type type = schema.getType();
  if (!NAMED_TYPES.contains(type)) {
    return;
  }
  //TODO - avro only validates the SIMPLE name, so for now so do we.
  //see https://issues.apache.org/jira/browse/AVRO-2742
  String simpleName = schema.getName();
  validateName(simpleName, " in " + type.name().toLowerCase(Locale.ROOT) + " " + schema.getFullName());
  if (type == Schema.Type.ENUM) {
    List<String> symbols = schema.getEnumSymbols();
    for (String symbol : symbols) {
      validateName(symbol, " in " + type.name().toLowerCase(Locale.ROOT) + " " + schema.getFullName());
    }
  }
}
 
Example 2
Source File: AvroLoader.java    From incubator-samoa with Apache License 2.0 6 votes vote down vote up
/**
 * Builds the Meta Data of from the Avro Schema
 * 
 * @return
 */
protected InstanceInformation getHeader() {

  String relation = schema.getName();
  attributes = new ArrayList<Attribute>();

  /** By Definition, the returned list is in the order of their positions. **/
  List<Schema.Field> fields = schema.getFields();

  for (Field field : fields) {
    Schema attributeSchema = field.schema();

    /** Currently SAMOA supports only NOMINAL & Numeric Types. **/
    if (attributeSchema.getType() == Schema.Type.ENUM)
    {
      List<String> attributeLabels = attributeSchema.getEnumSymbols();
      attributes.add(new Attribute(field.name(), attributeLabels));
    }
    else if (isNumeric(field))
      attributes.add(new Attribute(field.name()));
  }
  return new InstanceInformation(relation, attributes);
}
 
Example 3
Source File: AvroSchemaValidator.java    From registry with Apache License 2.0 5 votes vote down vote up
private SchemaCompatibilityResult checkReaderEnumContainsAllWriterEnumSymbols(
        final Schema reader, final Schema writer, final Stack<String> location) {
    location.push("symbols");
    final Set<String> symbols = new TreeSet<String>(writer.getEnumSymbols());
    symbols.removeAll(reader.getEnumSymbols());
    if (!symbols.isEmpty()) {
        return SchemaCompatibilityResult.incompatible(
                SchemaIncompatibilityType.MISSING_ENUM_SYMBOLS, reader, writer,
                symbols.toString(), location);
    }
    location.pop();
    return SchemaCompatibilityResult.compatible();
}
 
Example 4
Source File: Generator.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 4 votes vote down vote up
private GenericEnumSymbol generateEnumSymbol(Schema schema) {
  List<String> enums = schema.getEnumSymbols();
  return new
      GenericData.EnumSymbol(schema, enums.get(random.nextInt(enums.size())));
}
 
Example 5
Source File: AvroRandomDataGenerator.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private GenericEnumSymbol generateEnumSymbol(Schema schema) {
  List<String> enums = schema.getEnumSymbols();
  return AvroCompatibilityHelper.newEnumSymbol(schema, enums.get(random.nextInt(enums.size())));
}
 
Example 6
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 7
Source File: Generator.java    From avro-random-generator with Do What The F*ck You Want To Public License 4 votes vote down vote up
private GenericEnumSymbol generateEnumSymbol(Schema schema) {
  List<String> enums = schema.getEnumSymbols();
  return new
      GenericData.EnumSymbol(schema, enums.get(random.nextInt(enums.size())));
}
 
Example 8
Source File: AvroSchemaCheckDefaultStrategy.java    From incubator-gobblin with Apache License 2.0 4 votes vote down vote up
/**
 * This method will compare the name and types of the two schema
 * @param expected The expected schema
 * @param toValidate The real schema
 * @return true when expected schema and toValidate schema have matching field names and types
 */
public boolean compare(Schema expected, Schema toValidate)
{
  if (toValidate.getType() != expected.getType() || !toValidate.getName().equals(expected.getName())) {return false;}
  else {
    switch (toValidate.getType()) {
      case NULL:
      case BOOLEAN:
      case INT:
      case LONG:
      case FLOAT:
      case DOUBLE:
      case BYTES:
      case STRING: {
        return true;
      }
      case ARRAY: {
        return compare(toValidate.getElementType(), expected.getElementType());
      }
      case MAP: {
        return compare(toValidate.getValueType(), expected.getValueType());
      }
      case FIXED: {
        // fixed size and name must match:
        if (toValidate.getFixedSize() != expected.getFixedSize()) {
          return false;
        }
        return true;
      }
      case ENUM: {
        // expected symbols must contain all toValidate symbols:
        final Set<String> expectedSymbols = new HashSet<>(expected.getEnumSymbols());
        final Set<String> toValidateSymbols = new HashSet<String>(toValidate.getEnumSymbols());
        if (expectedSymbols.size() != toValidateSymbols.size()) {
          return false;
        }
        if (!expectedSymbols.containsAll(toValidateSymbols)) {
          return false;
        }
        return true;
      }

      case RECORD: {
        // Check that each field of toValidate schema is in expected schema
        if (toValidate.getFields().size() != expected.getFields().size()) {
          return false;
        }
        for (final Schema.Field expectedFiled : expected.getFields()) {
          final Schema.Field toValidateField = toValidate.getField(expectedFiled.name());
          if (toValidateField == null) {
            // expected field does not correspond to any field in the toValidate record schema
            return false;
          } else {
            if (!compare(toValidateField.schema(), expectedFiled.schema())) {
              return false;
            }
          }
        }
        return true;
      }
      case UNION: {
        // Check existing schema contains all the type in toValidate schema
        if (toValidate.getTypes().size() != expected.getTypes().size()) {
          return false;
        }
        HashSet<Schema> types = new HashSet<Schema>(expected.getTypes());
        for (Schema toValidateType : toValidate.getTypes()) {
          Schema equalSchema = null;
          for (Schema type : types) {
            if (compare(type, toValidateType)) {
              equalSchema = type;
              break;
            }
          }
          if (equalSchema == null) {
            return false;
          }
          types.remove(equalSchema);
        }
        return true;
      }
      default: {
        throw new AvroRuntimeException("Unknown schema type: " + toValidate.getType());
      }
    }
  }
}