Java Code Examples for org.apache.flink.types.StringValue#writeString()

The following examples show how to use org.apache.flink.types.StringValue#writeString() . 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: StringSerializationTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
public static final void testSerialization(String[] values) throws IOException {
	ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
	DataOutputStream serializer = new DataOutputStream(baos);
	
	for (String value : values) {
		StringValue.writeString(value, serializer);
	}
	
	serializer.close();
	baos.close();
	
	ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
	DataInputStream deserializer = new DataInputStream(bais);
	
	int num = 0;
	while (deserializer.available() > 0) {
		String deser = StringValue.readString(deserializer);
		
		assertEquals("DeserializedString differs from original string.", values[num], deser);
		num++;
	}
	
	assertEquals("Wrong number of deserialized values", values.length, num);
}
 
Example 2
Source File: StringSerializationTest.java    From flink with Apache License 2.0 6 votes vote down vote up
public static final void testSerialization(String[] values) throws IOException {
	ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
	DataOutputStream serializer = new DataOutputStream(baos);
	
	for (String value : values) {
		StringValue.writeString(value, serializer);
	}
	
	serializer.close();
	baos.close();
	
	ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
	DataInputStream deserializer = new DataInputStream(bais);
	
	int num = 0;
	while (deserializer.available() > 0) {
		String deser = StringValue.readString(deserializer);
		
		assertEquals("DeserializedString differs from original string.", values[num], deser);
		num++;
	}
	
	assertEquals("Wrong number of deserialized values", values.length, num);
}
 
Example 3
Source File: StringArraySerializer.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(String[] record, DataOutputView target) throws IOException {
	if (record == null) {
		throw new IllegalArgumentException("The record must not be null.");
	}
	
	final int len = record.length;
	target.writeInt(len);
	for (int i = 0; i < len; i++) {
		StringValue.writeString(record[i], target);
	}
}
 
Example 4
Source File: NodeId.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(NodeId record, DataOutputView target) throws IOException {
	if (record != null) {
		target.writeByte(1);
		eventIdSerializer.serialize(record.eventId, target);
		StringValue.writeString(record.pageName, target);
	} else {
		target.writeByte(0);
	}
}
 
Example 5
Source File: NodeId.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(NodeId record, DataOutputView target) throws IOException {
	if (record != null) {
		target.writeByte(1);
		eventIdSerializer.serialize(record.eventId, target);
		StringValue.writeString(record.pageName, target);
	} else {
		target.writeByte(0);
	}
}
 
Example 6
Source File: StringSerializationTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public static final void testCopy(String[] values) throws IOException {
	ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
	DataOutputStream serializer = new DataOutputStream(baos);
	
	for (String value : values) {
		StringValue.writeString(value, serializer);
	}
	
	serializer.close();
	baos.close();
	
	ByteArrayInputStream sourceInput = new ByteArrayInputStream(baos.toByteArray());
	DataInputStream source = new DataInputStream(sourceInput);
	ByteArrayOutputStream targetOutput = new ByteArrayOutputStream(4096);
	DataOutputStream target = new DataOutputStream(targetOutput);
	
	for (int i = 0; i < values.length; i++) {
		StringValue.copyString(source, target);
	}
	
	ByteArrayInputStream validateInput = new ByteArrayInputStream(targetOutput.toByteArray());
	DataInputStream validate = new DataInputStream(validateInput);
	
	int num = 0;
	while (validate.available() > 0) {
		String deser = StringValue.readString(validate);
		
		assertEquals("DeserializedString differs from original string.", values[num], deser);
		num++;
	}
	
	assertEquals("Wrong number of deserialized values", values.length, num);
}
 
Example 7
Source File: NFAStateSerializer.java    From flink with Apache License 2.0 5 votes vote down vote up
private void serializeSingleComputationState(
		ComputationState computationState,
		DataOutputView target) throws IOException {

	StringValue.writeString(computationState.getCurrentStateName(), target);
	nodeIdSerializer.serialize(computationState.getPreviousBufferEntry(), target);
	versionSerializer.serialize(computationState.getVersion(), target);
	target.writeLong(computationState.getStartTimestamp());
	serializeStartEvent(computationState.getStartEventID(), target);
}
 
