@Override public TypeInformation<Tuple> getProducedType() { return Types.TUPLE(Types.LONG, Types.STRING); }
names[i] = field.name(); return Types.ROW_NAMED(names, types); case ENUM: return Types.STRING; case ARRAY: return Types.OBJECT_ARRAY(convertToTypeInfo(schema.getElementType())); case MAP: return Types.MAP(Types.STRING, convertToTypeInfo(schema.getValueType())); case UNION: final Schema actualSchema; } else { return Types.GENERIC(Object.class); return Types.PRIMITIVE_ARRAY(Types.BYTE); case STRING: return Types.BIG_DEC; return Types.PRIMITIVE_ARRAY(Types.BYTE); case INT:
private static TypeInformation<Row> convertObject(String location, JsonNode node, JsonNode root) { // validate properties if (!node.has(PROPERTIES)) { return Types.ROW(); } if (!node.isObject()) { throw new IllegalArgumentException( "Invalid '" + PROPERTIES + "' property for object type in node: " + location); } final JsonNode props = node.get(PROPERTIES); final String[] names = new String[props.size()]; final TypeInformation<?>[] types = new TypeInformation[props.size()]; final Iterator<Map.Entry<String, JsonNode>> fieldIter = props.fields(); int i = 0; while (fieldIter.hasNext()) { final Map.Entry<String, JsonNode> subNode = fieldIter.next(); // set field name names[i] = subNode.getKey(); // set type types[i] = convertType(location + '/' + subNode.getKey(), subNode.getValue(), root); i++; } // validate that object does not contain additional properties if (node.has(ADDITIONAL_PROPERTIES) && node.get(ADDITIONAL_PROPERTIES).isBoolean() && node.get(ADDITIONAL_PROPERTIES).asBoolean()) { throw new IllegalArgumentException( "An object must not allow additional properties in node: " + location); } return Types.ROW_NAMED(names, types); }
private static TypeInformation<?> convertArray(String location, JsonNode node, JsonNode root) { // validate items if (!node.has(ITEMS)) { throw new IllegalArgumentException( "Arrays must specify an '" + ITEMS + "' property in node: " + location); } final JsonNode items = node.get(ITEMS); // list (translated to object array) if (items.isObject()) { final TypeInformation<?> elementType = convertType( location + '/' + ITEMS, items, root); // result type might either be ObjectArrayTypeInfo or BasicArrayTypeInfo for Strings return Types.OBJECT_ARRAY(elementType); } // tuple (translated to row) else if (items.isArray()) { final TypeInformation<?>[] types = convertTypes(location + '/' + ITEMS, items, root); // validate that array does not contain additional items if (node.has(ADDITIONAL_ITEMS) && node.get(ADDITIONAL_ITEMS).isBoolean() && node.get(ADDITIONAL_ITEMS).asBoolean()) { throw new IllegalArgumentException( "An array tuple must not allow additional items in node: " + location); } return Types.ROW(types); } throw new IllegalArgumentException( "Invalid type for '" + ITEMS + "' property in node: " + location); }
testReadAndWrite( "ROW<f0 DECIMAL, f1 TINYINT>", Types.ROW(Types.BIG_DEC, Types.BYTE)); Types.ROW_NAMED( new String[]{"hello", "world"}, Types.BIG_DEC, Types.BYTE)); Types.MAP(Types.STRING, Types.ROW(Types.BIG_DEC, Types.BYTE))); new MultisetTypeInfo<>(Types.ROW(Types.BIG_DEC, Types.BYTE))); Types.PRIMITIVE_ARRAY(Types.BYTE)); Types.OBJECT_ARRAY(TypeExtractor.createTypeInfo(TestPojo.class))); Types.ROW_NAMED( new String[] {"he \nllo", "world"}, Types.BIG_DEC, Types.BYTE), Types.ROW_NAMED( new String[] {"he`llo", "world"}, Types.BIG_DEC, Types.BYTE), Types.ROW_NAMED( new String[] {"he \nllo", "world"}, Types.BIG_DEC, Types.BYTE), Types.ROW_NAMED(
@Test public void testTypeInfoParser() { TypeInformation<Tuple3<String, Long, Object>> type1 = Types.TUPLE(Types.STRING, Types.LONG, Types.GENERIC(Object.class)); Assert.assertNotNull(type1); TypeInformation<Tuple4<String, Long, Object, InnerPojo>> type2 = Types.TUPLE(Types.STRING, Types.LONG, Types.GENERIC(Object.class), Types.GENERIC(InnerPojo.class)); Assert.assertNotNull(type2); }
/** * Converts a table schema into a (nested) type information describing a {@link Row}. */ public TypeInformation<Row> toRowType() { return Types.ROW_NAMED(fieldNames, fieldTypes); }
private static TypeInformation<?> convertStringEncoding(String location, JsonNode node) { if (!node.isTextual()) { throw new IllegalArgumentException("Invalid '" + CONTENT_ENCODING + "' property in node: " + location); } // "If the instance value is a string, this property defines that the string SHOULD // be interpreted as binary data and decoded using the encoding named by this property." switch (node.asText()) { case CONTENT_ENCODING_BASE64: return Types.PRIMITIVE_ARRAY(Types.BYTE); default: // we fail hard here: // this gives us the chance to support more encodings in the future without problems // of backwards compatibility throw new IllegalArgumentException("Invalid encoding '" + node.asText() + "' in node: " + location); } }
private TypeInformation<?> convertObjectArray() { nextToken(TokenType.BEGIN); nextToken(TokenType.LITERAL); final TypeInformation<?> elementTypeInfo = convertType(); nextToken(TokenType.END); return Types.OBJECT_ARRAY(elementTypeInfo); }
@Override public TypeInformation<Row> getReturnType() { return Types.ROW(Types.INT, Types.LONG, Types.STRING); }
private TypeInformation<?> convertAny() { nextToken(TokenType.BEGIN); // check if ANY(class) or ANY(class, serialized) if (isNextToken(2, TokenType.SEPARATOR)) { // any type information nextToken(TokenType.LITERAL); final String className = token().literal; nextToken(TokenType.SEPARATOR); nextToken(TokenType.LITERAL); final String serialized = token().literal; nextToken(TokenType.END); final Class<?> clazz = EncodingUtils.loadClass(className); final TypeInformation<?> typeInfo = EncodingUtils.decodeStringToObject(serialized, TypeInformation.class); if (!clazz.equals(typeInfo.getTypeClass())) { throw new ValidationException("Class '" + clazz + "' does no correspond to serialized data."); } return typeInfo; } else { // generic type information nextToken(TokenType.LITERAL); final String className = token().literal; nextToken(TokenType.END); final Class<?> clazz = EncodingUtils.loadClass(className); return Types.GENERIC(clazz); } }
private TypeInformation<?> convertMap() { nextToken(TokenType.BEGIN); nextToken(TokenType.LITERAL); final TypeInformation<?> keyTypeInfo = convertType(); nextToken(TokenType.SEPARATOR); nextToken(TokenType.LITERAL); final TypeInformation<?> valueTypeInfo = convertType(); nextToken(TokenType.END); return Types.MAP(keyTypeInfo, valueTypeInfo); }
private TypeInformation<?> convertPojo() { nextToken(TokenType.BEGIN); nextToken(TokenType.LITERAL); final String className = token().literal; nextToken(TokenType.END); final Class<?> clazz = EncodingUtils.loadClass(className); return Types.POJO(clazz); }
@Test public void testStreamTupleSerializerWithTuple() { TypeInformation<Tuple4> typeInfo = Types.GENERIC(Tuple4.class); StreamSchema<Tuple4> schema = new StreamSchema<>(typeInfo, "id", "timestamp", "name", "price"); assertEquals(Tuple4.class, schema.getTypeInfo().getTypeClass()); TypeInformation<Tuple2<String, Tuple4>> tuple2TypeInformation = Types.TUPLE(Types.STRING, schema.getTypeInfo()); assertEquals("Java Tuple2<String, GenericType<" + Tuple4.class.getName() + ">>", tuple2TypeInformation.toString()); }
private static TypeInformation<?> convertArray(String location, JsonNode node, JsonNode root) { // validate items if (!node.has(ITEMS)) { throw new IllegalArgumentException( "Arrays must specify an '" + ITEMS + "' property in node: " + location); } final JsonNode items = node.get(ITEMS); // list (translated to object array) if (items.isObject()) { final TypeInformation<?> elementType = convertType( location + '/' + ITEMS, items, root); // result type might either be ObjectArrayTypeInfo or BasicArrayTypeInfo for Strings return Types.OBJECT_ARRAY(elementType); } // tuple (translated to row) else if (items.isArray()) { final TypeInformation<?>[] types = convertTypes(location + '/' + ITEMS, items, root); // validate that array does not contain additional items if (node.has(ADDITIONAL_ITEMS) && node.get(ADDITIONAL_ITEMS).isBoolean() && node.get(ADDITIONAL_ITEMS).asBoolean()) { throw new IllegalArgumentException( "An array tuple must not allow additional items in node: " + location); } return Types.ROW(types); } throw new IllegalArgumentException( "Invalid type for '" + ITEMS + "' property in node: " + location); }
@Override public TypeInformation<Row> getOutputType() { return Types.ROW_NAMED(fieldNames, fieldTypes); }
private TypeInformation<?> convertPrimitiveArray() { nextToken(TokenType.BEGIN); nextToken(TokenType.LITERAL); final TypeInformation<?> elementTypeInfo = convertType(); nextToken(TokenType.END); return Types.PRIMITIVE_ARRAY(elementTypeInfo); }
private TypeInformation<?> convertObjectArray() { nextToken(TokenType.BEGIN); nextToken(TokenType.LITERAL); final TypeInformation<?> elementTypeInfo = convertType(); nextToken(TokenType.END); return Types.OBJECT_ARRAY(elementTypeInfo); }
@Override public TypeInformation<Row> getProducedType() { return Types.ROW(Types.INT, Types.LONG, Types.STRING); }
@Test public void testAvroStringAccess() throws Exception { ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); BatchTableEnvironment tEnv = TableEnvironment.getTableEnvironment(env, config()); Table t = tEnv.fromDataSet(testData(env)); Table result = t.select("name"); List<Utf8> results = tEnv.toDataSet(result, Types.GENERIC(Utf8.class)).collect(); String expected = "Charlie\n" + "Terminator\n" + "Whatever"; TestBaseUtils.compareResultAsText(results, expected); }