Search in sources :

Example 6 with StreamSchema

use of com.ibm.streams.operator.StreamSchema in project streamsx.topology by IBMStreams.

the class BOperatorInvocation method setParameter.

/*
    public BOperatorInvocation(GraphBuilder bt, String kind,
            Map<String, ? extends Object> params) {
        this(bt, Operator.class, params);
        json().put("kind", kind);
    }
    */
public void setParameter(String name, Object value) {
    Object jsonValue = value;
    String jsonType = null;
    if (value instanceof JSONAble) {
        value = ((JSONAble) value).toJSON();
    }
    if (value instanceof JSONObject) {
        JSONObject jo = ((JSONObject) value);
        if (jo.get("type") == null || jo.get("value") == null)
            throw new IllegalArgumentException("Illegal JSONObject " + jo);
        String type = (String) jo.get("type");
        Object val = (JSONObject) jo.get("value");
        if ("__spl_value".equals(type)) {
            /*
                 * The Value object is
                 * <pre><code>
                 * object {
                 *   type : "__spl_value"
                 *   value : object {
                 *     value : any. non-null. type appropriate for metaType
                 *     metaType : com.ibm.streams.operator.Type.MetaType.name() string
                 *   }
                 * }
                 * </code></pre>
                 */
            // unwrap and fall through to handling for the wrapped value
            JSONObject splValue = (JSONObject) val;
            value = splValue.get("value");
            jsonValue = value;
            String metaType = (String) splValue.get("metaType");
            if ("USTRING".equals(metaType) || "UINT8".equals(metaType) || "UINT16".equals(metaType) || "UINT32".equals(metaType) || "UINT64".equals(metaType)) {
                jsonType = metaType;
            }
        // fall through to handle jsonValue as usual 
        } else {
        // other kinds of JSONObject handled below
        }
    } else if (value instanceof Supplier<?>) {
        value = ((Supplier<?>) value).get();
        jsonValue = value;
    }
    if (value instanceof String) {
        op.setStringParameter(name, (String) value);
        if (jsonType == null)
            jsonType = MetaType.RSTRING.name();
    } else if (value instanceof Byte) {
        op.setByteParameter(name, (Byte) value);
        if (jsonType == null)
            jsonType = MetaType.INT8.name();
    } else if (value instanceof Short) {
        op.setShortParameter(name, (Short) value);
        if (jsonType == null)
            jsonType = MetaType.INT16.name();
    } else if (value instanceof Integer) {
        op.setIntParameter(name, (Integer) value);
        if (jsonType == null)
            jsonType = MetaType.INT32.name();
    } else if (value instanceof Long) {
        op.setLongParameter(name, (Long) value);
        if (jsonType == null)
            jsonType = MetaType.INT64.name();
    } else if (value instanceof Float) {
        op.setFloatParameter(name, (Float) value);
        jsonType = MetaType.FLOAT32.name();
    } else if (value instanceof Double) {
        op.setDoubleParameter(name, (Double) value);
        jsonType = MetaType.FLOAT64.name();
    } else if (value instanceof Boolean) {
        op.setBooleanParameter(name, (Boolean) value);
    } else if (value instanceof BigDecimal) {
        op.setBigDecimalParameter(name, (BigDecimal) value);
        // Need to maintain exact value
        jsonValue = value.toString();
    } else if (value instanceof Enum) {
        op.setCustomLiteralParameter(name, (Enum<?>) value);
        jsonValue = ((Enum<?>) value).name();
        jsonType = JParamTypes.TYPE_ENUM;
    } else if (value instanceof StreamSchema) {
        jsonValue = ((StreamSchema) value).getLanguageType();
        jsonType = JParamTypes.TYPE_SPLTYPE;
    } else if (value instanceof String[]) {
        String[] sa = (String[]) value;
        JSONArray a = new JSONArray(sa.length);
        for (String vs : sa) a.add(vs);
        jsonValue = a;
        op.setStringParameter(name, sa);
    } else if (value instanceof Attribute) {
        Attribute attr = (Attribute) value;
        jsonValue = attr.getName();
        jsonType = JParamTypes.TYPE_ATTRIBUTE;
        op.setAttributeParameter(name, attr.getName());
    } else if (value instanceof JSONObject) {
        JSONObject jo = (JSONObject) value;
        jsonType = (String) jo.get("type");
        jsonValue = (JSONObject) jo.get("value");
    } else {
        throw new IllegalArgumentException("Type for parameter " + name + " is not supported:" + value.getClass());
    }
    // Set the value as JSON
    JSONObject param = new JSONObject();
    param.put("value", jsonValue);
    if (jsonType != null)
        param.put("type", jsonType);
    jparams.put(name, param);
}
Also used : Attribute(com.ibm.streams.operator.Attribute) JSONArray(com.ibm.json.java.JSONArray) StreamSchema(com.ibm.streams.operator.StreamSchema) BigDecimal(java.math.BigDecimal) JSONObject(com.ibm.json.java.JSONObject) JSONAble(com.ibm.streamsx.topology.tuple.JSONAble) JSONObject(com.ibm.json.java.JSONObject) Supplier(com.ibm.streamsx.topology.function.Supplier)