Example 8
Source File: NodeId.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(NodeId record, DataOutputView target) throws IOException {
	if (record != null) {
		target.writeByte(1);
		eventIdSerializer.serialize(record.eventId, target);
		StringValue.writeString(record.pageName, target);
	} else {
		target.writeByte(0);
	}
}
 
Example 9
Source File: StringPairSerializer.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(StringPair record, DataOutputView target) throws IOException {
	StringValue.writeString(record.getKey(), target);
	StringValue.writeString(record.getValue(), target);
}
 
Example 10
Source File: StringPairSerializer.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void copy(DataInputView source, DataOutputView target) throws IOException {
	StringValue.writeString(StringValue.readString(source), target);
	StringValue.writeString(StringValue.readString(source), target);
}
 
Example 11
Source File: Configuration.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void write(final DataOutputView out) throws IOException {
	synchronized (this.confData) {
		out.writeInt(this.confData.size());

		for (Map.Entry<String, Object> entry : this.confData.entrySet()) {
			String key = entry.getKey();
			Object val = entry.getValue();

			StringValue.writeString(key, out);
			Class<?> clazz = val.getClass();

			if (clazz == String.class) {
				out.write(TYPE_STRING);
				StringValue.writeString((String) val, out);
			}
			else if (clazz == Integer.class) {
				out.write(TYPE_INT);
				out.writeInt((Integer) val);
			}
			else if (clazz == Long.class) {
				out.write(TYPE_LONG);
				out.writeLong((Long) val);
			}
			else if (clazz == Float.class) {
				out.write(TYPE_FLOAT);
				out.writeFloat((Float) val);
			}
			else if (clazz == Double.class) {
				out.write(TYPE_DOUBLE);
				out.writeDouble((Double) val);
			}
			else if (clazz == byte[].class) {
				out.write(TYPE_BYTES);
				byte[] bytes = (byte[]) val;
				out.writeInt(bytes.length);
				out.write(bytes);
			}
			else if (clazz == Boolean.class) {
				out.write(TYPE_BOOLEAN);
				out.writeBoolean((Boolean) val);
			}
			else {
				throw new IllegalArgumentException("Unrecognized type");
			}
		}
	}
}
 
Example 12
Source File: StringPairSerializer.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void copy(DataInputView source, DataOutputView target) throws IOException {
	StringValue.writeString(StringValue.readString(source), target);
	StringValue.writeString(StringValue.readString(source), target);
}
 
Example 13
Source File: StringPairSerializer.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(StringPair record, DataOutputView target) throws IOException {
	StringValue.writeString(record.getKey(), target);
	StringValue.writeString(record.getValue(), target);
}
 
Example 14
Source File: StringPairSerializer.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(StringPair record, DataOutputView target) throws IOException {
	StringValue.writeString(record.getKey(), target);
	StringValue.writeString(record.getValue(), target);
}
 
Example 15
Source File: Configuration.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void write(final DataOutputView out) throws IOException {
	synchronized (this.confData) {
		out.writeInt(this.confData.size());

		for (Map.Entry<String, Object> entry : this.confData.entrySet()) {
			String key = entry.getKey();
			Object val = entry.getValue();

			StringValue.writeString(key, out);
			Class<?> clazz = val.getClass();

			if (clazz == String.class) {
				out.write(TYPE_STRING);
				StringValue.writeString((String) val, out);
			}
			else if (clazz == Integer.class) {
				out.write(TYPE_INT);
				out.writeInt((Integer) val);
			}
			else if (clazz == Long.class) {
				out.write(TYPE_LONG);
				out.writeLong((Long) val);
			}
			else if (clazz == Float.class) {
				out.write(TYPE_FLOAT);
				out.writeFloat((Float) val);
			}
			else if (clazz == Double.class) {
				out.write(TYPE_DOUBLE);
				out.writeDouble((Double) val);
			}
			else if (clazz == byte[].class) {
				out.write(TYPE_BYTES);
				byte[] bytes = (byte[]) val;
				out.writeInt(bytes.length);
				out.write(bytes);
			}
			else if (clazz == Boolean.class) {
				out.write(TYPE_BOOLEAN);
				out.writeBoolean((Boolean) val);
			}
			else {
				throw new IllegalArgumentException("Unrecognized type. This method is deprecated and might not work" +
					" for all supported types.");
			}
		}
	}
}
 
