Java Code Examples for org.apache.flink.api.common.typeinfo.PrimitiveArrayTypeInfo

The following examples show how to use org.apache.flink.api.common.typeinfo.PrimitiveArrayTypeInfo. These examples are extracted from open source projects. 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 Project: flink   Author: apache   File: DataStreamTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testPOJOWithNestedArrayNoHashCodeKeyRejection() {
	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

	DataStream<POJOWithHashCode> input = env.fromElements(
			new POJOWithHashCode(new int[] {1, 2}));

	TypeInformation<?> expectedTypeInfo = new TupleTypeInfo<Tuple1<int[]>>(
			PrimitiveArrayTypeInfo.INT_PRIMITIVE_ARRAY_TYPE_INFO);

	// adjust the rule
	expectedException.expect(InvalidProgramException.class);
	expectedException.expectMessage(new StringStartsWith("Type " + expectedTypeInfo + " cannot be used as key."));

	input.keyBy("id");
}
 
Example #2
Source Project: Flink-CEPplus   Author: ljygz   File: CollectionDataSets.java    License: Apache License 2.0 6 votes vote down vote up
public static DataSet<Tuple2<byte[], Integer>> getTuple2WithByteArrayDataSet(ExecutionEnvironment env) {
	List<Tuple2<byte[], Integer>> data = new ArrayList<>();
	data.add(new Tuple2<>(new byte[]{0, 4}, 1));
	data.add(new Tuple2<>(new byte[]{2, 0}, 1));
	data.add(new Tuple2<>(new byte[]{2, 0, 4}, 4));
	data.add(new Tuple2<>(new byte[]{2, 1}, 3));
	data.add(new Tuple2<>(new byte[]{0}, 0));
	data.add(new Tuple2<>(new byte[]{2, 0}, 1));

	TupleTypeInfo<Tuple2<byte[], Integer>> type = new TupleTypeInfo<>(
			PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO,
			BasicTypeInfo.INT_TYPE_INFO
	);

	return env.fromCollection(data, type);
}
 
Example #3
Source Project: Flink-CEPplus   Author: ljygz   File: TypeExtractorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testTupleWithPrimitiveArray() {
	RichMapFunction<Integer, Tuple9<int[],double[],long[],byte[],char[],float[],short[], boolean[], String[]>> function = new RichMapFunction<Integer, Tuple9<int[],double[],long[],byte[],char[],float[],short[], boolean[], String[]>>() {
		private static final long serialVersionUID = 1L;

		@Override
		public Tuple9<int[], double[], long[], byte[], char[], float[], short[], boolean[], String[]> map(Integer value) throws Exception {
			return null;
		}
	};
	
	TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, BasicTypeInfo.INT_TYPE_INFO);
	TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti;
	Assert.assertEquals(PrimitiveArrayTypeInfo.INT_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(0));
	Assert.assertEquals(PrimitiveArrayTypeInfo.DOUBLE_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(1));
	Assert.assertEquals(PrimitiveArrayTypeInfo.LONG_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(2));
	Assert.assertEquals(PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(3));
	Assert.assertEquals(PrimitiveArrayTypeInfo.CHAR_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(4));
	Assert.assertEquals(PrimitiveArrayTypeInfo.FLOAT_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(5));
	Assert.assertEquals(PrimitiveArrayTypeInfo.SHORT_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(6));
	Assert.assertEquals(PrimitiveArrayTypeInfo.BOOLEAN_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(7));
	Assert.assertEquals(BasicArrayTypeInfo.STRING_ARRAY_TYPE_INFO, tti.getTypeAt(8));
}
 
Example #4
Source Project: flink   Author: apache   File: KeySelectorUtil.java    License: Apache License 2.0 6 votes vote down vote up
public static <X> ArrayKeySelector<X> getSelectorForArray(int[] positions, TypeInformation<X> typeInfo) {
	if (positions == null || positions.length == 0 || positions.length > Tuple.MAX_ARITY) {
		throw new IllegalArgumentException("Array keys must have between 1 and " + Tuple.MAX_ARITY + " fields.");
	}

	TypeInformation<?> componentType;

	if (typeInfo instanceof BasicArrayTypeInfo) {
		BasicArrayTypeInfo<X, ?>  arrayInfo = (BasicArrayTypeInfo<X, ?>) typeInfo;
		componentType = arrayInfo.getComponentInfo();
	}
	else if (typeInfo instanceof PrimitiveArrayTypeInfo) {
		PrimitiveArrayTypeInfo<X> arrayType = (PrimitiveArrayTypeInfo<X>) typeInfo;
		componentType = arrayType.getComponentType();
	}
	else {
		throw new IllegalArgumentException("This method only supports arrays of primitives and boxed primitives.");
	}

	TypeInformation<?>[] primitiveInfos = new TypeInformation<?>[positions.length];
	Arrays.fill(primitiveInfos, componentType);

	return new ArrayKeySelector<>(positions, new TupleTypeInfo<>(primitiveInfos));
}
 
