Java Code Examples for org.apache.flink.api.java.tuple.Tuple#setField()

The following examples show how to use org.apache.flink.api.java.tuple.Tuple#setField() . 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: ArrayFromTupleTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testConvertFromTupleToArray() throws InstantiationException, IllegalAccessException {
	for (int i = 0; i < Tuple.MAX_ARITY; i++) {
		Tuple currentTuple = (Tuple) CLASSES[i].newInstance();
		String[] currentArray = new String[i + 1];
		for (int j = 0; j <= i; j++) {
			currentTuple.setField(testStrings[j], j);
			currentArray[j] = testStrings[j];
		}
		arrayEqualityCheck(currentArray, new ArrayFromTuple().extract(currentTuple));
	}
}
 
Example 2
Source File: KeySelectorUtil.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public Tuple getKey(IN value) {
	Tuple key = Tuple.newInstance(fields.length);
	for (int i = 0; i < fields.length; i++) {
		key.setField(Array.get(value, fields[i]), i);
	}
	return key;
}
 
Example 3
Source File: FieldsFromTupleTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testUserSpecifiedOrder() throws InstantiationException, IllegalAccessException {
	Tuple currentTuple = (Tuple) CLASSES[Tuple.MAX_ARITY - 1].newInstance();
	for (int i = 0; i < Tuple.MAX_ARITY; i++) {
		currentTuple.setField(testDouble[i], i);
	}

	double[] expected = { testDouble[5], testDouble[3], testDouble[6], testDouble[7],
			testDouble[0] };
	arrayEqualityCheck(expected, new FieldsFromTuple(5, 3, 6, 7, 0).extract(currentTuple));

	double[] expected2 = { testDouble[0], testDouble[Tuple.MAX_ARITY - 1] };
	arrayEqualityCheck(expected2,
			new FieldsFromTuple(0, Tuple.MAX_ARITY - 1).extract(currentTuple));

	double[] expected3 = { testDouble[Tuple.MAX_ARITY - 1], testDouble[0] };
	arrayEqualityCheck(expected3,
			new FieldsFromTuple(Tuple.MAX_ARITY - 1, 0).extract(currentTuple));

	double[] expected4 = { testDouble[13], testDouble[4], testDouble[5], testDouble[4],
			testDouble[2], testDouble[8], testDouble[6], testDouble[2], testDouble[8],
			testDouble[3], testDouble[5], testDouble[2], testDouble[16], testDouble[4],
			testDouble[3], testDouble[2], testDouble[6], testDouble[4], testDouble[7],
			testDouble[4], testDouble[2], testDouble[8], testDouble[7], testDouble[2] };
	arrayEqualityCheck(expected4, new FieldsFromTuple(13, 4, 5, 4, 2, 8, 6, 2, 8, 3, 5, 2, 16,
			4, 3, 2, 6, 4, 7, 4, 2, 8, 7, 2).extract(currentTuple));
}
 
Example 4
Source File: DataFormatConverters.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
Tuple toExternalImpl(RowData value) {
	try {
		Tuple tuple = clazz.newInstance();
		for (int i = 0; i < converters.length; i++) {
			tuple.setField(converters[i].toExternal(value, i), i);
		}
		return tuple;
	} catch (InstantiationException | IllegalAccessException e) {
		throw new RuntimeException(e);
	}

}
 
Example 5
Source File: SiddhiTupleFactory.java    From bahir-flink with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T extends Tuple> T setTupleValue(Tuple tuple, Object[] row) {
    if (row.length != tuple.getArity()) {
        throw new IllegalArgumentException("Row length" + row.length + " is not equal with tuple's arity: " + tuple.getArity());
    }
    for (int i = 0; i < row.length; i++) {
        tuple.setField(row[i], i);
    }
    return (T) tuple;
}
 
Example 6
Source File: SiddhiTupleFactory.java    From flink-siddhi with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T extends Tuple> T setTupleValue(Tuple tuple, Object[] row) {
    if (row.length != tuple.getArity()) {
        throw new IllegalArgumentException("Row length" + row.length + " is not equal with tuple's arity: " + tuple.getArity());
    }
    for (int i = 0; i < row.length; i++) {
        tuple.setField(row[i], i);
    }
    return (T) tuple;
}
 
