Java Code Examples for org.apache.flink.api.java.typeutils.GenericTypeInfo#createSerializer()

The following examples show how to use org.apache.flink.api.java.typeutils.GenericTypeInfo#createSerializer() . 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: NestedRowTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testNestedRowWithMultipleSegments() {
	BinaryRow row = getBinaryRow();
	GenericTypeInfo<MyObj> info = new GenericTypeInfo<>(MyObj.class);
	TypeSerializer<MyObj> genericSerializer = info.createSerializer(new ExecutionConfig());

	MemorySegment[] segments = splitBytes(row.getSegments()[0].getHeapMemory(), 3);
	row.pointTo(segments, 3, row.getSizeInBytes());
	{
		BaseRow nestedRow = row.getRow(0, 5);
		assertEquals(nestedRow.getInt(0), 1);
		assertEquals(nestedRow.getLong(1), 5L);
		assertEquals(nestedRow.getString(2), BinaryString.fromString("12345678"));
		assertTrue(nestedRow.isNullAt(3));
		assertEquals(new MyObj(15, 5),
			BinaryGeneric.getJavaObjectFromBinaryGeneric(nestedRow.getGeneric(4), genericSerializer));
	}
}
 
Example 2
Source File: NestedRowDataTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testNestedRowDataWithMultipleSegments() {
	BinaryRowData row = getBinaryRowData();
	GenericTypeInfo<MyObj> info = new GenericTypeInfo<>(MyObj.class);
	TypeSerializer<MyObj> genericSerializer = info.createSerializer(new ExecutionConfig());

	MemorySegment[] segments = splitBytes(row.getSegments()[0].getHeapMemory(), 3);
	row.pointTo(segments, 3, row.getSizeInBytes());
	{
		RowData nestedRow = row.getRow(0, 5);
		assertEquals(nestedRow.getInt(0), 1);
		assertEquals(nestedRow.getLong(1), 5L);
		assertEquals(nestedRow.getString(2), StringData.fromString("12345678"));
		assertTrue(nestedRow.isNullAt(3));
		assertEquals(new MyObj(15, 5), nestedRow.<MyObj>getRawValue(4).toObject(genericSerializer));
	}
}
 
Example 3
Source File: BinaryRowTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenericObject() throws Exception {

	GenericTypeInfo<MyObj> info = new GenericTypeInfo<>(MyObj.class);
	TypeSerializer<MyObj> genericSerializer = info.createSerializer(new ExecutionConfig());

	BinaryRow row = new BinaryRow(4);
	BinaryRowWriter writer = new BinaryRowWriter(row);
	writer.writeInt(0, 0);

	BinaryGeneric<MyObj> myObj1 = new BinaryGeneric<>(new MyObj(0, 1), genericSerializer);
	writer.writeGeneric(1, myObj1);
	BinaryGeneric<MyObj> myObj2 = new BinaryGeneric<>(new MyObj(123, 5.0), genericSerializer);
	myObj2.ensureMaterialized();
	writer.writeGeneric(2, myObj2);
	BinaryGeneric<MyObj> myObj3 = new BinaryGeneric<>(new MyObj(1, 1), genericSerializer);
	writer.writeGeneric(3, myObj3);
	writer.complete();

	assertTestGenericObjectRow(row, genericSerializer);

	// getBytes from var-length memorySegments.
	BinaryRowSerializer serializer = new BinaryRowSerializer(4);
	MemorySegment[] memorySegments = new MemorySegment[3];
	ArrayList<MemorySegment> memorySegmentList = new ArrayList<>();
	for (int i = 0; i < 3; i++) {
		memorySegments[i] = MemorySegmentFactory.wrap(new byte[64]);
		memorySegmentList.add(memorySegments[i]);
	}
	RandomAccessOutputView out = new RandomAccessOutputView(memorySegments, 64);
	serializer.serializeToPages(row, out);

	BinaryRow mapRow = serializer.mapFromPages(new RandomAccessInputView(memorySegmentList, 64));
	assertTestGenericObjectRow(mapRow, genericSerializer);
}
 