Example #5
Source Project: flink   Author: apache   File: TypeExtractorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testTupleWithPrimitiveArray() {
	RichMapFunction<Integer, Tuple9<int[],double[],long[],byte[],char[],float[],short[], boolean[], String[]>> function = new RichMapFunction<Integer, Tuple9<int[],double[],long[],byte[],char[],float[],short[], boolean[], String[]>>() {
		private static final long serialVersionUID = 1L;

		@Override
		public Tuple9<int[], double[], long[], byte[], char[], float[], short[], boolean[], String[]> map(Integer value) throws Exception {
			return null;
		}
	};
	
	TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, BasicTypeInfo.INT_TYPE_INFO);
	TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti;
	Assert.assertEquals(PrimitiveArrayTypeInfo.INT_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(0));
	Assert.assertEquals(PrimitiveArrayTypeInfo.DOUBLE_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(1));
	Assert.assertEquals(PrimitiveArrayTypeInfo.LONG_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(2));
	Assert.assertEquals(PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(3));
	Assert.assertEquals(PrimitiveArrayTypeInfo.CHAR_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(4));
	Assert.assertEquals(PrimitiveArrayTypeInfo.FLOAT_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(5));
	Assert.assertEquals(PrimitiveArrayTypeInfo.SHORT_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(6));
	Assert.assertEquals(PrimitiveArrayTypeInfo.BOOLEAN_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(7));
	Assert.assertEquals(BasicArrayTypeInfo.STRING_ARRAY_TYPE_INFO, tti.getTypeAt(8));
}
 
Example #6
Source Project: Flink-CEPplus   Author: ljygz   File: DataStreamTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testPrimitiveArrayKeyRejection() {

	KeySelector<Tuple2<Integer[], String>, int[]> keySelector =
			new KeySelector<Tuple2<Integer[], String>, int[]>() {

		@Override
		public int[] getKey(Tuple2<Integer[], String> value) throws Exception {
			int[] ks = new int[value.f0.length];
			for (int i = 0; i < ks.length; i++) {
				ks[i] = value.f0[i];
			}
			return ks;
		}
	};

	testKeyRejection(keySelector, PrimitiveArrayTypeInfo.INT_PRIMITIVE_ARRAY_TYPE_INFO);
}
 
Example #7
Source Project: Flink-CEPplus   Author: ljygz   File: DataStreamTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testPOJOWithNestedArrayNoHashCodeKeyRejection() {
	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

	DataStream<POJOWithHashCode> input = env.fromElements(
			new POJOWithHashCode(new int[] {1, 2}));

	TypeInformation<?> expectedTypeInfo = new TupleTypeInfo<Tuple1<int[]>>(
			PrimitiveArrayTypeInfo.INT_PRIMITIVE_ARRAY_TYPE_INFO);

	// adjust the rule
	expectedException.expect(InvalidProgramException.class);
	expectedException.expectMessage(new StringStartsWith("Type " + expectedTypeInfo + " cannot be used as key."));

	input.keyBy("id");
}
 
Example #8
Source Project: flink   Author: flink-tpc-ds   File: Types.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Generates type information for an array consisting of Java primitive elements. The elements do
 * not support null values.
 *
 * @param elementType type of the array elements; e.g. Types.INT()
 */
public static TypeInformation<?> PRIMITIVE_ARRAY(TypeInformation<?> elementType) {
	if (elementType.equals(BOOLEAN())) {
		return PrimitiveArrayTypeInfo.BOOLEAN_PRIMITIVE_ARRAY_TYPE_INFO;
	} else if (elementType.equals(BYTE())) {
		return PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO;
	} else if (elementType.equals(SHORT())) {
		return PrimitiveArrayTypeInfo.SHORT_PRIMITIVE_ARRAY_TYPE_INFO;
	} else if (elementType.equals(INT())) {
		return PrimitiveArrayTypeInfo.INT_PRIMITIVE_ARRAY_TYPE_INFO;
	} else if (elementType.equals(LONG())) {
		return PrimitiveArrayTypeInfo.LONG_PRIMITIVE_ARRAY_TYPE_INFO;
	} else if (elementType.equals(FLOAT())) {
		return PrimitiveArrayTypeInfo.FLOAT_PRIMITIVE_ARRAY_TYPE_INFO;
	} else if (elementType.equals(DOUBLE())) {
		return PrimitiveArrayTypeInfo.DOUBLE_PRIMITIVE_ARRAY_TYPE_INFO;
	}
	throw new TableException(
		String.format(
			"%s cannot be an element of a primitive array. Only Java primitive types are supported.",
			elementType));
}
 
