Package org.apache.avro.specific

Examples of org.apache.avro.specific.SpecificDatumWriter


    case DOUBLE:  return Bytes.toBytes((Double)o);
    case BOOLEAN: return (Boolean)o ? new byte[] {1} : new byte[] {0};
    case ENUM:    return new byte[] { (byte)((Enum<?>) o).ordinal() };
    case UNION:
    case RECORD:
      SpecificDatumWriter writer = (SpecificDatumWriter<?>) writerMap.get(schema.getFullName());
      if (writer == null) {
        writer = new SpecificDatumWriter(schema);// ignore dirty bits
        writerMap.put(schema.getFullName(),writer);
      }

      BinaryEncoder encoderFromCache = encoders.get();
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      outputStream.set(bos);
      BinaryEncoder encoder = EncoderFactory.get().directBinaryEncoder(bos, null);
      if (encoderFromCache == null) {
        encoders.set(encoder);
      }

      //reset the buffers
      ByteArrayOutputStream os = outputStream.get();
      os.reset();

      writer.write(o, encoder);
      encoder.flush();
      return os.toByteArray();
    default: throw new RuntimeException("Unknown type: "+type);
    }
  }
View Full Code Here


  public static final ConcurrentHashMap<String, SpecificDatumReader<?>> readerMap =
      new ConcurrentHashMap<String, SpecificDatumReader<?>>();
 
  @SuppressWarnings({ "unchecked", "rawtypes" })
  public static <T> byte[] serializer(T value, Schema schema) throws IOException{
    SpecificDatumWriter writer = (SpecificDatumWriter<?>) writerMap.get(schema.getFullName());
    if (writer == null) {
      writer = new SpecificDatumWriter(schema);// ignore dirty bits
      writerMap.put(schema.getFullName(),writer);
    }
   
    BinaryEncoder encoderFromCache = encoders.get();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    outputStream.set(bos);
    BinaryEncoder encoder = EncoderFactory.get().directBinaryEncoder(bos, null);
    if (encoderFromCache == null) {
      encoders.set(encoder);
    }
   
    //reset the buffers
    ByteArrayOutputStream os = outputStream.get();
    os.reset();
   
    writer.write(value, encoder);
    encoder.flush();
    byte[] byteValue = os.toByteArray();
    return byteValue;
  }
View Full Code Here

  }

  @InterfaceAudience.Private
  @Override
  public DatumWriter getWriter(Class<SpecificRecord> clazz) {
    return new SpecificDatumWriter();
  }
View Full Code Here

            m.put(col.getFirst(), new Text(toBytes(j++)), new Value(toBytes(item)));
            count++;
          }
          break;
        case RECORD:
          SpecificDatumWriter writer = new SpecificDatumWriter(field.schema());
          ByteArrayOutputStream os = new ByteArrayOutputStream();
          BinaryEncoder encoder = new BinaryEncoder(os);
          writer.write(o, encoder);
          encoder.flush();
          m.put(col.getFirst(), col.getSecond(), new Value(os.toByteArray()));
          break;
        default:
          m.put(col.getFirst(), col.getSecond(), new Value(toBytes(o)));
View Full Code Here

TOP

Related Classes of org.apache.avro.specific.SpecificDatumWriter

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.