Example 7
Source File: FieldFromTupleTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleFieldExtraction() throws InstantiationException, IllegalAccessException {
	// extract single fields
	for (int i = 0; i < Tuple.MAX_ARITY; i++) {
		Tuple current = (Tuple) CLASSES[i].newInstance();
		for (int j = 0; j < i; j++) {
			current.setField(testStrings[j], j);
		}
		for (int j = 0; j < i; j++) {
			assertEquals(testStrings[j], new FieldFromTuple<String>(j).extract(current));
		}
	}
}
 
Example 8
Source File: FieldsFromTupleTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testUserSpecifiedOrder() throws InstantiationException, IllegalAccessException {
	Tuple currentTuple = (Tuple) CLASSES[Tuple.MAX_ARITY - 1].newInstance();
	for (int i = 0; i < Tuple.MAX_ARITY; i++) {
		currentTuple.setField(testDouble[i], i);
	}

	double[] expected = { testDouble[5], testDouble[3], testDouble[6], testDouble[7],
			testDouble[0] };
	arrayEqualityCheck(expected, new FieldsFromTuple(5, 3, 6, 7, 0).extract(currentTuple));

	double[] expected2 = { testDouble[0], testDouble[Tuple.MAX_ARITY - 1] };
	arrayEqualityCheck(expected2,
			new FieldsFromTuple(0, Tuple.MAX_ARITY - 1).extract(currentTuple));

	double[] expected3 = { testDouble[Tuple.MAX_ARITY - 1], testDouble[0] };
	arrayEqualityCheck(expected3,
			new FieldsFromTuple(Tuple.MAX_ARITY - 1, 0).extract(currentTuple));

	double[] expected4 = { testDouble[13], testDouble[4], testDouble[5], testDouble[4],
			testDouble[2], testDouble[8], testDouble[6], testDouble[2], testDouble[8],
			testDouble[3], testDouble[5], testDouble[2], testDouble[16], testDouble[4],
			testDouble[3], testDouble[2], testDouble[6], testDouble[4], testDouble[7],
			testDouble[4], testDouble[2], testDouble[8], testDouble[7], testDouble[2] };
	arrayEqualityCheck(expected4, new FieldsFromTuple(13, 4, 5, 4, 2, 8, 6, 2, 8, 3, 5, 2, 16,
			4, 3, 2, 6, 4, 7, 4, 2, 8, 7, 2).extract(currentTuple));
}
 
Example 9
Source File: FieldFromTupleTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleFieldExtraction() throws InstantiationException, IllegalAccessException {
	// extract single fields
	for (int i = 0; i < Tuple.MAX_ARITY; i++) {
		Tuple current = (Tuple) CLASSES[i].newInstance();
		for (int j = 0; j < i; j++) {
			current.setField(testStrings[j], j);
		}
		for (int j = 0; j < i; j++) {
			assertEquals(testStrings[j], new FieldFromTuple<String>(j).extract(current));
		}
	}
}
 
Example 10
Source File: ArrayFromTupleTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testConvertFromTupleToArray() throws InstantiationException, IllegalAccessException {
	for (int i = 0; i < Tuple.MAX_ARITY; i++) {
		Tuple currentTuple = (Tuple) CLASSES[i].newInstance();
		String[] currentArray = new String[i + 1];
		for (int j = 0; j <= i; j++) {
			currentTuple.setField(testStrings[j], j);
			currentArray[j] = testStrings[j];
		}
		arrayEqualityCheck(currentArray, new ArrayFromTuple().extract(currentTuple));
	}
}
 
Example 11
Source File: PythonReceiver.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public Tuple2<Tuple, byte[]> deserialize() {
	int keyTupleSize = fileBuffer.get();
	Tuple keys = createTuple(keyTupleSize);
	for (int x = 0; x < keyTupleSize; x++) {
		byte[] data = new byte[fileBuffer.getInt()];
		fileBuffer.get(data);
		keys.setField(data, x);
	}
	byte[] value = new byte[fileBuffer.getInt()];
	fileBuffer.get(value);
	return new Tuple2<>(keys, value);
}
 
