Working with SPL types
Each SPL type maps to a Java™ counterpart.
The following mappings apply:
SPL type | Java™ object type | Java™ primitive type |
---|---|---|
boolean |
java.lang.Boolean |
boolean |
enum |
java.lang.String |
none |
int8 |
java.lang.Byte |
byte |
int16 |
java.lang.Short |
short |
int32 |
java.lang.Integer |
int |
int64 |
java.lang.Long |
long |
uint8 |
java.lang.Byte |
byte |
uint16 |
java.lang.Short |
short |
uint32 |
java.lang.Integer |
int |
uint64 |
java.lang.Long |
long |
float32 |
java.lang.Float |
float |
float64 |
java.lang.Double |
double |
decimal32 |
java.math.BigDecimal |
none |
decimal64 |
java.math.BigDecimal |
none |
decimal128 |
java.math.BigDecimal |
none |
complex32 |
org.apache.commons.math.complex.Complex |
none |
complex64 |
org.apache.commons.math.complex.Complex |
none |
timestamp |
Timestamp |
none |
rstring |
RString |
none |
ustring |
java.lang.String |
none |
rstring[n] |
RString |
none |
blob |
Blob |
none |
xml |
com.ibm.streams.operator.types.XML |
none |
list<T> |
Depends on the element type T, see Table 2 | none |
bounded list type |
Depends on the element type T, see Table 2 | none |
set<T> |
java.util.Set<Tc> |
none |
bounded set type |
java.util.Set<Tc> |
none |
map<K,V> |
java.util.Map<Kc,Vc> |
none |
bounded map type |
java.util.Map<Kc,Vc> |
none |
tuple<T name, ...> |
Tuple |
none |
optional<T> |
Java object type for type T | none |
SPL type | Object type | java.util.List type |
---|---|---|
list<boolean> |
boolean[] |
List<Boolean> |
list<int8> |
byte[] |
List<Byte> |
list<int16> |
short[] |
List<Short> |
list<int32> |
int[] |
List<Integer> |
list<int64> |
long[] |
List<Long> |
list<float32> |
float[] |
List<Float> |
list<float64> |
double[] |
List<Double> |
list<ustring> |
String[] |
List<String> |
list<rstring> |
java.util.List<RString> |
|
list<T> |
java.util.List<Tc> |
An SPL tuple is a collection of attributes, each with a specified SPL type. When you implement an operator in Java, you must understand how each SPL type is represented as a value in Java™. The following section describes the type-related metadata available in the Java™ Operator API.
An instance of an SPL type in a tuple is represented by
an instance of the com.ibm.streams.operator.Type
interface. A Type
object
contains type-specific information for the SPL type for an attribute,
such as the type's bound or a list's element type, and fundamental
information about the SPL type.
The fundamental type information
is represented by the enumeration Type.MetaType
and
is obtained using the getMetaType()
method on a Type
instance. The
values of the enumeration naturally map to SPL type names with the exceptions BSTRING
, BLIST
, BSET
,
and BMAP
, which map to the bounded versions of SPL
types rstring
, list
, set
,
and map
.
The Javadoc™ for
each enumerated value in Type.MetaType
describes
how a type's value is represented in Java™. Some types can have multiple possible
representations in Java™. For
example, SPL float64
can be represented as Java™ object type java.lang.Double
and
primitive Java™ type double
. The Javadoc™ also contains any special
information about the type's representation in Java™, such as for list types (MetaType.LIST
and BLIST
),
where for some list element types (for example, list<int32>
the
object type is a Java™ primitive int
array (int[])
instead of a List<Integer>
).
Type
specific information is obtained by casting the instance of Type
to
a specific interface from the com.ibm.streams.operator.meta
package.
For example, for a map
type (MetaType.MAP
and BMAP
)
the Type
instance is also an instance of com.ibm.streams.operator.meta.MapType
. MapType
provides
information about the types for the keys and values, with each type
described as a Type
instance. The Javadoc™ for a Type
's MetaType
enumeration
value describes which interfaces from the com.ibm.streams.operator.meta
package
are implemented by the instance of Type
.
Type
information is only associated with an attribute of a tuple and is
obtained from the Attribute.getType()
method. Attribute
objects for a tuple are obtained using the StreamSchema
object
that is associated with a tuple, either through its port definition
(StreamingData.getStreamSchema()
) or through Tuple.getStreamSchema()
for
any tuple, including tuples that are nested as attributes of another tuple.
stream<int32 sensorid, list<float32>[4] readings, timestamp when>
Look at the Type.MetaType Javadoc™ for INT32, LIST and TIMESTAMP to understand the Java™ representation of the SPL types. From the Javadoc™ you see that the following code extract can be used to get the attribute's values from a tuple on an input port:
import com.ibm.streams.operator.types.Timestamp;
…
@Override
public void process(StreamingInput port, Tuple tuple) {
int id = tuple.getInt("sensorid");
float[] readings = (float[]) tuple.getObject("readings");
Timestamp when = tuple.getTimestamp("when");
// process readings …
…
}
Your operator can also determine the bound of the readings attribute as follows:
int getMaxNumberOfReadings(StreamingInput port) {
StreamSchema schema = port.getStreamSchema();
Attribute attr = schema.getAttribute("readings");
BoundedType bounded = (BoundedType) attr.getType();
return bounded.getBound();
}