1 package org.wikimedia.eventutilities.flink.formats.json;
2
3 import java.time.Instant;
4 import java.util.List;
5
6 import javax.annotation.Nonnull;
7 import javax.annotation.ParametersAreNonnullByDefault;
8
9 import org.apache.flink.api.common.typeinfo.TypeInformation;
10 import org.apache.flink.api.common.typeinfo.Types;
11 import org.apache.flink.api.java.typeutils.RowTypeInfo;
12 import org.apache.flink.types.Row;
13 import org.wikimedia.eventutilities.core.event.types.SchemaConversions;
14
15
16 /**
17 * Implementation of {@link SchemaConversions}
18 * that can convert to Flink DataStream API TypeInformation.
19 * Used by JsonSchemaFlinkConverter.
20 *
21 * If you modify this class, please also augment {@link DataTypeSchemaConversions} accordingly.
22 * To handle proper conversion between Table API and DataStream,
23 * Flink needs the types consistently converted to the same underlying representations.
24 */
25 @ParametersAreNonnullByDefault
26 public class TypeInformationSchemaConversions implements SchemaConversions<TypeInformation<?>> {
27
28 /**
29 * @return
30 * {@link Types#VOID}
31 */
32 @Override
33 public TypeInformation<Void> typeNull() {
34 return Types.VOID;
35 }
36
37 /**
38 * @return
39 * {@link Types#BOOLEAN}
40 */
41 @Override
42 public TypeInformation<Boolean> typeBoolean() {
43 return Types.BOOLEAN;
44 }
45
46 /**
47 * @return
48 * {@link Types#STRING}
49 */
50 @Override
51 public TypeInformation<String> typeString() {
52 return Types.STRING;
53 }
54
55 /**
56 * @return
57 * {@link Types#DOUBLE}
58 */
59 @Override
60 public TypeInformation<Double> typeDecimal() {
61 return Types.DOUBLE;
62 }
63
64 /**
65 * @return
66 * {@link Types#LONG}
67 */
68 @Override
69 public TypeInformation<Long> typeInteger() {
70 return Types.LONG;
71 }
72
73 /**
74 * There are many ways to represent timestamps. We want to choose the representation
75 * that Flink chooses when converting from {@link org.apache.flink.table.data.TimestampData}
76 * to DataStream API types.
77 * In {@link DataTypeSchemaConversions#typeTimestamp},
78 * we use {@link org.apache.flink.table.api.DataTypes#TIMESTAMP_LTZ},
79 * which will end up using org.apache.flink.table.data.conversion.LocalZonedTimestampInstantConverter
80 * when doing conversions. So, we choose {@link java.time.Instant} here.
81 *
82 * @return
83 * {@link Types#INSTANT}
84 */
85 @Override
86 public TypeInformation<Instant> typeTimestamp() {
87 return Types.INSTANT;
88 }
89
90 /**
91 * {@code elementsAreNullable} is ignored, element values can always be null.
92 * See {@link Types#OBJECT_ARRAY}.
93 *
94 * @return
95 * {@link Types#OBJECT_ARRAY}(elementType)
96 */
97 @Override
98 public TypeInformation<?> typeArray(
99 TypeInformation<?> elementType,
100 boolean elementsAreNullable
101 ) {
102 return Types.OBJECT_ARRAY(elementType);
103 }
104
105 // TODO what is the generic return type here?
106 /**
107 * {@code valuesAreNullable} is ignored, an map values can always be null.
108 * See {@link Types#MAP}.
109 *
110 * @return
111 * {@link Types#MAP}(keyType, valueType)
112 */
113 @Override
114 public TypeInformation<?> typeMap(
115 TypeInformation<?> keyType,
116 TypeInformation<?> valueType,
117 boolean valuesAreNullable
118 ) {
119 return Types.MAP(typeString(), valueType);
120 }
121
122 /**
123 * Converts rowFields to {@link TypeInformation} of {@link Row}.
124 * {@link RowField} description and isNullable is ignored.
125 * Every field can be null independent of the field's type.
126 * See {@link Types#ROW_NAMED}
127 *
128 * @return
129 * {@link Types#ROW_NAMED} with RowFields names and types.
130 */
131 @Override
132 public TypeInformation<Row> typeRow(@Nonnull List<RowField<TypeInformation<?>>> rowFields) {
133 String[] fieldNames = new String[rowFields.size()];
134 TypeInformation<?>[] fieldTypes = new TypeInformation<?>[rowFields.size()];
135
136 // extract list of names and types from the list of rowFields.
137 for (int i = 0; i < rowFields.size(); i++) {
138 fieldNames[i] = rowFields.get(i).getName();
139 fieldTypes[i] = rowFields.get(i).getType();
140 }
141
142 return new RowTypeInfo(fieldTypes, fieldNames);
143 }
144
145 }