Example #9
Source Project: flink   Author: flink-tpc-ds   File: CollectionDataSets.java    License: Apache License 2.0 6 votes vote down vote up
public static DataSet<Tuple2<byte[], Integer>> getTuple2WithByteArrayDataSet(ExecutionEnvironment env) {
	List<Tuple2<byte[], Integer>> data = new ArrayList<>();
	data.add(new Tuple2<>(new byte[]{0, 4}, 1));
	data.add(new Tuple2<>(new byte[]{2, 0}, 1));
	data.add(new Tuple2<>(new byte[]{2, 0, 4}, 4));
	data.add(new Tuple2<>(new byte[]{2, 1}, 3));
	data.add(new Tuple2<>(new byte[]{0}, 0));
	data.add(new Tuple2<>(new byte[]{2, 0}, 1));

	TupleTypeInfo<Tuple2<byte[], Integer>> type = new TupleTypeInfo<>(
			PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO,
			BasicTypeInfo.INT_TYPE_INFO
	);

	return env.fromCollection(data, type);
}
 
Example #10
Source Project: flink   Author: flink-tpc-ds   File: TypeExtractorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testTupleWithPrimitiveArray() {
	RichMapFunction<Integer, Tuple9<int[],double[],long[],byte[],char[],float[],short[], boolean[], String[]>> function = new RichMapFunction<Integer, Tuple9<int[],double[],long[],byte[],char[],float[],short[], boolean[], String[]>>() {
		private static final long serialVersionUID = 1L;

		@Override
		public Tuple9<int[], double[], long[], byte[], char[], float[], short[], boolean[], String[]> map(Integer value) throws Exception {
			return null;
		}
	};
	
	TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, BasicTypeInfo.INT_TYPE_INFO);
	TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti;
	Assert.assertEquals(PrimitiveArrayTypeInfo.INT_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(0));
	Assert.assertEquals(PrimitiveArrayTypeInfo.DOUBLE_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(1));
	Assert.assertEquals(PrimitiveArrayTypeInfo.LONG_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(2));
	Assert.assertEquals(PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(3));
	Assert.assertEquals(PrimitiveArrayTypeInfo.CHAR_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(4));
	Assert.assertEquals(PrimitiveArrayTypeInfo.FLOAT_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(5));
	Assert.assertEquals(PrimitiveArrayTypeInfo.SHORT_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(6));
	Assert.assertEquals(PrimitiveArrayTypeInfo.BOOLEAN_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(7));
	Assert.assertEquals(BasicArrayTypeInfo.STRING_ARRAY_TYPE_INFO, tti.getTypeAt(8));
}
 
Example #11
Source Project: flink   Author: flink-tpc-ds   File: KeySelectorUtil.java    License: Apache License 2.0 6 votes vote down vote up
public static <X> ArrayKeySelector<X> getSelectorForArray(int[] positions, TypeInformation<X> typeInfo) {
	if (positions == null || positions.length == 0 || positions.length > Tuple.MAX_ARITY) {
		throw new IllegalArgumentException("Array keys must have between 1 and " + Tuple.MAX_ARITY + " fields.");
	}

	TypeInformation<?> componentType;

	if (typeInfo instanceof BasicArrayTypeInfo) {
		BasicArrayTypeInfo<X, ?>  arrayInfo = (BasicArrayTypeInfo<X, ?>) typeInfo;
		componentType = arrayInfo.getComponentInfo();
	}
	else if (typeInfo instanceof PrimitiveArrayTypeInfo) {
		PrimitiveArrayTypeInfo<X> arrayType = (PrimitiveArrayTypeInfo<X>) typeInfo;
		componentType = arrayType.getComponentType();
	}
	else {
		throw new IllegalArgumentException("This method only supports arrays of primitives and boxed primitives.");
	}

	TypeInformation<?>[] primitiveInfos = new TypeInformation<?>[positions.length];
	Arrays.fill(primitiveInfos, componentType);

	return new ArrayKeySelector<>(positions, new TupleTypeInfo<>(primitiveInfos));
}
 