Example 7 with StreamSchema

use of com.ibm.streams.operator.StreamSchema in project streamsx.topology by IBMStreams.

the class Topology method subscribe.

/**
     * Declare a stream that is a subscription to {@code topic}.
     * A topic is published using {@link TStream#publish(String)}.
     * Subscribers are matched to published streams when the {@code topic}
     * is an exact match and the type of the stream ({@code T},
     * {@code tupleTypeClass}) is an exact match.
     * <BR>
     * Publish-subscribe is a many to many relationship,
     * multiple streams from multiple applications may
     * be published on the same topic and type. Multiple
     * subscribers may subscribe to a topic and type.
     * <BR>
     * A subscription will match all publishers using the
     * same topic and tuple type. Tuples on the published
     * streams will appear on the returned stream, as
     * a single stream.
     * <BR>
     * The subscription is dynamic, the returned stream
     * will subscribe to a matching stream published by
     * a newly submitted application (a job), and stops a
     * subscription when an running job is cancelled.
     * <P>
     * Publish-subscribe only works when the topology is
     * submitted to a {@link com.ibm.streamsx.topology.context.StreamsContext.Type#DISTRIBUTED}
     * context. This allows different applications (or
     * even within the same application) to communicate
     * using published streams.
     * </P>
     * <P>
     * If {@code tupleTypeClass} is {@code JSONObject.class} then the
     * subscription is the generic IBM Streams schema for JSON
     * ({@link JSONSchemas#JSON}). Streams of type {@code JSONObject}
     * are always published and subscribed using the generic schema
     * to allow interchange between applications implemented in
     * different languages.
     * </P>
     * @param topic Topic to subscribe to.
     * @param tupleTypeClass Type to subscribe to.
     * @return Stream the will contain tuples from matching publishers.
     * 
     * @see TStream#publish(String)
     * @see SPLStreams#subscribe(TopologyElement, String, com.ibm.streams.operator.StreamSchema)
     */
public <T> TStream<T> subscribe(String topic, Class<T> tupleTypeClass) {
    checkTopicFilter(topic);
    if (JSONObject.class.equals(tupleTypeClass)) {
        @SuppressWarnings("unchecked") TStream<T> json = (TStream<T>) SPLStreams.subscribe(this, topic, JSONSchemas.JSON).toJSON();
        return json;
    }
    StreamSchema mappingSchema = Schemas.getSPLMappingSchema(tupleTypeClass);
    SPLStream splImport;
    // Subscribed as an SPL Stream.
    if (Schemas.usesDirectSchema(tupleTypeClass)) {
        splImport = SPLStreams.subscribe(this, topic, mappingSchema);
    } else {
        Map<String, Object> params = new HashMap<>();
        params.put("topic", topic);
        params.put("class", tupleTypeClass.getName());
        params.put("streamType", mappingSchema);
        splImport = SPL.invokeSource(this, "com.ibm.streamsx.topology.topic::SubscribeJava", params, mappingSchema);
    }
    return new StreamImpl<T>(this, splImport.output(), tupleTypeClass);
}
Also used : HashMap(java.util.HashMap) StreamImpl(com.ibm.streamsx.topology.internal.core.StreamImpl) JSONObject(com.ibm.json.java.JSONObject) StreamSchema(com.ibm.streams.operator.StreamSchema) SPLStream(com.ibm.streamsx.topology.spl.SPLStream)

