Package org.apache.avro

Examples of org.apache.avro.AvroTypeException


      case RECORD:
        return resolveRecords(writer, reader, seen);
      case UNION:
        return resolveUnion(writer, reader, seen);
      default:
        throw new AvroTypeException("Unkown type for schema: " + writerType);
      }
    } else // writer and reader are of different types
      if (writerType == Schema.Type.UNION) {
        return resolveUnion(writer, reader, seen);
      }
View Full Code Here


    } else if (top == Symbol.RECORD_START) {
      out.writeStartObject();
    } else if (top == Symbol.RECORD_END || top == Symbol.UNION_END) {
      out.writeEndObject();
    } else {
      throw new AvroTypeException("Unknown action symbol " + top);
    }
    return Symbol.CONTINUE;
  }
View Full Code Here

      Symbol top = stack[--pos];
      if (top.kind == Symbol.Kind.TERMINAL) {
        if (top == input) {
          return top; // A common case
        } else {
          throw new AvroTypeException("Attempt to process a "
              + input + " when a "
              + top + " was expected.");
        }
      } else if (top.kind == Symbol.Kind.IMPLICIT_ACTION) {
          Symbol result = symbolHandler.doAction(input, top);
View Full Code Here

      Schema elementType = null;
      for (Object element : (GenericArray)datum) {
        if (elementType == null) {
          elementType = induce(element);
        } else if (!elementType.equals(induce(element))) {
          throw new AvroTypeException("No mixed type arrays.");
        }
      }
      if (elementType == null) {
        throw new AvroTypeException("Empty array: "+datum);
      }
      return Schema.createArray(elementType);

    } else if (datum instanceof Map) {
      @SuppressWarnings(value="unchecked")
      Map<Object,Object> map = (Map<Object,Object>)datum;
      Schema value = null;
      for (Map.Entry<Object,Object> entry : map.entrySet()) {
        if (value == null) {
          value = induce(entry.getValue());
        } else if (!value.equals(induce(entry.getValue()))) {
          throw new AvroTypeException("No mixed type map values.");
        }
      }
      if (value == null) {
        throw new AvroTypeException("Empty map: "+datum);
      }
      return Schema.createMap(value);
    } else if (datum instanceof GenericFixed) {
      return Schema.createFixed(null, null,
                                ((GenericFixed)datum).bytes().length);
    }
    else if (datum instanceof Utf8)       return Schema.create(Type.STRING);
    else if (datum instanceof ByteBuffer) return Schema.create(Type.BYTES);
    else if (datum instanceof Integer)    return Schema.create(Type.INT);
    else if (datum instanceof Long)       return Schema.create(Type.LONG);
    else if (datum instanceof Float)      return Schema.create(Type.FLOAT);
    else if (datum instanceof Double)     return Schema.create(Type.DOUBLE);
    else if (datum instanceof Boolean)    return Schema.create(Type.BOOLEAN);
    else if (datum == null)               return Schema.create(Type.NULL);

    else throw new AvroTypeException("Can't create schema for: "+datum);
  }
View Full Code Here

    throws IOException {
    out.writeFixed(((GenericFixed)datum).bytes(), 0, schema.getFixedSize());
  }
 
  private void error(Schema schema, Object datum) {
    throw new AvroTypeException("Not a "+schema+": "+datum);
  }
View Full Code Here

        case DOUBLE:
          return branch;
        }
        break;
      }
    throw new AvroTypeException("Expected "+expected+", found "+actual);
  }
View Full Code Here

                              Decoder in) throws IOException {
    /* TODO: We may want to compute the expected and actual mapping and cache
     * the mapping (keyed by <actual, expected>). */
    String recordName = expected.getName();
    if (recordName != null && !recordName.equals(actual.getName()))
      throw new AvroTypeException("Expected "+expected+", found "+actual);
    Map<String, Field> expectedFields = expected.getFields();
    // all fields not in expected should be removed by newRecord.
    Object record = newRecord(old, expected);
    int size = 0;
    for (Map.Entry<String, Field> entry : actual.getFields().entrySet()) {
View Full Code Here

   * representations.  By default, returns the symbol as a String. */
  protected Object readEnum(Schema actual, Schema expected, Decoder in)
    throws IOException {
    String name = expected.getName();
    if (name != null && !name.equals(actual.getName()))
      throw new AvroTypeException("Expected "+expected+", found "+actual);
    return createEnum(actual.getEnumSymbols().get(in.readEnum()), expected);
  }
View Full Code Here

   * representations.  By default, returns {@link GenericFixed}. */
  protected Object readFixed(Object old, Schema actual, Schema expected,
                             Decoder in)
    throws IOException {
    if (!actual.equals(expected))
      throw new AvroTypeException("Expected "+expected+", found "+actual);
    GenericFixed fixed = (GenericFixed)createFixed(old, expected);
    in.readFixed(fixed.bytes(), 0, actual.getFixedSize());
    return fixed;
  }
View Full Code Here

      java.lang.reflect.Type[] params = ptype.getActualTypeArguments();
      for (int i = 0; i < params.length; i++)
        System.out.println("param ="+params[i]);
      if (GenericArray.class.isAssignableFrom(raw)) { // array
        if (params.length != 1)
          throw new AvroTypeException("No array type specified.");
        return Schema.createArray(createSchema(params[0], names));
      } else if (Map.class.isAssignableFrom(raw)) { // map
        java.lang.reflect.Type key = params[0];
        java.lang.reflect.Type value = params[1];
        if (!(key == Utf8.class))
          throw new AvroTypeException("Map key class not Utf8: "+key);
        return Schema.createMap(createSchema(value, names));
      }
    } else if (type instanceof Class) {
      Class c = (Class)type;
      String name = c.getSimpleName();
      String space = c.getPackage().getName();
     
      Schema schema = names.get(name);
      if (schema == null) {

        if (c.isEnum()) {                         // enum
          List<String> symbols = new ArrayList<String>();
          Enum[] constants = (Enum[])c.getEnumConstants();
          for (int i = 0; i < constants.length; i++)
            symbols.add(constants[i].name());
          schema = Schema.createEnum(name, space, symbols);
          names.put(name, schema);
          return schema;
        }
                                                  // fixed
        if (GenericFixed.class.isAssignableFrom(c)) {
          int size = ((FixedSize)c.getAnnotation(FixedSize.class)).value();
          schema = Schema.createFixed(name, space, size);
          names.put(name, schema);
          return schema;
        }
                                                  // record
        LinkedHashMap<String,Schema.Field> fields =
          new LinkedHashMap<String,Schema.Field>();
        schema = Schema.createRecord(name, space,
                                     Throwable.class.isAssignableFrom(c));
        if (!names.containsKey(name))
          names.put(name, schema);
        for (Field field : c.getDeclaredFields())
          if ((field.getModifiers()&(Modifier.TRANSIENT|Modifier.STATIC))==0) {
            Schema fieldSchema = createFieldSchema(field, names);
            fields.put(field.getName(), new Schema.Field(fieldSchema, null));
          }
        schema.setFields(fields);
      }
      return schema;
    }
    throw new AvroTypeException("Unknown type: "+type);
  }
View Full Code Here

TOP

Related Classes of org.apache.avro.AvroTypeException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.