Example #12
Source Project: flink   Author: flink-tpc-ds   File: ArrayKeySelectorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testPrimitiveArrays() {
	try {
		int[] array1 = { 1, 2, 3, 4, 5 };
		int[] array2 = { -5, -4, -3, -2, -1, 0 };

		KeySelectorUtil.ArrayKeySelector<int[]> singleFieldSelector =
				KeySelectorUtil.getSelectorForArray(new int[] {1}, PrimitiveArrayTypeInfo.INT_PRIMITIVE_ARRAY_TYPE_INFO);

		assertEquals(new Tuple1<>(2), singleFieldSelector.getKey(array1));
		assertEquals(new Tuple1<>(-4), singleFieldSelector.getKey(array2));

		KeySelectorUtil.ArrayKeySelector<int[]> twoFieldsSelector =
				KeySelectorUtil.getSelectorForArray(new int[] {3, 0}, PrimitiveArrayTypeInfo.INT_PRIMITIVE_ARRAY_TYPE_INFO);

		assertEquals(new Tuple2<>(4, 1), twoFieldsSelector.getKey(array1));
		assertEquals(new Tuple2<>(-2, -5), twoFieldsSelector.getKey(array2));

	}
	catch (Exception e) {
		e.printStackTrace();
		fail(e.getMessage());
	}
}
 
Example #13
Source Project: flink   Author: apache   File: CollectionDataSets.java    License: Apache License 2.0 6 votes vote down vote up
public static DataSet<Tuple2<byte[], Integer>> getTuple2WithByteArrayDataSet(ExecutionEnvironment env) {
	List<Tuple2<byte[], Integer>> data = new ArrayList<>();
	data.add(new Tuple2<>(new byte[]{0, 4}, 1));
	data.add(new Tuple2<>(new byte[]{2, 0}, 1));
	data.add(new Tuple2<>(new byte[]{2, 0, 4}, 4));
	data.add(new Tuple2<>(new byte[]{2, 1}, 3));
	data.add(new Tuple2<>(new byte[]{0}, 0));
	data.add(new Tuple2<>(new byte[]{2, 0}, 1));

	TupleTypeInfo<Tuple2<byte[], Integer>> type = new TupleTypeInfo<>(
			PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO,
			BasicTypeInfo.INT_TYPE_INFO
	);

	return env.fromCollection(data, type);
}
 
Example #14
Source Project: flink   Author: apache   File: DataStreamTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testPrimitiveArrayKeyRejection() {

	KeySelector<Tuple2<Integer[], String>, int[]> keySelector =
			new KeySelector<Tuple2<Integer[], String>, int[]>() {

		@Override
		public int[] getKey(Tuple2<Integer[], String> value) throws Exception {
			int[] ks = new int[value.f0.length];
			for (int i = 0; i < ks.length; i++) {
				ks[i] = value.f0[i];
			}
			return ks;
		}
	};

	assertArrayKeyRejection(keySelector, PrimitiveArrayTypeInfo.INT_PRIMITIVE_ARRAY_TYPE_INFO);
}
 
Example #15
Source Project: Flink-CEPplus   Author: ljygz   File: HCatInputFormatBase.java    License: Apache License 2.0 5 votes vote down vote up
private TypeInformation getFieldType(HCatFieldSchema fieldSchema) {

		switch(fieldSchema.getType()) {
			case INT:
				return BasicTypeInfo.INT_TYPE_INFO;
			case TINYINT:
				return BasicTypeInfo.BYTE_TYPE_INFO;
			case SMALLINT:
				return BasicTypeInfo.SHORT_TYPE_INFO;
			case BIGINT:
				return BasicTypeInfo.LONG_TYPE_INFO;
			case BOOLEAN:
				return BasicTypeInfo.BOOLEAN_TYPE_INFO;
			case FLOAT:
				return BasicTypeInfo.FLOAT_TYPE_INFO;
			case DOUBLE:
				return BasicTypeInfo.DOUBLE_TYPE_INFO;
			case STRING:
				return BasicTypeInfo.STRING_TYPE_INFO;
			case BINARY:
				return PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO;
			case ARRAY:
				return new GenericTypeInfo(List.class);
			case MAP:
				return new GenericTypeInfo(Map.class);
			case STRUCT:
				return new GenericTypeInfo(List.class);
			default:
				throw new IllegalArgumentException("Unknown data type \"" + fieldSchema.getType() + "\" encountered.");
		}
	}
 
Example #16
Source Project: Flink-CEPplus   Author: ljygz   File: JDBCTypeUtil.java    License: Apache License 2.0 5 votes vote down vote up
static int typeInformationToSqlType(TypeInformation<?> type) {

		if (TYPE_MAPPING.containsKey(type)) {
			return TYPE_MAPPING.get(type);
		} else if (type instanceof ObjectArrayTypeInfo || type instanceof PrimitiveArrayTypeInfo) {
			return Types.ARRAY;
		} else {
			throw new IllegalArgumentException("Unsupported type: " + type);
		}
	}
 
