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

The following examples show how to use org.apache.avro.Schema#hasEnumSymbol() . These examples are extracted from open source projects. 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 Project: Cubert   File: PigAvroDatumWriter.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Recursively check whether "datum" is an instance of "schema" and called 
 * by {@link #resolveUnionSchema(Schema,Object)},
 * {@link #unwrappedInstanceOf(Schema,Object)}. 
 * 
 */
protected boolean instanceOf(Schema schema, Object datum)
                                throws IOException {

    try {
        switch (schema.getType()) {
        case RECORD:
            if (datum instanceof Tuple) {
                Tuple tuple = (Tuple) datum;
                List<Field> fields = schema.getFields();
                if (fields.size() != tuple.size()) {
                    return false;
                }
                for (int i = 0; i < fields.size(); i++) {
                    if (!instanceOf(fields.get(i).schema(), tuple.get(i)))
                        return false;
                }
                return true;
            }
            return false;

        case UNION:
            @SuppressWarnings("unused")
            int index = resolveUnionSchema(schema, datum);
            return true;
        case ENUM:
            return datum instanceof String && schema.hasEnumSymbol(((String) datum))
                        || unwrappedInstanceOf(schema, datum);
        case ARRAY:
            return datum instanceof DataBag
                        ||  unwrappedInstanceOf(schema, datum);
        case MAP:
            return datum instanceof Map
                        || unwrappedInstanceOf(schema, datum);
        case FIXED:
            return datum instanceof DataByteArray && ((DataByteArray) datum).size() == schema.getFixedSize()
                        || unwrappedInstanceOf(schema, datum);
        case STRING:
            return datum instanceof String
                        || unwrappedInstanceOf(schema, datum);
        case BYTES:
            return datum instanceof DataByteArray
                        || unwrappedInstanceOf(schema, datum);
        case INT:
            return datum instanceof Integer
                        || unwrappedInstanceOf(schema, datum);
        case LONG:
            return datum instanceof Long
                        || datum instanceof Integer
                        || unwrappedInstanceOf(schema, datum);
        case FLOAT:
            return datum instanceof Float
                        || datum instanceof Integer
                        || datum instanceof Long
                        || unwrappedInstanceOf(schema, datum);
        case DOUBLE:
            return datum instanceof Double
                        || datum instanceof Float
                        || datum instanceof Integer
                        || datum instanceof Long
                        || unwrappedInstanceOf(schema, datum);
        case BOOLEAN:
            return datum instanceof Boolean
                        || datum instanceof Integer
                        || unwrappedInstanceOf(schema, datum);
        case NULL:
            return datum == null;
        default:
            throw new RuntimeException("Unexpected type: " + schema);
        }
    } catch (ExecException e) {
        e.printStackTrace(System.err);
        throw new RuntimeException(e);
    }
}
 
Example 2
Source Project: parquet-mr   File: AvroJson.java    License: Apache License 2.0 4 votes vote down vote up
private static boolean matches(JsonNode datum, Schema schema) {
  switch (schema.getType()) {
    case RECORD:
      if (datum.isObject()) {
        // check that each field is present or has a default
        boolean missingField = false;
        for (Schema.Field field : schema.getFields()) {
          if (!datum.has(field.name()) && field.defaultVal() == null) {
            missingField = true;
            break;
          }
        }
        if (!missingField) {
          return true;
        }
      }
      break;
    case UNION:
      if (resolveUnion(datum, schema.getTypes()) != null) {
        return true;
      }
      break;
    case MAP:
      if (datum.isObject()) {
        return true;
      }
      break;
    case ARRAY:
      if (datum.isArray()) {
        return true;
      }
      break;
    case BOOLEAN:
      if (datum.isBoolean()) {
        return true;
      }
      break;
    case FLOAT:
      if (datum.isFloat() || datum.isInt()) {
        return true;
      }
      break;
    case DOUBLE:
      if (datum.isDouble() || datum.isFloat() ||
          datum.isLong() || datum.isInt()) {
        return true;
      }
      break;
    case INT:
      if (datum.isInt()) {
        return true;
      }
      break;
    case LONG:
      if (datum.isLong() || datum.isInt()) {
        return true;
      }
      break;
    case STRING:
      if (datum.isTextual()) {
        return true;
      }
      break;
    case ENUM:
      if (datum.isTextual() && schema.hasEnumSymbol(datum.textValue())) {
        return true;
      }
      break;
    case BYTES:
    case FIXED:
      if (datum.isBinary()) {
        return true;
      }
      break;
    case NULL:
      if (datum == null || datum.isNull()) {
        return true;
      }
      break;
    default: // UNION or unknown
      throw new IllegalArgumentException("Unsupported schema: " + schema);
  }
  return false;
}
 
Example 3
Source Project: parquet-mr   File: RecordBuilder.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Returns a the value as the first matching schema type or null.
 *
 * Note that if the value may be null even if the schema does not allow the
 * value to be null.
 *
 * @param string a String representation of the value
 * @param schema a Schema
 * @return the string coerced to the correct type from the schema or null
 */
private static Object makeValue(String string, Schema schema) {
  if (string == null) {
    return null;
  }

  try {
    switch (schema.getType()) {
      case BOOLEAN:
        return Boolean.valueOf(string);
      case STRING:
        return string;
      case FLOAT:
        return Float.valueOf(string);
      case DOUBLE:
        return Double.valueOf(string);
      case INT:
        return Integer.valueOf(string);
      case LONG:
        return Long.valueOf(string);
      case ENUM:
        // TODO: translate to enum class
        if (schema.hasEnumSymbol(string)) {
          return string;
        } else {
          try {
            return schema.getEnumSymbols().get(Integer.parseInt(string));
          } catch (IndexOutOfBoundsException ex) {
            return null;
          }
        }
      case UNION:
        Object value = null;
        for (Schema possible : schema.getTypes()) {
          value = makeValue(string, possible);
          if (value != null) {
            return value;
          }
        }
        return null;
      case NULL:
        return null;
      default:
        // FIXED, BYTES, MAP, ARRAY, RECORD are not supported
        throw new RecordException(
            "Unsupported field type:" + schema.getType());
    }
  } catch (NumberFormatException e) {
    // empty string is considered null for numeric types
    if (string.isEmpty()) {
      return null;
    } else {
      throw e;
    }
  }
}
 
Example 4
Source Project: spork   File: PigAvroDatumWriter.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Recursively check whether "datum" is an instance of "schema" and called 
 * by {@link #resolveUnionSchema(Schema,Object)},
 * {@link #unwrappedInstanceOf(Schema,Object)}. 
 * 
 */
protected boolean instanceOf(Schema schema, Object datum)
                                throws IOException {

    try {
        switch (schema.getType()) {
        case RECORD:
            if (datum instanceof Tuple) {
                Tuple tuple = (Tuple) datum;
                List<Field> fields = schema.getFields();
                if (fields.size() != tuple.size()) {
                    return false;
                }
                for (int i = 0; i < fields.size(); i++) {
                    if (!instanceOf(fields.get(i).schema(), tuple.get(i)))
                        return false;
                }
                return true;
            }
            return false;

        case UNION:
            @SuppressWarnings("unused")
            int index = resolveUnionSchema(schema, datum);
            return true;
        case ENUM:
            return datum instanceof String && schema.hasEnumSymbol(((String) datum))
                        || unwrappedInstanceOf(schema, datum);
        case ARRAY:
            return datum instanceof DataBag
                        ||  unwrappedInstanceOf(schema, datum);
        case MAP:
            return datum instanceof Map
                        || unwrappedInstanceOf(schema, datum);
        case FIXED:
            return datum instanceof DataByteArray && ((DataByteArray) datum).size() == schema.getFixedSize()
                        || unwrappedInstanceOf(schema, datum);
        case STRING:
            return datum instanceof String
                        || unwrappedInstanceOf(schema, datum);
        case BYTES:
            return datum instanceof DataByteArray
                        || unwrappedInstanceOf(schema, datum);
        case INT:
            return datum instanceof Integer
                        || unwrappedInstanceOf(schema, datum);
        case LONG:
            return datum instanceof Long
                        || datum instanceof Integer
                        || unwrappedInstanceOf(schema, datum);
        case FLOAT:
            return datum instanceof Float
                        || datum instanceof Integer
                        || datum instanceof Long
                        || unwrappedInstanceOf(schema, datum);
        case DOUBLE:
            return datum instanceof Double
                        || datum instanceof Float
                        || datum instanceof Integer
                        || datum instanceof Long
                        || unwrappedInstanceOf(schema, datum);
        case BOOLEAN:
            return datum instanceof Boolean
                        || datum instanceof Integer
                        || unwrappedInstanceOf(schema, datum);
        case NULL:
            return datum == null;
        default:
            throw new RuntimeException("Unexpected type: " + schema);
        }
    } catch (ExecException e) {
        e.printStackTrace(System.err);
        throw new RuntimeException(e);
    }
}
 
Example 5
Source Project: kite   File: CSVRecordBuilder.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Returns a the value as the first matching schema type or null.
 *
 * Note that if the value may be null even if the schema does not allow the
 * value to be null.
 *
 * @param string a String representation of the value
 * @param schema a Schema
 * @return the string coerced to the correct type from the schema or null
 */
private static Object makeValue(@Nullable String string, Schema schema) {
  if (string == null) {
    return null;
  }

  try {
    switch (schema.getType()) {
      case BOOLEAN:
        return Boolean.valueOf(string);
      case STRING:
        return string;
      case FLOAT:
        return Float.valueOf(string);
      case DOUBLE:
        return Double.valueOf(string);
      case INT:
        return Integer.valueOf(string);
      case LONG:
        return Long.valueOf(string);
      case ENUM:
        // TODO: translate to enum class
        if (schema.hasEnumSymbol(string)) {
          return string;
        } else {
          try {
            return schema.getEnumSymbols().get(Integer.parseInt(string));
          } catch (IndexOutOfBoundsException ex) {
            return null;
          }
        }
      case UNION:
        Object value = null;
        for (Schema possible : schema.getTypes()) {
          value = makeValue(string, possible);
          if (value != null) {
            return value;
          }
        }
        return null;
      case NULL:
        return null;
      default:
        // FIXED, BYTES, MAP, ARRAY, RECORD are not supported
        throw new DatasetOperationException(
            "Unsupported field type:" + schema.getType());
    }
  } catch (NumberFormatException e) {
    // empty string is considered null for numeric types
    if (string.isEmpty()) {
      return null;
    } else {
      throw e;
    }
  }
}
 
Example 6
Source Project: kite   File: JsonUtil.java    License: Apache License 2.0 4 votes vote down vote up
private static boolean matches(JsonNode datum, Schema schema) {
  switch (schema.getType()) {
    case RECORD:
      if (datum.isObject()) {
        // check that each field is present or has a default
        boolean missingField = false;
        for (Schema.Field field : schema.getFields()) {
          if (!datum.has(field.name()) && field.defaultValue() == null) {
            missingField = true;
            break;
          }
        }
        if (!missingField) {
          return true;
        }
      }
      break;
    case UNION:
      if (resolveUnion(datum, schema.getTypes()) != null) {
        return true;
      }
      break;
    case MAP:
      if (datum.isObject()) {
        return true;
      }
      break;
    case ARRAY:
      if (datum.isArray()) {
        return true;
      }
      break;
    case BOOLEAN:
      if (datum.isBoolean()) {
        return true;
      }
      break;
    case FLOAT:
      if (datum.isFloat() || datum.isInt()) {
        return true;
      }
      break;
    case DOUBLE:
      if (datum.isDouble() || datum.isFloat() ||
          datum.isLong() || datum.isInt()) {
        return true;
      }
      break;
    case INT:
      if (datum.isInt()) {
        return true;
      }
      break;
    case LONG:
      if (datum.isLong() || datum.isInt()) {
        return true;
      }
      break;
    case STRING:
      if (datum.isTextual()) {
        return true;
      }
      break;
    case ENUM:
      if (datum.isTextual() && schema.hasEnumSymbol(datum.textValue())) {
        return true;
      }
      break;
    case BYTES:
    case FIXED:
      if (datum.isBinary()) {
        return true;
      }
      break;
    case NULL:
      if (datum == null || datum.isNull()) {
        return true;
      }
      break;
    default: // UNION or unknown
      throw new IllegalArgumentException("Unsupported schema: " + schema);
  }
  return false;
}