Search in sources :

Example 1 with ByteBufferOutputStream

use of org.apache.avro.util.ByteBufferOutputStream in project gora by apache.

the class TestIOUtils method testSerializeDeserialize.

@SafeVarargs
@SuppressWarnings("unchecked")
public static <T> void testSerializeDeserialize(T... objects) throws Exception {
    ByteBufferOutputStream os = new ByteBufferOutputStream();
    DataOutputStream dos = new DataOutputStream(os);
    ByteBufferInputStream is = null;
    DataInputStream dis = null;
    GoraMapReduceUtils.setIOSerializations(conf, true);
    try {
        for (T before : objects) {
            IOUtils.serialize(conf, dos, before, (Class<T>) before.getClass());
            dos.flush();
        }
        is = new ByteBufferInputStream(os.getBufferList());
        dis = new DataInputStream(is);
        for (T before : objects) {
            T after = IOUtils.deserialize(conf, dis, null, (Class<T>) before.getClass());
            if (before instanceof BoolArrayWrapper) {
                if (after instanceof BoolArrayWrapper) {
                    log.info("Before : " + java.util.Arrays.toString(((BoolArrayWrapper) before).arr));
                    log.info("After : " + java.util.Arrays.toString(((BoolArrayWrapper) after).arr));
                }
            } else if (before instanceof StringArrayWrapper) {
                if (after instanceof StringArrayWrapper) {
                    log.info("Before : " + java.util.Arrays.toString(((StringArrayWrapper) before).arr));
                    log.info("After : " + java.util.Arrays.toString(((StringArrayWrapper) after).arr));
                }
            } else {
                log.info("Before : " + before);
                log.info("After : " + before);
            }
            assertEquals(before, after);
            if ((before instanceof PersistentBase) && (after instanceof PersistentBase)) {
                assertEquals(Arrays.equals(((PersistentBase) before).getDirtyBytes().array(), ((PersistentBase) after).getDirtyBytes().array()), true);
            }
        }
        //assert that the end of input is reached
        long skipped = dis.skip(1);
        assertEquals(0, skipped);
    } catch (EOFException expected) {
    //either should throw exception or return 0 as skipped
    } finally {
        org.apache.hadoop.io.IOUtils.closeStream(dos);
        org.apache.hadoop.io.IOUtils.closeStream(os);
        org.apache.hadoop.io.IOUtils.closeStream(dis);
        org.apache.hadoop.io.IOUtils.closeStream(is);
    }
}
Also used : PersistentBase(org.apache.gora.persistency.impl.PersistentBase) ByteBufferOutputStream(org.apache.avro.util.ByteBufferOutputStream) DataOutputStream(java.io.DataOutputStream) ByteBufferInputStream(org.apache.avro.util.ByteBufferInputStream) EOFException(java.io.EOFException) DataInputStream(java.io.DataInputStream)

Example 2 with ByteBufferOutputStream

use of org.apache.avro.util.ByteBufferOutputStream in project gora by apache.

the class IOUtils method serialize.

/**
   * Serializes the object to the given data output using
   * available Hadoop serializations.
   *
   * @param conf Hadoop conf.
   * @param obj object instance to be serialized.
   * @param out data stream which serialized content is written.
   * @param objClass Class type of the object to be serialized.
   * @param <T> class type of object to be serialized.
   * @throws IOException occurred while serializing the object to bytes.
   */
public static <T> void serialize(Configuration conf, DataOutput out, T obj, Class<T> objClass) throws IOException {
    SerializationFactory serializationFactory = new SerializationFactory(getOrCreateConf(conf));
    Serializer<T> serializer = serializationFactory.getSerializer(objClass);
    try (ByteBufferOutputStream os = new ByteBufferOutputStream()) {
        serializer.open(os);
        serializer.serialize(obj);
        int length = 0;
        List<ByteBuffer> buffers = os.getBufferList();
        for (ByteBuffer buffer : buffers) {
            length += buffer.limit() - buffer.arrayOffset();
        }
        WritableUtils.writeVInt(out, length);
        for (ByteBuffer buffer : buffers) {
            byte[] arr = buffer.array();
            out.write(arr, buffer.arrayOffset(), buffer.limit());
        }
    } finally {
        if (serializer != null)
            serializer.close();
    }
}
Also used : ByteBufferOutputStream(org.apache.avro.util.ByteBufferOutputStream) SerializationFactory(org.apache.hadoop.io.serializer.SerializationFactory) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteBufferOutputStream (org.apache.avro.util.ByteBufferOutputStream)2 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 EOFException (java.io.EOFException)1 ByteBuffer (java.nio.ByteBuffer)1 ByteBufferInputStream (org.apache.avro.util.ByteBufferInputStream)1 PersistentBase (org.apache.gora.persistency.impl.PersistentBase)1 SerializationFactory (org.apache.hadoop.io.serializer.SerializationFactory)1