Example 16
Source File: Configuration.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public void write(final DataOutputView out) throws IOException {
	synchronized (this.confData) {
		out.writeInt(this.confData.size());

		for (Map.Entry<String, Object> entry : this.confData.entrySet()) {
			String key = entry.getKey();
			Object val = entry.getValue();

			StringValue.writeString(key, out);
			Class<?> clazz = val.getClass();

			if (clazz == String.class) {
				out.write(TYPE_STRING);
				StringValue.writeString((String) val, out);
			}
			else if (clazz == Integer.class) {
				out.write(TYPE_INT);
				out.writeInt((Integer) val);
			}
			else if (clazz == Long.class) {
				out.write(TYPE_LONG);
				out.writeLong((Long) val);
			}
			else if (clazz == Float.class) {
				out.write(TYPE_FLOAT);
				out.writeFloat((Float) val);
			}
			else if (clazz == Double.class) {
				out.write(TYPE_DOUBLE);
				out.writeDouble((Double) val);
			}
			else if (clazz == byte[].class) {
				out.write(TYPE_BYTES);
				byte[] bytes = (byte[]) val;
				out.writeInt(bytes.length);
				out.write(bytes);
			}
			else if (clazz == Boolean.class) {
				out.write(TYPE_BOOLEAN);
				out.writeBoolean((Boolean) val);
			}
			else {
				throw new IllegalArgumentException("Unrecognized type");
			}
		}
	}
}
 
Example 17
Source File: StringUtils.java    From flink-learning with Apache License 2.0 2 votes vote down vote up
/**
 * Writes a String to the given output.
 * The written string can be read with {@link #readString(DataInputView)}.
 *
 * @param str The string to write
 * @param out The output to write to
 *
 * @throws IOException Thrown, if the writing or the serialization fails.
 */
public static void writeString(@Nonnull String str, DataOutputView out) throws IOException {
	checkNotNull(str);
	StringValue.writeString(str, out);
}
 
Example 18
Source File: StringUtils.java    From flink-learning with Apache License 2.0 2 votes vote down vote up
/**
 * Writes a String to the given output.
 * The written string can be read with {@link #readString(DataInputView)}.
 *
 * @param str The string to write
 * @param out The output to write to
 *
 * @throws IOException Thrown, if the writing or the serialization fails.
 */
public static void writeString(@Nonnull String str, DataOutputView out) throws IOException {
	checkNotNull(str);
	StringValue.writeString(str, out);
}
 
Example 19
Source File: StringUtils.java    From flink with Apache License 2.0 2 votes vote down vote up
/**
 * Writes a String to the given output.
 * The written string can be read with {@link #readString(DataInputView)}.
 *
 * @param str The string to write
 * @param out The output to write to
 *
 * @throws IOException Thrown, if the writing or the serialization fails.
 */
public static void writeString(@Nonnull String str, DataOutputView out) throws IOException {
	checkNotNull(str);
	StringValue.writeString(str, out);
}
 
Example 20
Source File: StringUtils.java    From Flink-CEPplus with Apache License 2.0 2 votes vote down vote up
/**
 * Writes a String to the given output.
 * The written string can be read with {@link #readString(DataInputView)}.
 *
 * @param str The string to write
 * @param out The output to write to
 *
 * @throws IOException Thrown, if the writing or the serialization fails.
 */
public static void writeString(@Nonnull String str, DataOutputView out) throws IOException {
	checkNotNull(str);
	StringValue.writeString(str, out);
}