Example #17
Source Project: Flink-CEPplus   Author: ljygz   File: OrcTableSourceTest.java    License: Apache License 2.0 5 votes vote down vote up
private TypeInformation[] getNestedFieldTypes() {
	return new TypeInformation[]{
		Types.BOOLEAN, Types.BYTE, Types.SHORT, Types.INT, Types.LONG, Types.FLOAT, Types.DOUBLE,
		PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO, Types.STRING,
		Types.ROW_NAMED(
			new String[]{"list"},
			ObjectArrayTypeInfo.getInfoFor(
				Types.ROW_NAMED(
					new String[]{"int1", "string1"},
					Types.INT, Types.STRING
				)
			)
		),
		ObjectArrayTypeInfo.getInfoFor(
			Types.ROW_NAMED(
				new String[]{"int1", "string1"},
				Types.INT, Types.STRING
			)
		),
		new MapTypeInfo<>(
			Types.STRING,
			Types.ROW_NAMED(
				new String[]{"int1", "string1"},
				Types.INT, Types.STRING
			)
		)
	};
}
 
Example #18
Source Project: Flink-CEPplus   Author: ljygz   File: OrcRowInputFormatTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testProducedType() throws IOException {
	rowOrcInputFormat =
		new OrcRowInputFormat(getPath(TEST_FILE_NESTED), TEST_SCHEMA_NESTED, new Configuration());

	assertTrue(rowOrcInputFormat.getProducedType() instanceof RowTypeInfo);
	RowTypeInfo producedType = (RowTypeInfo) rowOrcInputFormat.getProducedType();

	assertArrayEquals(
		new TypeInformation[]{
			// primitives
			Types.BOOLEAN, Types.BYTE, Types.SHORT, Types.INT, Types.LONG, Types.FLOAT, Types.DOUBLE,
			// binary
			PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO,
			// string
			Types.STRING,
			// struct
			Types.ROW_NAMED(
				new String[]{"list"},
				ObjectArrayTypeInfo.getInfoFor(
					Types.ROW_NAMED(new String[]{"int1", "string1"}, Types.INT, Types.STRING))),
			// list
			ObjectArrayTypeInfo.getInfoFor(
				Types.ROW_NAMED(new String[]{"int1", "string1"}, Types.INT, Types.STRING)),
			// map
			new MapTypeInfo<>(Types.STRING, Types.ROW_NAMED(new String[]{"int1", "string1"}, Types.INT, Types.STRING))
		},
		producedType.getFieldTypes());
	assertArrayEquals(
		new String[]{"boolean1", "byte1", "short1", "int1", "long1", "float1", "double1", "bytes1", "string1", "middle", "list", "map"},
		producedType.getFieldNames());
}
 
Example #19
Source Project: Flink-CEPplus   Author: ljygz   File: CsvRowSchemaConverter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Convert {@link TypeInformation} to {@link CsvSchema.ColumnType} based on Jackson's categories.
 */
private static CsvSchema.ColumnType convertType(String fieldName, TypeInformation<?> info) {
	if (STRING_TYPES.contains(info)) {
		return CsvSchema.ColumnType.STRING;
	} else if (NUMBER_TYPES.contains(info)) {
		return CsvSchema.ColumnType.NUMBER;
	} else if (BOOLEAN_TYPES.contains(info)) {
		return CsvSchema.ColumnType.BOOLEAN;
	} else if (info instanceof ObjectArrayTypeInfo) {
		validateNestedField(fieldName, ((ObjectArrayTypeInfo) info).getComponentInfo());
		return CsvSchema.ColumnType.ARRAY;
	} else if (info instanceof BasicArrayTypeInfo) {
		validateNestedField(fieldName, ((BasicArrayTypeInfo) info).getComponentInfo());
		return CsvSchema.ColumnType.ARRAY;
	} else if (info instanceof RowTypeInfo) {
		final TypeInformation<?>[] types = ((RowTypeInfo) info).getFieldTypes();
		for (TypeInformation<?> type : types) {
			validateNestedField(fieldName, type);
		}
		return CsvSchema.ColumnType.ARRAY;
	} else if (info instanceof PrimitiveArrayTypeInfo &&
			((PrimitiveArrayTypeInfo) info).getComponentType() == Types.BYTE) {
		return CsvSchema.ColumnType.STRING;
	} else {
		throw new IllegalArgumentException(
			"Unsupported type information '" + info.toString() + "' for field '" + fieldName + "'.");
	}
}
 
