Data Types
Tuples
Tuples are composite types that contain a fixed number of fields with various types. The Java API provides classes from Tuple1 up to Tuple25. Every field of a tuple can be an arbitrary Flink type including further tuples, resulting in nested tuples. Fields of a tuple can be accessed directly using the field’s name as tuple.f4, or using the generic getter method tuple.getField(int position). The field indices start at 0. Note that this stands in contrast to the Scala tuples, but it is more consistent with Java’s general indexing.
DataStream<Tuple2<String, Integer>> wordCounts = env.fromElements(
new Tuple2<String, Integer>("hello", 1),
new Tuple2<String, Integer>("world", 2));
wordCounts.map(new MapFunction<Tuple2<String, Integer>, Integer>() {
@Override
public Integer map(Tuple2<String, Integer> value) throws Exception {
return value.f1;
}
});
wordCounts.keyBy(value -> value.f0);
javatuples
Technically speaking, a tuple is like a small and immutable wrapper object that holds different pieces of information. Once we put them in, we can’t change or remove these things. The pieces of contained information do not necessarily relate to each other in any way, but collectively these will have some meaning.
Sample Tuples
["Java", 1.8, "Windows"]
["Alex", 32, "New York", true]
[3, "Alexa", "howtodoinjava.com", 37000]
javatuples offers you tuple classes from one to ten elements:
- Unit<A> (1 element)
- Pair<A,B> (2 elements)
- Triplet<A,B,C> (3 elements)
- Quartet<A,B,C,D> (4 elements)
- Quintet<A,B,C,D,E> (5 elements)
- Sextet<A,B,C,D,E,F> (6 elements)
- Septet<A,B,C,D,E,F,G> (7 elements)
- Octet<A,B,C,D,E,F,G,H> (8 elements)
- Ennead<A,B,C,D,E,F,G,H,I> (9 elements)
- Decade<A,B,C,D,E,F,G,H,I,J> (10 elements)
All tuple classes are:
- Typesafe
- Immutable
- Iterable
- Serializable
- Comparable (implements Comparable<Tuple>)
- Implementing equals(...) and hashCode()
- Implementing toString()
javatuples
Java Tuple (with Examples)