Data Types
If the type has not been declared, data would be serialized or deserialized using Pickle. For example, the program below specifies no data types.
- Passing Python records to Java operations.
- Improve serialization and deserialization performance.
Since Java operators or functions can not identify Python data, types need to be provided to help to convert Python types to Java types for processing. For example, types need to be provided if you want to output data using the StreamingFileSink which is implemented in Java.
from pyflink.common.typeinfo import Types
from pyflink.datastream.connectors import StreamingFileSink
def streaming_file_sink():
env = StreamExecutionEnvironment.get_execution_environment()
env.set_parallelism(1)
env.from_collection(collection=[(1, 'aaa'), (2, 'bbb')]) \
.map(lambda record: (record[0]+1, record[1].upper()),
.add_sink(StreamingFileSink
.for_row_format('/tmp/output', Encoder.simple_string_encoder())
.build())
env.execute()
if __name__ == '__main__':
Improve serialization and deserialization performance
Supported Data Types
You can use pyflink.common.typeinfo.Types
to define types in Python DataStream API. The table below shows the types supported now and how to define them:
PyFlink Array Type | Python Type | Java Type |
---|---|---|
Types.PRIMITIVE_ARRAY(Types.BYTE()) | bytes | byte[] |
Types.PRIMITIVE_ARRAY(Types.BOOLEAN()) | list of bool | boolean[] |
list of int | short[] | |
Types.PRIMITIVE_ARRAY(Types.INT()) | list of int | int[] |
Types.PRIMITIVE_ARRAY(Types.LONG()) | list of int | long[] |
Types.PRIMITIVE_ARRAY(Types.FLOAT()) | list of float | float[] |
Types.PRIMITIVE_ARRAY(Types.DOUBLE()) | list of float | double[] |
Types.PRIMITIVE_ARRAY(Types.CHAR()) | list of str | char[] |
Types.BASIC_ARRAY(Types.BYTE()) | list of int | java.lang.Byte[] |
Types.BASIC_ARRAY(Types.BOOLEAN()) | list of bool | java.lang.Boolean[] |
Types.BASIC_ARRAY(Types.SHORT()) | list of int | java.lang.Short[] |
Types.BASIC_ARRAY(Types.INT()) | list of int | java.lang.Integer[] |
Types.BASIC_ARRAY(Types.LONG()) | list of int | java.lang.Long[] |
Types.BASIC_ARRAY(Types.FLOAT()) | list of float | java.lang.Float[] |
Types.BASIC_ARRAY(Types.DOUBLE()) | list of float | java.lang.Double[] |
Types.BASIC_ARRAY(Types.CHAR()) | list of str | java.lang.Character[] |
Types.BASIC_ARRAY(Types.STRING()) | list of str | java.lang.String[] |
Types.OBJECT_ARRAY() | Array |