Example 4
Source File: BinaryRowTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testZeroOutPaddingGeneric() {

	GenericTypeInfo<MyObj> info = new GenericTypeInfo<>(MyObj.class);
	TypeSerializer<MyObj> genericSerializer = info.createSerializer(new ExecutionConfig());

	Random random = new Random();
	byte[] bytes = new byte[1024];

	BinaryRow row = new BinaryRow(1);
	BinaryRowWriter writer = new BinaryRowWriter(row);

	// let's random the bytes
	writer.reset();
	random.nextBytes(bytes);
	writer.writeBinary(0, bytes);
	writer.reset();
	writer.writeGeneric(0, new BinaryGeneric<>(new MyObj(0, 1), genericSerializer));
	writer.complete();
	int hash1 = row.hashCode();

	writer.reset();
	random.nextBytes(bytes);
	writer.writeBinary(0, bytes);
	writer.reset();
	writer.writeGeneric(0, new BinaryGeneric<>(new MyObj(0, 1), genericSerializer));
	writer.complete();
	int hash2 = row.hashCode();

	assertEquals(hash1, hash2);
}
 
Example 5
Source File: NestedRowTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testNestedRowWithOneSegment() {
	BinaryRow row = getBinaryRow();
	GenericTypeInfo<MyObj> info = new GenericTypeInfo<>(MyObj.class);
	TypeSerializer<MyObj> genericSerializer = info.createSerializer(new ExecutionConfig());

	BaseRow nestedRow = row.getRow(0, 5);
	assertEquals(nestedRow.getInt(0), 1);
	assertEquals(nestedRow.getLong(1), 5L);
	assertEquals(nestedRow.getString(2), BinaryString.fromString("12345678"));
	assertTrue(nestedRow.isNullAt(3));
	assertEquals(new MyObj(15, 5),
		BinaryGeneric.getJavaObjectFromBinaryGeneric(nestedRow.getGeneric(4), genericSerializer));
}
 
Example 6
Source File: NestedRowTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private BinaryRow getBinaryRow() {
	BinaryRow row = new BinaryRow(1);
	BinaryRowWriter writer = new BinaryRowWriter(row);

	GenericTypeInfo<MyObj> info = new GenericTypeInfo<>(MyObj.class);
	TypeSerializer<MyObj> genericSerializer = info.createSerializer(new ExecutionConfig());
	GenericRow gRow = new GenericRow(5);
	gRow.setField(0, 1);
	gRow.setField(1, 5L);
	gRow.setField(2, BinaryString.fromString("12345678"));
	gRow.setField(3, null);
	gRow.setField(4, new BinaryGeneric<>(new MyObj(15, 5), genericSerializer));

	BaseRowSerializer serializer = new BaseRowSerializer(
		new LogicalType[]{
			DataTypes.INT().getLogicalType(),
			DataTypes.BIGINT().getLogicalType(),
			DataTypes.STRING().getLogicalType(),
			DataTypes.STRING().getLogicalType(),
			DataTypes.ANY(info).getLogicalType()
		},
		new TypeSerializer[]{
			IntSerializer.INSTANCE,
			LongSerializer.INSTANCE,
			StringSerializer.INSTANCE,
			StringSerializer.INSTANCE,
			genericSerializer
		});
	writer.writeRow(0, gRow, serializer);
	writer.complete();

	return row;
}
 
Example 7
Source File: NestedRowDataTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testNestedRowDataWithOneSegment() {
	BinaryRowData row = getBinaryRowData();
	GenericTypeInfo<MyObj> info = new GenericTypeInfo<>(MyObj.class);
	TypeSerializer<MyObj> genericSerializer = info.createSerializer(new ExecutionConfig());

	RowData nestedRow = row.getRow(0, 5);
	assertEquals(nestedRow.getInt(0), 1);
	assertEquals(nestedRow.getLong(1), 5L);
	assertEquals(nestedRow.getString(2), StringData.fromString("12345678"));
	assertTrue(nestedRow.isNullAt(3));
	assertEquals(new MyObj(15, 5), nestedRow.<MyObj>getRawValue(4).toObject(genericSerializer));
}
 