Example 12
Source File: KeySelectorUtil.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public Tuple getKey(IN value) {
	Tuple key = Tuple.newInstance(keyLength);
	comparator.extractKeys(value, keyArray, 0);
	for (int i = 0; i < keyLength; i++) {
		key.setField(keyArray[i], i);
	}
	return key;
}
 
Example 13
Source File: DataFormatConverters.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
Tuple toExternalImpl(BaseRow value) {
	try {
		Tuple tuple = clazz.newInstance();
		for (int i = 0; i < converters.length; i++) {
			tuple.setField(converters[i].toExternal(value, i), i);
		}
		return tuple;
	} catch (InstantiationException | IllegalAccessException e) {
		throw new RuntimeException(e);
	}

}
 
Example 14
Source File: FieldFromTupleTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleFieldExtraction() throws InstantiationException, IllegalAccessException {
	// extract single fields
	for (int i = 0; i < Tuple.MAX_ARITY; i++) {
		Tuple current = (Tuple) CLASSES[i].newInstance();
		for (int j = 0; j < i; j++) {
			current.setField(testStrings[j], j);
		}
		for (int j = 0; j < i; j++) {
			assertEquals(testStrings[j], new FieldFromTuple<String>(j).extract(current));
		}
	}
}
 
Example 15
Source File: FieldsFromTupleTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testUserSpecifiedOrder() throws InstantiationException, IllegalAccessException {
	Tuple currentTuple = (Tuple) CLASSES[Tuple.MAX_ARITY - 1].newInstance();
	for (int i = 0; i < Tuple.MAX_ARITY; i++) {
		currentTuple.setField(testDouble[i], i);
	}

	double[] expected = { testDouble[5], testDouble[3], testDouble[6], testDouble[7],
			testDouble[0] };
	arrayEqualityCheck(expected, new FieldsFromTuple(5, 3, 6, 7, 0).extract(currentTuple));

	double[] expected2 = { testDouble[0], testDouble[Tuple.MAX_ARITY - 1] };
	arrayEqualityCheck(expected2,
			new FieldsFromTuple(0, Tuple.MAX_ARITY - 1).extract(currentTuple));

	double[] expected3 = { testDouble[Tuple.MAX_ARITY - 1], testDouble[0] };
	arrayEqualityCheck(expected3,
			new FieldsFromTuple(Tuple.MAX_ARITY - 1, 0).extract(currentTuple));

	double[] expected4 = { testDouble[13], testDouble[4], testDouble[5], testDouble[4],
			testDouble[2], testDouble[8], testDouble[6], testDouble[2], testDouble[8],
			testDouble[3], testDouble[5], testDouble[2], testDouble[16], testDouble[4],
			testDouble[3], testDouble[2], testDouble[6], testDouble[4], testDouble[7],
			testDouble[4], testDouble[2], testDouble[8], testDouble[7], testDouble[2] };
	arrayEqualityCheck(expected4, new FieldsFromTuple(13, 4, 5, 4, 2, 8, 6, 2, 8, 3, 5, 2, 16,
			4, 3, 2, 6, 4, 7, 4, 2, 8, 7, 2).extract(currentTuple));
}
 
