Package org.apache.avro.Schema

Examples of org.apache.avro.Schema.Type


   * @param schema  the schema belonging to the particular Avro field
   * @param value   the field value
   */
  @SuppressWarnings({ "unchecked", "rawtypes" })
  private void addOrUpdateField(K key, Field field, Schema schema, Object value) {
    Type type = schema.getType();
    // checking if the value to be updated is used for saving union schema
    if (field.name().indexOf(CassandraStore.UNION_COL_SUFIX) < 0){
      switch (type) {
      case STRING:
      case BOOLEAN:
      case INT:
      case LONG:
      case BYTES:
      case FLOAT:
      case DOUBLE:
      case FIXED:
        this.cassandraClient.addColumn(key, field.name(), value);
        break;
      case RECORD:
        if (value != null) {
          if (value instanceof PersistentBase) {
            PersistentBase persistentBase = (PersistentBase) value;           
            try {
              byte[] byteValue = AvroSerializerUtil.serializer(persistentBase, schema);
              this.cassandraClient.addColumn(key, field.name(), byteValue);
            } catch (IOException e) {
              LOG.warn(field.name() + " named record could not be serialized.");
            }
          } else {
            LOG.warn("Record with value: " + value.toString() + " not supported for field: " + field.name());
          }
        } else {
          LOG.warn("Setting content of: " + field.name() + " to null.");
          String familyName =  this.cassandraClient.getCassandraMapping().getFamily(field.name());
          this.cassandraClient.deleteColumn(key, familyName, this.cassandraClient.toByteBuffer(field.name()));
        }
        break;
      case MAP:
        if (value != null) {
          if (value instanceof Map<?, ?>) {           
            Map<CharSequence,Object> map = (Map<CharSequence,Object>)value;
            Schema valueSchema = schema.getValueType();
            Type valueType = valueSchema.getType();
            if (Type.UNION.equals(valueType)){
              Map<CharSequence,Object> valueMap = new HashMap<CharSequence, Object>();
              for (CharSequence mapKey: map.keySet()) {
                Object mapValue = map.get(mapKey);
                int valueUnionIndex = getUnionSchema(mapValue, valueSchema);
View Full Code Here


  private int getUnionSchema(Object pValue, Schema pUnionSchema){
    int unionSchemaPos = 0;
//    String valueType = pValue.getClass().getSimpleName();
    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
    return DEFAULT_UNION_SCHEMA;
View Full Code Here

        init(null, schema, false);
    }

    private boolean isNamedSchema(Schema schema) {
        Type type = schema.getType();
        return type.equals(Type.RECORD) || type.equals(Type.ENUM) || type.equals(Type.FIXED);
    }
View Full Code Here

  //TODO temporary solution, has to be changed after implementation of saving the index of union type
  private int getResolvedUnionIndex(Schema unionScema) {
    if (unionScema.getTypes().size() == 2) {

      // schema [type0, type1]
      Type type0 = unionScema.getTypes().get(0).getType();
      Type type1 = unionScema.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))
          return 1;
        else
          return 0;
View Full Code Here

    // // // Shared helper methods

    protected GenericRecord _createRecord(Schema schema) throws JsonMappingException
    {
        // Quick check: if type is Union, need to find actual record type...
        Type type = schema.getType();
        if (type == Schema.Type.UNION) {
            schema = _recordOrMapFromUnion(schema);
        }
        if (type == Schema.Type.MAP) {
            throw new IllegalStateException("_createRecord should never be called for elements of type MAP");
View Full Code Here

    for (int i = 0; iter.hasNext(); i++) {
      Field field = iter.next();
      if (!stateManager.isDirty(persistent, i)) {
        continue;
      }
      Type type = field.schema().getType();
      Object o = persistent.get(i);
      HBaseColumn hcol = mapping.getColumn(field.name());
      switch(type) {
        case MAP:
          if(o instanceof StatefulMap) {
View Full Code Here

    if (value != null) {
      return true;
    }
   
    Schema schema = f.schema();
    Type type = schema.getType();
   
    // If the type is null, any value is valid
    if (type == Type.NULL) {
      return true;
    }
View Full Code Here

  @SuppressWarnings("unchecked")
  private final Value convertAvroToPactValue(final Field field, final Object avroRecord) {
    if (avroRecord == null) {
      return null;
    }
    final Type type = checkTypeConstraintsAndGetType(field.schema());

    // check for complex types
    // (complex type FIXED is not yet supported)
    switch (type) {
      case ARRAY:
        final Type elementType = field.schema().getElementType().getType();
        final List<?> avroList = (List<?>) avroRecord;
        return convertAvroArrayToListValue(elementType, avroList);
      case ENUM:
        final List<String> symbols = field.schema().getEnumSymbols();
        final String avroRecordString = avroRecord.toString();
        if (!symbols.contains(avroRecordString)) {
          throw new RuntimeException("The given Avro file contains field with a invalid enum symbol");
        }
        sString.setValue(avroRecordString);
        return sString;
      case MAP:
        final Type valueType = field.schema().getValueType().getType();
        final Map<CharSequence, ?> avroMap = (Map<CharSequence, ?>) avroRecord;
        return convertAvroMapToMapValue(valueType, avroMap);
 
      // primitive type
      default:
View Full Code Here

              + " for AvroInputFormat is not implemented. Open an issue on GitHub.");
    }
  }

  private final Type checkTypeConstraintsAndGetType(final Schema schema) {
    final Type type = schema.getType();
    if (type == Type.RECORD) {
      throw new RuntimeException("The given Avro file contains complex data types which are not supported right now");
    }

    if (type == Type.UNION) {
View Full Code Here

    if (value != null) {
      return true;
    }
   
    Schema schema = f.schema();
    Type type = schema.getType();
   
    // If the type is null, any value is valid
    if (type == Type.NULL) {
      return true;
    }
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.