Search in sources :

Example 6 with ExpressionKeys

use of org.apache.flink.api.common.operators.Keys.ExpressionKeys in project flink by apache.

the class PojoComparatorTest method createComparator.

@Override
protected TypeComparator<PojoContainingTuple> createComparator(boolean ascending) {
    Assert.assertTrue(type instanceof CompositeType);
    CompositeType<PojoContainingTuple> cType = (CompositeType<PojoContainingTuple>) type;
    ExpressionKeys<PojoContainingTuple> keys = new ExpressionKeys<PojoContainingTuple>(new String[] { "theTuple.*" }, cType);
    boolean[] orders = new boolean[keys.getNumberOfKeyFields()];
    Arrays.fill(orders, ascending);
    return cType.createComparator(keys.computeLogicalKeyPositions(), orders, 0, new ExecutionConfig());
}
Also used : ExpressionKeys(org.apache.flink.api.common.operators.Keys.ExpressionKeys) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) CompositeType(org.apache.flink.api.common.typeutils.CompositeType)

Example 7 with ExpressionKeys

use of org.apache.flink.api.common.operators.Keys.ExpressionKeys in project flink by apache.

the class PojoSerializerTest method testTuplePojoTestEquality.

/**
 * This tests if the hashes returned by the pojo and tuple comparators are the same
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testTuplePojoTestEquality() {
    // test with a simple, string-key first.
    PojoTypeInfo<TestUserClass> pType = (PojoTypeInfo<TestUserClass>) type;
    List<FlatFieldDescriptor> result = new ArrayList<FlatFieldDescriptor>();
    pType.getFlatFields("nestedClass.dumm2", 0, result);
    // see below
    int[] fields = new int[1];
    fields[0] = result.get(0).getPosition();
    TypeComparator<TestUserClass> pojoComp = pType.createComparator(fields, new boolean[] { true }, 0, new ExecutionConfig());
    TestUserClass pojoTestRecord = new TestUserClass(0, "abc", 3d, new int[] { 1, 2, 3 }, new Date(), new NestedTestUserClass(1, "haha", 4d, new int[] { 5, 4, 3 }));
    int pHash = pojoComp.hash(pojoTestRecord);
    Tuple1<String> tupleTest = new Tuple1<String>("haha");
    TupleTypeInfo<Tuple1<String>> tType = (TupleTypeInfo<Tuple1<String>>) TypeExtractor.getForObject(tupleTest);
    TypeComparator<Tuple1<String>> tupleComp = tType.createComparator(new int[] { 0 }, new boolean[] { true }, 0, new ExecutionConfig());
    int tHash = tupleComp.hash(tupleTest);
    Assert.assertTrue("The hashing for tuples and pojos must be the same, so that they are mixable", pHash == tHash);
    Tuple3<Integer, String, Double> multiTupleTest = new Tuple3<Integer, String, Double>(1, "haha", // its important here to use the same values.
    4d);
    TupleTypeInfo<Tuple3<Integer, String, Double>> multiTupleType = (TupleTypeInfo<Tuple3<Integer, String, Double>>) TypeExtractor.getForObject(multiTupleTest);
    ExpressionKeys fieldKey = new ExpressionKeys(new int[] { 1, 0, 2 }, multiTupleType);
    ExpressionKeys expressKey = new ExpressionKeys(new String[] { "nestedClass.dumm2", "nestedClass.dumm1", "nestedClass.dumm3" }, pType);
    try {
        Assert.assertTrue("Expecting the keys to be compatible", fieldKey.areCompatible(expressKey));
    } catch (IncompatibleKeysException e) {
        e.printStackTrace();
        Assert.fail("Keys must be compatible: " + e.getMessage());
    }
    TypeComparator<TestUserClass> multiPojoComp = pType.createComparator(expressKey.computeLogicalKeyPositions(), new boolean[] { true, true, true }, 0, new ExecutionConfig());
    int multiPojoHash = multiPojoComp.hash(pojoTestRecord);
    // pojo order is: dumm2 (str), dumm1 (int), dumm3 (double).
    TypeComparator<Tuple3<Integer, String, Double>> multiTupleComp = multiTupleType.createComparator(fieldKey.computeLogicalKeyPositions(), new boolean[] { true, true, true }, 0, new ExecutionConfig());
    int multiTupleHash = multiTupleComp.hash(multiTupleTest);
    Assert.assertTrue("The hashing for tuples and pojos must be the same, so that they are mixable. Also for those with multiple key fields", multiPojoHash == multiTupleHash);
}
Also used : ArrayList(java.util.ArrayList) PojoTypeInfo(org.apache.flink.api.java.typeutils.PojoTypeInfo) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) FlatFieldDescriptor(org.apache.flink.api.common.typeutils.CompositeType.FlatFieldDescriptor) ExpressionKeys(org.apache.flink.api.common.operators.Keys.ExpressionKeys) Date(java.util.Date) TupleTypeInfo(org.apache.flink.api.java.typeutils.TupleTypeInfo) Tuple1(org.apache.flink.api.java.tuple.Tuple1) Tuple3(org.apache.flink.api.java.tuple.Tuple3) IncompatibleKeysException(org.apache.flink.api.common.operators.Keys.IncompatibleKeysException) Test(org.junit.Test)

Aggregations

ExpressionKeys (org.apache.flink.api.common.operators.Keys.ExpressionKeys)7 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)3 Test (org.junit.Test)3 CompositeType (org.apache.flink.api.common.typeutils.CompositeType)2 Tuple3 (org.apache.flink.api.java.tuple.Tuple3)2 TupleTypeInfo (org.apache.flink.api.java.typeutils.TupleTypeInfo)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 GroupReduceFunction (org.apache.flink.api.common.functions.GroupReduceFunction)1 RichGroupReduceFunction (org.apache.flink.api.common.functions.RichGroupReduceFunction)1 IncompatibleKeysException (org.apache.flink.api.common.operators.Keys.IncompatibleKeysException)1 SelectorFunctionKeys (org.apache.flink.api.common.operators.Keys.SelectorFunctionKeys)1 Ordering (org.apache.flink.api.common.operators.Ordering)1 KeySelector1 (org.apache.flink.api.common.operators.SelectorFunctionKeysTest.KeySelector1)1 KeySelector3 (org.apache.flink.api.common.operators.SelectorFunctionKeysTest.KeySelector3)1 UnaryOperatorInformation (org.apache.flink.api.common.operators.UnaryOperatorInformation)1 GroupReduceOperatorBase (org.apache.flink.api.common.operators.base.GroupReduceOperatorBase)1 FlatFieldDescriptor (org.apache.flink.api.common.typeutils.CompositeType.FlatFieldDescriptor)1 CombineToGroupCombineWrapper (org.apache.flink.api.java.operators.translation.CombineToGroupCombineWrapper)1 RichCombineToGroupCombineWrapper (org.apache.flink.api.java.operators.translation.RichCombineToGroupCombineWrapper)1