Example 8
Source File: NestedRowDataTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private BinaryRowData getBinaryRowData() {
	BinaryRowData row = new BinaryRowData(1);
	BinaryRowWriter writer = new BinaryRowWriter(row);

	GenericTypeInfo<MyObj> info = new GenericTypeInfo<>(MyObj.class);
	TypeSerializer<MyObj> genericSerializer = info.createSerializer(new ExecutionConfig());
	GenericRowData gRow = new GenericRowData(5);
	gRow.setField(0, 1);
	gRow.setField(1, 5L);
	gRow.setField(2, StringData.fromString("12345678"));
	gRow.setField(3, null);
	gRow.setField(4, RawValueData.fromObject(new MyObj(15, 5)));

	RowDataSerializer serializer = new RowDataSerializer(
		new LogicalType[]{
			DataTypes.INT().getLogicalType(),
			DataTypes.BIGINT().getLogicalType(),
			DataTypes.STRING().getLogicalType(),
			DataTypes.STRING().getLogicalType(),
			DataTypes.RAW(info).getLogicalType()
		},
		new TypeSerializer[]{
			IntSerializer.INSTANCE,
			LongSerializer.INSTANCE,
			StringDataSerializer.INSTANCE,
			StringDataSerializer.INSTANCE,
			new RawValueDataSerializer<>(genericSerializer)
		});
	writer.writeRow(0, gRow, serializer);
	writer.complete();

	return row;
}
 
Example 9
Source File: BinaryRowDataTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenericObject() throws Exception {

	GenericTypeInfo<MyObj> info = new GenericTypeInfo<>(MyObj.class);
	TypeSerializer<MyObj> genericSerializer = info.createSerializer(new ExecutionConfig());
	RawValueDataSerializer<MyObj> binarySerializer = new RawValueDataSerializer<>(genericSerializer);

	BinaryRowData row = new BinaryRowData(4);
	BinaryRowWriter writer = new BinaryRowWriter(row);
	writer.writeInt(0, 0);

	RawValueData<MyObj> myObj1 = RawValueData.fromObject(new MyObj(0, 1));
	writer.writeRawValue(1, myObj1, binarySerializer);
	RawValueData<MyObj> myObj2 = RawValueData.fromObject(new MyObj(123, 5.0));
	writer.writeRawValue(2, myObj2, binarySerializer);
	RawValueData<MyObj> myObj3 = RawValueData.fromObject(new MyObj(1, 1));
	writer.writeRawValue(3, myObj3, binarySerializer);
	writer.complete();

	assertTestGenericObjectRow(row, genericSerializer);

	// getBytes from var-length memorySegments.
	BinaryRowDataSerializer serializer = new BinaryRowDataSerializer(4);
	MemorySegment[] memorySegments = new MemorySegment[3];
	ArrayList<MemorySegment> memorySegmentList = new ArrayList<>();
	for (int i = 0; i < 3; i++) {
		memorySegments[i] = MemorySegmentFactory.wrap(new byte[64]);
		memorySegmentList.add(memorySegments[i]);
	}
	RandomAccessOutputView out = new RandomAccessOutputView(memorySegments, 64);
	serializer.serializeToPages(row, out);

	BinaryRowData mapRow = serializer.mapFromPages(new RandomAccessInputView(memorySegmentList, 64));
	assertTestGenericObjectRow(mapRow, genericSerializer);
}
 
Example 10
Source File: BinaryRowDataTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testZeroOutPaddingGeneric() {

	GenericTypeInfo<MyObj> info = new GenericTypeInfo<>(MyObj.class);
	TypeSerializer<MyObj> genericSerializer = info.createSerializer(new ExecutionConfig());

	Random random = new Random();
	byte[] bytes = new byte[1024];

	BinaryRowData row = new BinaryRowData(1);
	BinaryRowWriter writer = new BinaryRowWriter(row);

	// let's random the bytes
	writer.reset();
	random.nextBytes(bytes);
	writer.writeBinary(0, bytes);
	writer.reset();
	writer.writeRawValue(0, RawValueData.fromObject(new MyObj(0, 1)), new RawValueDataSerializer<>(genericSerializer));
	writer.complete();
	int hash1 = row.hashCode();

	writer.reset();
	random.nextBytes(bytes);
	writer.writeBinary(0, bytes);
	writer.reset();
	writer.writeRawValue(0, RawValueData.fromObject(new MyObj(0, 1)), new RawValueDataSerializer<>(genericSerializer));
	writer.complete();
	int hash2 = row.hashCode();

	assertEquals(hash1, hash2);
}