Example #20
Source Project: flink   Author: apache   File: OrcRowInputFormatTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testProducedTypeWithProjection() throws IOException {
	rowOrcInputFormat =
		new OrcRowInputFormat(getPath(TEST_FILE_NESTED), TEST_SCHEMA_NESTED, new Configuration());

	rowOrcInputFormat.selectFields(9, 3, 7, 10);

	assertTrue(rowOrcInputFormat.getProducedType() instanceof RowTypeInfo);
	RowTypeInfo producedType = (RowTypeInfo) rowOrcInputFormat.getProducedType();

	assertArrayEquals(
		new TypeInformation[]{
			// struct
			Types.ROW_NAMED(
				new String[]{"list"},
				ObjectArrayTypeInfo.getInfoFor(
					Types.ROW_NAMED(new String[]{"int1", "string1"}, Types.INT, Types.STRING))),
			// int
			Types.INT,
			// binary
			PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO,
			// list
			ObjectArrayTypeInfo.getInfoFor(
				Types.ROW_NAMED(new String[]{"int1", "string1"}, Types.INT, Types.STRING))
		},
		producedType.getFieldTypes());
	assertArrayEquals(
		new String[]{"middle", "int1", "bytes1", "list"},
		producedType.getFieldNames());
}
 
Example #21
Source Project: flink   Author: apache   File: OrcRowInputFormatTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testProducedType() throws IOException {
	rowOrcInputFormat =
		new OrcRowInputFormat(getPath(TEST_FILE_NESTED), TEST_SCHEMA_NESTED, new Configuration());

	assertTrue(rowOrcInputFormat.getProducedType() instanceof RowTypeInfo);
	RowTypeInfo producedType = (RowTypeInfo) rowOrcInputFormat.getProducedType();

	assertArrayEquals(
		new TypeInformation[]{
			// primitives
			Types.BOOLEAN, Types.BYTE, Types.SHORT, Types.INT, Types.LONG, Types.FLOAT, Types.DOUBLE,
			// binary
			PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO,
			// string
			Types.STRING,
			// struct
			Types.ROW_NAMED(
				new String[]{"list"},
				ObjectArrayTypeInfo.getInfoFor(
					Types.ROW_NAMED(new String[]{"int1", "string1"}, Types.INT, Types.STRING))),
			// list
			ObjectArrayTypeInfo.getInfoFor(
				Types.ROW_NAMED(new String[]{"int1", "string1"}, Types.INT, Types.STRING)),
			// map
			new MapTypeInfo<>(Types.STRING, Types.ROW_NAMED(new String[]{"int1", "string1"}, Types.INT, Types.STRING))
		},
		producedType.getFieldTypes());
	assertArrayEquals(
		new String[]{"boolean1", "byte1", "short1", "int1", "long1", "float1", "double1", "bytes1", "string1", "middle", "list", "map"},
		producedType.getFieldNames());
}
 
Example #22
Source Project: Flink-CEPplus   Author: ljygz   File: FieldAccessorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testArray() {
	int[] a = new int[]{3, 5};
	FieldAccessor<int[], Integer> fieldAccessor =
			(FieldAccessor<int[], Integer>) (Object)
					FieldAccessorFactory.getAccessor(PrimitiveArrayTypeInfo.getInfoFor(a.getClass()), 1, null);

	assertEquals(Integer.class, fieldAccessor.getFieldType().getTypeClass());

	assertEquals((Integer) a[1], fieldAccessor.get(a));

	a = fieldAccessor.set(a, 6);
	assertEquals((Integer) a[1], fieldAccessor.get(a));

	Integer[] b = new Integer[]{3, 5};
	FieldAccessor<Integer[], Integer> fieldAccessor2 =
			(FieldAccessor<Integer[], Integer>) (Object)
					FieldAccessorFactory.getAccessor(BasicArrayTypeInfo.getInfoFor(b.getClass()), 1, null);

	assertEquals(Integer.class, fieldAccessor2.getFieldType().getTypeClass());

	assertEquals(b[1], fieldAccessor2.get(b));

	b = fieldAccessor2.set(b, 6);
	assertEquals(b[1], fieldAccessor2.get(b));
}
 
Example #23
Source Project: flink   Author: flink-tpc-ds   File: HCatInputFormatBase.java    License: Apache License 2.0 5 votes vote down vote up
private TypeInformation getFieldType(HCatFieldSchema fieldSchema) {

		switch(fieldSchema.getType()) {
			case INT:
				return BasicTypeInfo.INT_TYPE_INFO;
			case TINYINT:
				return BasicTypeInfo.BYTE_TYPE_INFO;
			case SMALLINT:
				return BasicTypeInfo.SHORT_TYPE_INFO;
			case BIGINT:
				return BasicTypeInfo.LONG_TYPE_INFO;
			case BOOLEAN:
				return BasicTypeInfo.BOOLEAN_TYPE_INFO;
			case FLOAT:
				return BasicTypeInfo.FLOAT_TYPE_INFO;
			case DOUBLE:
				return BasicTypeInfo.DOUBLE_TYPE_INFO;
			case STRING:
				return BasicTypeInfo.STRING_TYPE_INFO;
			case BINARY:
				return PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO;
			case ARRAY:
				return new GenericTypeInfo(List.class);
			case MAP:
				return new GenericTypeInfo(Map.class);
			case STRUCT:
				return new GenericTypeInfo(List.class);
			default:
				throw new IllegalArgumentException("Unknown data type \"" + fieldSchema.getType() + "\" encountered.");
		}
	}
 
