Package org.apache.avro.Schema

Examples of org.apache.avro.Schema.Type


      fieldValue = new Utf8(solrValue.toString());
      break;
    case UNION:
      if (fieldSchema.getTypes().size() == 2 && isNullable(fieldSchema)) {
        // schema [type0, type1]
        Type type0 = fieldSchema.getTypes().get(0).getType();
        Type type1 = fieldSchema.getTypes().get(1).getType();

        // Check if types are different and there's a "null", like
        // ["null","type"] or ["type","null"]
        if (!type0.equals(type1)) {
          if (type0.equals(Schema.Type.NULL))
View Full Code Here


   */
  private int getUnionSchema(Object pValue, Schema pUnionSchema) {
    int unionSchemaPos = 0;
    Iterator<Schema> it = pUnionSchema.getTypes().iterator();
    while (it.hasNext()) {
      Type schemaType = it.next().getType();
      if (pValue instanceof Utf8 && schemaType.equals(Type.STRING))
        return unionSchemaPos;
      else if (pValue instanceof ByteBuffer && schemaType.equals(Type.BYTES))
        return unionSchemaPos;
      else if (pValue instanceof Integer && schemaType.equals(Type.INT))
        return unionSchemaPos;
      else if (pValue instanceof Long && schemaType.equals(Type.LONG))
        return unionSchemaPos;
      else if (pValue instanceof Double && schemaType.equals(Type.DOUBLE))
        return unionSchemaPos;
      else if (pValue instanceof Float && schemaType.equals(Type.FLOAT))
        return unionSchemaPos;
      else if (pValue instanceof Boolean && schemaType.equals(Type.BOOLEAN))
        return unionSchemaPos;
      else if (pValue instanceof Map && schemaType.equals(Type.MAP))
        return unionSchemaPos;
      else if (pValue instanceof List && schemaType.equals(Type.ARRAY))
        return unionSchemaPos;
      else if (pValue instanceof Persistent && schemaType.equals(Type.RECORD))
        return unionSchemaPos;
      unionSchemaPos++;
    }
    // if we weren't able to determine which data type it is, then we return the
    // default
View Full Code Here

   * @return Enum|Utf8|ByteBuffer|Integer|Long|Float|Double|Boolean|Persistent|Null
   * @throws IOException
   */
  @SuppressWarnings({ "rawtypes" })
  public static Object fromBytes(Schema schema, byte[] val) throws IOException {
    Type type = schema.getType();
    switch (type) {
    case ENUM:    return AvroUtils.getEnumValue(schema, val[0]);
    case STRING:  return new Utf8(Bytes.toString(val));
    case BYTES:   return ByteBuffer.wrap(val);
    case INT:     return Bytes.toInt(val);
    case LONG:    return Bytes.toLong(val);
    case FLOAT:   return Bytes.toFloat(val);
    case DOUBLE:  return Bytes.toDouble(val);
    case BOOLEAN: return val[0] != 0;
    case UNION:
      // XXX Special case: When reading the top-level field of a record we must handle the
      // special case ["null","type"] definitions: this will be written as if it was ["type"]
      // if not in a special case, will execute "case RECORD".
     
      // if 'val' is empty we ignore the special case (will match Null in "case RECORD") 
      if (schema.getTypes().size() == 2) {
       
        // schema [type0, type1]
        Type type0 = schema.getTypes().get(0).getType() ;
        Type type1 = schema.getTypes().get(1).getType() ;
       
        // Check if types are different and there's a "null", like ["null","type"] or ["type","null"]
        if (!type0.equals(type1)
            && (   type0.equals(Schema.Type.NULL)
                || type1.equals(Schema.Type.NULL))) {

          if (type0.equals(Schema.Type.NULL))
            schema = schema.getTypes().get(1) ;
          else
            schema = schema.getTypes().get(0) ;
View Full Code Here

   * @return array of bytes of the serialized object
   * @throws IOException
   */
  @SuppressWarnings({ "rawtypes", "unchecked" })
  public static byte[] toBytes(Object o, Schema schema) throws IOException {
    Type type = schema.getType();
    switch (type) {
    case STRING:  return Bytes.toBytes(((CharSequence)o).toString()); // TODO: maybe ((Utf8)o).getBytes(); ?
    case BYTES:   return ((ByteBuffer)o).array();
    case INT:     return Bytes.toBytes((Integer)o);
    case LONG:    return Bytes.toBytes((Long)o);
View Full Code Here

    return Types.OTHER;
  }

  public static JdbcType getJdbcType(Schema schema, int length, int scale) throws IOException {
    Type type = schema.getType();

    switch(type) {
      case MAP    : return JdbcType.BLOB;
      case ARRAY  : return JdbcType.BLOB;
      case BOOLEAN: return JdbcType.BIT;
View Full Code Here

  }

  @SuppressWarnings({ "rawtypes", "unchecked" })
  public static <T> Serializer<T> getSerializer(Schema schema) {
    Serializer serializer = null;
    Type type = schema.getType();
    if (type.equals(Type.STRING)) {
      serializer = CharSequenceSerializer.get();
    } else if (type.equals(Type.BOOLEAN)) {
      serializer = BooleanSerializer.get();
    } else if (type.equals(Type.BYTES)) {
      serializer = ByteBufferSerializer.get();
    } else if (type.equals(Type.DOUBLE)) {
      serializer = DoubleSerializer.get();
    } else if (type.equals(Type.FLOAT)) {
      serializer = FloatSerializer.get();
    } else if (type.equals(Type.INT)) {
      serializer = IntegerSerializer.get();
    } else if (type.equals(Type.LONG)) {
      serializer = LongSerializer.get();
    } else if (type.equals(Type.FIXED)) {
      Class clazz = TypeUtils.getClass(schema);
      serializer = SpecificFixedSerializer.get(clazz);
      // serializer = SpecificFixedSerializer.get(schema);
    } else if (type.equals(Type.ARRAY)) {
      serializer = ListSerializer.get(schema.getElementType());
    } else if (type.equals(Type.MAP)) {
      serializer = MapSerializer.get(schema.getValueType());
    } else if (type.equals(Type.UNION)){
      serializer = ByteBufferSerializer.get();
    } else if (type.equals(Type.RECORD)){
      serializer = BytesArraySerializer.get();
    } else {
      serializer = null;
    }
    return serializer;
View Full Code Here

    }
    return serializer;
  }

  public static MapSerializer get(Schema valueSchema) {
    Type type = valueSchema.getType();
    if (type == Type.FIXED) {
      return get(Type.FIXED, TypeUtils.getClass(valueSchema));
    } else {
      return get(type);
    }
View Full Code Here

    }
    return serializer;
  }

  public static ListSerializer get(Schema elementSchema) {
    Type type = elementSchema.getType();
    if (type == Type.FIXED) {
      return get(Type.FIXED, TypeUtils.getClass(elementSchema));
    } else {
      return get(type);
    }
View Full Code Here

        // get field
        if (fieldName.indexOf(CassandraStore.UNION_COL_SUFIX) < 0) {

          int pos = this.persistent.getSchema().getField(fieldName).pos();
          Field field = fields.get(pos);
          Type fieldType = field.schema().getType();
          // LOG.info(StringSerializer.get().fromByteBuffer(cassandraColumn.getName())
          // + fieldName + " " + fieldType.name());
          if (fieldType.equals(Type.UNION)) {
            //getting UNION stored type
            CassandraColumn cc = getUnionTypeColumn(fieldName
                + CassandraStore.UNION_COL_SUFIX, cassandraRow.toArray());
            //creating temporary UNION Field
            Field unionField = new Field(fieldName
View Full Code Here

    } else if (type.equals(Type.RECORD)){
      value = fromByteBuffer(fieldSchema, byteBuffer);
    } else if (type.equals(Type.UNION)){
      // the selected union schema is obtained
      Schema unionFieldSchema = getUnionSchema(super.getUnionType(), fieldSchema);
      Type unionFieldType = unionFieldSchema.getType();
      // we use the selected union schema to deserialize our actual value
      //value = fromByteBuffer(unionFieldSchema, byteBuffer);
      value = getFieldValue(unionFieldType, unionFieldSchema, byteBuffer);
    } else {
      value = fromByteBuffer(fieldSchema, byteBuffer);
View Full Code Here

TOP

Related Classes of org.apache.avro.Schema.Type

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.