Example 8 with StreamSchema

use of com.ibm.streams.operator.StreamSchema in project streamsx.topology by IBMStreams.

the class SimpleEmbeddedTest method testIsSupportedNeg.

@Test
public void testIsSupportedNeg() throws Exception {
    StreamSchema AnySchema = Type.Factory.getStreamSchema("tuple<rstring ticker>");
    Topology topology = new Topology("test");
    TStream<String> hw = topology.strings("Hello", "World!", "Test!!");
    // add SPL (non-java) operator
    FileSPLStreams.csvReader(hw, AnySchema);
    StreamsContext<?> sc = StreamsContextFactory.getStreamsContext(StreamsContext.Type.EMBEDDED);
    assertTrue(!sc.isSupported(topology));
    sc = StreamsContextFactory.getStreamsContext(StreamsContext.Type.EMBEDDED_TESTER);
    assertTrue(!sc.isSupported(topology));
}
Also used : Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) StreamSchema(com.ibm.streams.operator.StreamSchema) Test(org.junit.Test)

Example 9 with StreamSchema

use of com.ibm.streams.operator.StreamSchema in project streamsx.topology by IBMStreams.

the class PythonFunctionalOperatorsTest method testNonHashableMapInput.

@Test(expected = Exception.class)
public void testNonHashableMapInput() throws Exception {
    StreamSchema bad = Type.Factory.getStreamSchema("tuple<map<set<int32>,rstring> a>");
    _testSchemaBuild(bad, INT32_SCHEMA);
}
Also used : StreamSchema(com.ibm.streams.operator.StreamSchema) Test(org.junit.Test)

Example 10 with StreamSchema

use of com.ibm.streams.operator.StreamSchema in project streamsx.topology by IBMStreams.

the class PythonFunctionalOperatorsTest method testNonHashableSetOutput.

@Test(expected = Exception.class)
public void testNonHashableSetOutput() throws Exception {
    StreamSchema bad = Type.Factory.getStreamSchema("tuple<set<list<int32>> a>");
    _testSchemaBuild(INT32_SCHEMA, bad);
}
Also used : StreamSchema(com.ibm.streams.operator.StreamSchema) Test(org.junit.Test)

Aggregations

StreamSchema (com.ibm.streams.operator.StreamSchema)32 Test (org.junit.Test)16 SPLStream (com.ibm.streamsx.topology.spl.SPLStream)15 Topology (com.ibm.streamsx.topology.Topology)14 OutputTuple (com.ibm.streams.operator.OutputTuple)12 Tester (com.ibm.streamsx.topology.tester.Tester)12 List (java.util.List)11 HashMap (java.util.HashMap)8 TStream (com.ibm.streamsx.topology.TStream)7 TestTopology (com.ibm.streamsx.topology.test.TestTopology)7 Map (java.util.Map)7 Attribute (com.ibm.streams.operator.Attribute)6 Constants (com.ibm.streamsx.kafka.test.utils.Constants)6 Delay (com.ibm.streamsx.kafka.test.utils.Delay)6 KafkaSPLStreamsUtils (com.ibm.streamsx.kafka.test.utils.KafkaSPLStreamsUtils)6 StreamsContext (com.ibm.streamsx.topology.context.StreamsContext)6 Type (com.ibm.streamsx.topology.context.StreamsContext.Type)6 StreamsContextFactory (com.ibm.streamsx.topology.context.StreamsContextFactory)6 BiFunction (com.ibm.streamsx.topology.function.BiFunction)6 SPL (com.ibm.streamsx.topology.spl.SPL)6