Example 16
Source File: ArrayFromTupleTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testUserSpecifiedOrder() throws InstantiationException, IllegalAccessException {
	Tuple currentTuple = (Tuple) CLASSES[Tuple.MAX_ARITY - 1].newInstance();
	for (int i = 0; i < Tuple.MAX_ARITY; i++) {
		currentTuple.setField(testStrings[i], i);
	}

	String[] expected = { testStrings[5], testStrings[3], testStrings[6], testStrings[7],
			testStrings[0] };
	arrayEqualityCheck(expected, new ArrayFromTuple(5, 3, 6, 7, 0).extract(currentTuple));

	String[] expected2 = { testStrings[0], testStrings[Tuple.MAX_ARITY - 1] };
	arrayEqualityCheck(expected2,
			new ArrayFromTuple(0, Tuple.MAX_ARITY - 1).extract(currentTuple));

	String[] expected3 = { testStrings[Tuple.MAX_ARITY - 1], testStrings[0] };
	arrayEqualityCheck(expected3,
			new ArrayFromTuple(Tuple.MAX_ARITY - 1, 0).extract(currentTuple));

	String[] expected4 = { testStrings[13], testStrings[4], testStrings[5], testStrings[4],
			testStrings[2], testStrings[8], testStrings[6], testStrings[2], testStrings[8],
			testStrings[3], testStrings[5], testStrings[2], testStrings[16], testStrings[4],
			testStrings[3], testStrings[2], testStrings[6], testStrings[4], testStrings[7],
			testStrings[4], testStrings[2], testStrings[8], testStrings[7], testStrings[2] };
	arrayEqualityCheck(expected4, new ArrayFromTuple(13, 4, 5, 4, 2, 8, 6, 2, 8, 3, 5, 2, 16,
			4, 3, 2, 6, 4, 7, 4, 2, 8, 7, 2).extract(currentTuple));
}
 
Example 17
Source File: ArrayFromTupleTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testUserSpecifiedOrder() throws InstantiationException, IllegalAccessException {
	Tuple currentTuple = (Tuple) CLASSES[Tuple.MAX_ARITY - 1].newInstance();
	for (int i = 0; i < Tuple.MAX_ARITY; i++) {
		currentTuple.setField(testStrings[i], i);
	}

	String[] expected = { testStrings[5], testStrings[3], testStrings[6], testStrings[7],
			testStrings[0] };
	arrayEqualityCheck(expected, new ArrayFromTuple(5, 3, 6, 7, 0).extract(currentTuple));

	String[] expected2 = { testStrings[0], testStrings[Tuple.MAX_ARITY - 1] };
	arrayEqualityCheck(expected2,
			new ArrayFromTuple(0, Tuple.MAX_ARITY - 1).extract(currentTuple));

	String[] expected3 = { testStrings[Tuple.MAX_ARITY - 1], testStrings[0] };
	arrayEqualityCheck(expected3,
			new ArrayFromTuple(Tuple.MAX_ARITY - 1, 0).extract(currentTuple));

	String[] expected4 = { testStrings[13], testStrings[4], testStrings[5], testStrings[4],
			testStrings[2], testStrings[8], testStrings[6], testStrings[2], testStrings[8],
			testStrings[3], testStrings[5], testStrings[2], testStrings[16], testStrings[4],
			testStrings[3], testStrings[2], testStrings[6], testStrings[4], testStrings[7],
			testStrings[4], testStrings[2], testStrings[8], testStrings[7], testStrings[2] };
	arrayEqualityCheck(expected4, new ArrayFromTuple(13, 4, 5, 4, 2, 8, 6, 2, 8, 3, 5, 2, 16,
			4, 3, 2, 6, 4, 7, 4, 2, 8, 7, 2).extract(currentTuple));
}
 
Example 18
Source File: KeySelectorUtil.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public Tuple getKey(IN value) {
	Tuple key = Tuple.newInstance(fields.length);
	for (int i = 0; i < fields.length; i++) {
		key.setField(Array.get(value, fields[i]), i);
	}
	return key;
}
 
Example 19
Source File: KeySelectorUtil.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public Tuple getKey(IN value) {
	Tuple key = Tuple.newInstance(keyLength);
	comparator.extractKeys(value, keyArray, 0);
	for (int i = 0; i < keyLength; i++) {
		key.setField(keyArray[i], i);
	}
	return key;
}
 
Example 20
Source File: PythonPlanReceiver.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public Tuple deserialize(boolean normalized) throws IOException {
	Tuple result = createTuple(deserializer.length);
	for (int x = 0; x < result.getArity(); x++) {
		result.setField(deserializer[x].deserialize(normalized), x);
	}
	return result;
}