Java Code Examples for com.esotericsoftware.kryo.io.Output#position()

The following examples show how to use com.esotericsoftware.kryo.io.Output#position() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultKryoStreamCodec.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public Slice toByteArray(T o)
{
  final Output output = new Output(32, -1);
  try {
    kryo.writeClassAndObject(output, o);
  } finally {
    output.close();
  }
  return new Slice(output.getBuffer(), 0, output.position());
}
 
Example 2
Source File: DefaultKryoStreamCodec.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
@Override
public Slice toByteArray(T o)
{
  final Output output = new Output(32, -1);
  try {
    kryo.writeClassAndObject(output, o);
  } finally {
    output.close();
  }
  return new Slice(output.getBuffer(), 0, output.position());
}
 
Example 3
Source File: ApplicationDimensionComputation.java    From streaming-benchmarks with Apache License 2.0 5 votes vote down vote up
@Override
public Slice toByteArray(Aggregate o)
{
  final Output output = new Output(32, -1);
  try {
    kryo.writeClassAndObject(output, o);
  } finally {
    output.close();
  }
  return new Slice(output.getBuffer(), 0, output.position());
}
 
Example 4
Source File: KryoSerializer.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(T record, DataOutputView target) throws IOException {
	if (CONCURRENT_ACCESS_CHECK) {
		enterExclusiveThread();
	}

	try {
		checkKryoInitialized();

		if (target != previousOut) {
			DataOutputViewStream outputStream = new DataOutputViewStream(target);
			output = new Output(outputStream);
			previousOut = target;
		}

		// Sanity check: Make sure that the output is cleared/has been flushed by the last call
		// otherwise data might be written multiple times in case of a previous EOFException
		if (output.position() != 0) {
			throw new IllegalStateException("The Kryo Output still contains data from a previous " +
				"serialize call. It has to be flushed or cleared at the end of the serialize call.");
		}

		try {
			kryo.writeClassAndObject(output, record);
			output.flush();
		}
		catch (KryoException ke) {
			// make sure that the Kryo output buffer is cleared in case that we can recover from
			// the exception (e.g. EOFException which denotes buffer full)
			output.clear();

			Throwable cause = ke.getCause();
			if (cause instanceof EOFException) {
				throw (EOFException) cause;
			}
			else {
				throw ke;
			}
		}
	}
	finally {
		if (CONCURRENT_ACCESS_CHECK) {
			exitExclusiveThread();
		}
	}
}
 
Example 5
Source File: KryoSerializer.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(T record, DataOutputView target) throws IOException {
	if (CONCURRENT_ACCESS_CHECK) {
		enterExclusiveThread();
	}

	try {
		checkKryoInitialized();

		if (target != previousOut) {
			DataOutputViewStream outputStream = new DataOutputViewStream(target);
			output = new Output(outputStream);
			previousOut = target;
		}

		// Sanity check: Make sure that the output is cleared/has been flushed by the last call
		// otherwise data might be written multiple times in case of a previous EOFException
		if (output.position() != 0) {
			throw new IllegalStateException("The Kryo Output still contains data from a previous " +
				"serialize call. It has to be flushed or cleared at the end of the serialize call.");
		}

		try {
			kryo.writeClassAndObject(output, record);
			output.flush();
		}
		catch (KryoException ke) {
			// make sure that the Kryo output buffer is cleared in case that we can recover from
			// the exception (e.g. EOFException which denotes buffer full)
			output.clear();

			Throwable cause = ke.getCause();
			if (cause instanceof EOFException) {
				throw (EOFException) cause;
			}
			else {
				throw ke;
			}
		}
	}
	finally {
		if (CONCURRENT_ACCESS_CHECK) {
			exitExclusiveThread();
		}
	}
}
 
Example 6
Source File: TestParquetWriter.java    From paraflow with Apache License 2.0 4 votes vote down vote up
@Test
public void testParquetCompression()
{
    final Kryo kryo = new Kryo();
    kryo.register(LineOrder.class);
    kryo.register(byte[].class);
    kryo.register(Object[].class);

    final LoaderConfig config = LoaderConfig.INSTANCE();
    try {
        config.init();
    }
    catch (ConfigFileNotFoundException e) {
        e.printStackTrace();
    }
    final MetaClient metaClient = new MetaClient("127.0.0.1", 10012);
    final int capacity = 8;
    Iterable<LineOrder> lineOrderIterable = TpchTable.LINEORDER.createGenerator(1000, 1, 1500, 0, 10000000);
    Iterator<LineOrder> lineOrderIterator = lineOrderIterable.iterator();
    TpchDataTransformer transformer = new TpchDataTransformer();
    ParaflowRecord[][] content = new ParaflowRecord[1][];
    ParaflowRecord[] records = new ParaflowRecord[capacity];
    int counter = 0;
    long textSize = 0;
    Output output = new ByteBufferOutput(300, 2000);
    while (lineOrderIterator.hasNext() && counter < capacity) {
        LineOrder lineOrder = lineOrderIterator.next();
        kryo.writeObject(output, lineOrder);
        textSize += output.position();
        ParaflowRecord record = transformer.transform(output.toBytes(), 80);
        records[counter++] = record;
        output.reset();
    }
    content[0] = records;
    output.close();
    ParaflowSegment segment = new ParaflowSegment(content, new long[0], new long[0], 0.0d);
    segment.setPath("file:///Users/Jelly/Desktop/1");
    MetaProto.StringListType columnNames = metaClient.listColumns("test", "debug01");
    MetaProto.StringListType columnTypes = metaClient.listColumnsDataType("test", "debug01");
    final ParquetSegmentWriter segmentWriter = new ParquetSegmentWriter(segment, metaClient, null);
    long start = System.currentTimeMillis();
    if (segmentWriter.write(segment, columnNames, columnTypes)) {
        System.out.println("Binary size: " + (1.0 * textSize / 1024.0 / 1024.0) + " MB.");
    }
    long end = System.currentTimeMillis();
    System.out.println("Time cost: " + (end - start));
}
 
Example 7
Source File: KryoSerializer.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(T record, DataOutputView target) throws IOException {
	if (CONCURRENT_ACCESS_CHECK) {
		enterExclusiveThread();
	}

	try {
		checkKryoInitialized();

		if (target != previousOut) {
			DataOutputViewStream outputStream = new DataOutputViewStream(target);
			output = new Output(outputStream);
			previousOut = target;
		}

		// Sanity check: Make sure that the output is cleared/has been flushed by the last call
		// otherwise data might be written multiple times in case of a previous EOFException
		if (output.position() != 0) {
			throw new IllegalStateException("The Kryo Output still contains data from a previous " +
				"serialize call. It has to be flushed or cleared at the end of the serialize call.");
		}

		try {
			kryo.writeClassAndObject(output, record);
			output.flush();
		}
		catch (KryoException ke) {
			// make sure that the Kryo output buffer is cleared in case that we can recover from
			// the exception (e.g. EOFException which denotes buffer full)
			output.clear();

			Throwable cause = ke.getCause();
			if (cause instanceof EOFException) {
				throw (EOFException) cause;
			}
			else {
				throw ke;
			}
		}
	}
	finally {
		if (CONCURRENT_ACCESS_CHECK) {
			exitExclusiveThread();
		}
	}
}