Example #24
Source Project: flink   Author: flink-tpc-ds   File: JDBCTypeUtil.java    License: Apache License 2.0 5 votes vote down vote up
static int typeInformationToSqlType(TypeInformation<?> type) {

		if (TYPE_MAPPING.containsKey(type)) {
			return TYPE_MAPPING.get(type);
		} else if (type instanceof ObjectArrayTypeInfo || type instanceof PrimitiveArrayTypeInfo) {
			return Types.ARRAY;
		} else {
			throw new IllegalArgumentException("Unsupported type: " + type);
		}
	}
 
Example #25
Source Project: flink   Author: flink-tpc-ds   File: OrcTableSourceTest.java    License: Apache License 2.0 5 votes vote down vote up
private TypeInformation[] getNestedFieldTypes() {
	return new TypeInformation[]{
		Types.BOOLEAN, Types.BYTE, Types.SHORT, Types.INT, Types.LONG, Types.FLOAT, Types.DOUBLE,
		PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO, Types.STRING,
		Types.ROW_NAMED(
			new String[]{"list"},
			ObjectArrayTypeInfo.getInfoFor(
				Types.ROW_NAMED(
					new String[]{"int1", "string1"},
					Types.INT, Types.STRING
				)
			)
		),
		ObjectArrayTypeInfo.getInfoFor(
			Types.ROW_NAMED(
				new String[]{"int1", "string1"},
				Types.INT, Types.STRING
			)
		),
		new MapTypeInfo<>(
			Types.STRING,
			Types.ROW_NAMED(
				new String[]{"int1", "string1"},
				Types.INT, Types.STRING
			)
		)
	};
}
 
Example #26
Source Project: flink   Author: flink-tpc-ds   File: OrcRowInputFormatTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testProducedType() throws IOException {
	rowOrcInputFormat =
		new OrcRowInputFormat(getPath(TEST_FILE_NESTED), TEST_SCHEMA_NESTED, new Configuration());

	assertTrue(rowOrcInputFormat.getProducedType() instanceof RowTypeInfo);
	RowTypeInfo producedType = (RowTypeInfo) rowOrcInputFormat.getProducedType();

	assertArrayEquals(
		new TypeInformation[]{
			// primitives
			Types.BOOLEAN, Types.BYTE, Types.SHORT, Types.INT, Types.LONG, Types.FLOAT, Types.DOUBLE,
			// binary
			PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO,
			// string
			Types.STRING,
			// struct
			Types.ROW_NAMED(
				new String[]{"list"},
				ObjectArrayTypeInfo.getInfoFor(
					Types.ROW_NAMED(new String[]{"int1", "string1"}, Types.INT, Types.STRING))),
			// list
			ObjectArrayTypeInfo.getInfoFor(
				Types.ROW_NAMED(new String[]{"int1", "string1"}, Types.INT, Types.STRING)),
			// map
			new MapTypeInfo<>(Types.STRING, Types.ROW_NAMED(new String[]{"int1", "string1"}, Types.INT, Types.STRING))
		},
		producedType.getFieldTypes());
	assertArrayEquals(
		new String[]{"boolean1", "byte1", "short1", "int1", "long1", "float1", "double1", "bytes1", "string1", "middle", "list", "map"},
		producedType.getFieldNames());
}
 
Example #27
Source Project: flink   Author: flink-tpc-ds   File: OrcRowInputFormatTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testProducedTypeWithProjection() throws IOException {
	rowOrcInputFormat =
		new OrcRowInputFormat(getPath(TEST_FILE_NESTED), TEST_SCHEMA_NESTED, new Configuration());

	rowOrcInputFormat.selectFields(9, 3, 7, 10);

	assertTrue(rowOrcInputFormat.getProducedType() instanceof RowTypeInfo);
	RowTypeInfo producedType = (RowTypeInfo) rowOrcInputFormat.getProducedType();

	assertArrayEquals(
		new TypeInformation[]{
			// struct
			Types.ROW_NAMED(
				new String[]{"list"},
				ObjectArrayTypeInfo.getInfoFor(
					Types.ROW_NAMED(new String[]{"int1", "string1"}, Types.INT, Types.STRING))),
			// int
			Types.INT,
			// binary
			PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO,
			// list
			ObjectArrayTypeInfo.getInfoFor(
				Types.ROW_NAMED(new String[]{"int1", "string1"}, Types.INT, Types.STRING))
		},
		producedType.getFieldTypes());
	assertArrayEquals(
		new String[]{"middle", "int1", "bytes1", "list"},
		producedType.getFieldNames());
}
 
Example #28
Source Project: flink   Author: flink-tpc-ds   File: CsvRowSchemaConverter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Convert {@link TypeInformation} to {@link CsvSchema.ColumnType} based on Jackson's categories.
 */
private static CsvSchema.ColumnType convertType(String fieldName, TypeInformation<?> info) {
	if (STRING_TYPES.contains(info)) {
		return CsvSchema.ColumnType.STRING;
	} else if (NUMBER_TYPES.contains(info)) {
		return CsvSchema.ColumnType.NUMBER;
	} else if (BOOLEAN_TYPES.contains(info)) {
		return CsvSchema.ColumnType.BOOLEAN;
	} else if (info instanceof ObjectArrayTypeInfo) {
		validateNestedField(fieldName, ((ObjectArrayTypeInfo) info).getComponentInfo());
		return CsvSchema.ColumnType.ARRAY;
	} else if (info instanceof BasicArrayTypeInfo) {
		validateNestedField(fieldName, ((BasicArrayTypeInfo) info).getComponentInfo());
		return CsvSchema.ColumnType.ARRAY;
	} else if (info instanceof RowTypeInfo) {
		final TypeInformation<?>[] types = ((RowTypeInfo) info).getFieldTypes();
		for (TypeInformation<?> type : types) {
			validateNestedField(fieldName, type);
		}
		return CsvSchema.ColumnType.ARRAY;
	} else if (info instanceof PrimitiveArrayTypeInfo &&
			((PrimitiveArrayTypeInfo) info).getComponentType() == Types.BYTE) {
		return CsvSchema.ColumnType.STRING;
	} else {
		throw new IllegalArgumentException(
			"Unsupported type information '" + info.toString() + "' for field '" + fieldName + "'.");
	}
}
 
Example #29
Source Project: flink   Author: flink-tpc-ds   File: ParquetMapInputFormat.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void convert(Map<String, Object> map, Row row, TypeInformation<?>[] fieldTypes, String[] fieldNames) {
	for (int i = 0; i < fieldNames.length; i++) {
		if (row.getField(i) != null) {
			if (fieldTypes[i] instanceof BasicTypeInfo
				|| fieldTypes[i] instanceof PrimitiveArrayTypeInfo
				|| fieldTypes[i] instanceof BasicArrayTypeInfo) {
				map.put(fieldNames[i], row.getField(i));
			} else if (fieldTypes[i] instanceof RowTypeInfo) {
				Map<String, Object> nestedRow = new HashMap<>();
				RowTypeInfo nestedRowTypeInfo = (RowTypeInfo) fieldTypes[i];
				convert(nestedRow, (Row) row.getField(i),
					nestedRowTypeInfo.getFieldTypes(), nestedRowTypeInfo.getFieldNames());
				map.put(fieldNames[i], nestedRow);
			} else if (fieldTypes[i] instanceof MapTypeInfo) {
				Map<String, Object> nestedMap = new HashMap<>();
				MapTypeInfo mapTypeInfo = (MapTypeInfo) fieldTypes[i];
				convert(nestedMap, (Map<String, Object>) row.getField(i), mapTypeInfo);
				map.put(fieldNames[i], nestedMap);
			} else if (fieldTypes[i] instanceof ObjectArrayTypeInfo) {
				List<Object> nestedObjectList = new ArrayList<>();
				ObjectArrayTypeInfo objectArrayTypeInfo = (ObjectArrayTypeInfo) fieldTypes[i];
				convert(nestedObjectList, (Row[]) row.getField(i), objectArrayTypeInfo);
				map.put(fieldNames[i], nestedObjectList);
			}
		}
	}
}
 
Example #30
Source Project: flink   Author: flink-tpc-ds   File: OneInputOperatorTransformation.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Partitions the operator state of a {@link OperatorTransformation} by the given key positions.
 *
 * @param fields The position of the fields on which the {@code OperatorTransformation} will be grouped.
 * @return The {@code OperatorTransformation} with partitioned state.
 */
public KeyedOperatorTransformation<Tuple, T> keyBy(int... fields) {
	if (dataSet.getType() instanceof BasicArrayTypeInfo || dataSet.getType() instanceof PrimitiveArrayTypeInfo) {
		return keyBy(KeySelectorUtil.getSelectorForArray(fields, dataSet.getType()));
	} else {
		return keyBy(new Keys.ExpressionKeys<>(fields, dataSet.getType()));
	}
}