Search in sources :

Example 1 with IBasicBolt

use of org.apache.heron.api.bolt.IBasicBolt in project heron by twitter.

the class StreamBuilder method buildStreams.

protected <K extends Serializable, V extends Serializable> void buildStreams(EcoExecutionContext executionContext, TopologyBuilder builder, ObjectBuilder objectBuilder) throws IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchFieldException, InvocationTargetException {
    EcoTopologyDefinition topologyDefinition = executionContext.getTopologyDefinition();
    Map<String, ComponentStream> componentStreams = new HashMap<>();
    HashMap<String, BoltDeclarer> declarers = new HashMap<>();
    for (StreamDefinition stream : topologyDefinition.getStreams()) {
        Object boltObj = executionContext.getBolt(stream.getTo());
        BoltDeclarer declarer = declarers.get(stream.getTo());
        if (boltObj instanceof IRichBolt) {
            if (declarer == null) {
                declarer = builder.setBolt(stream.getTo(), (IRichBolt) boltObj, topologyDefinition.parallelismForBolt(stream.getTo()));
                declarers.put(stream.getTo(), declarer);
            }
        } else if (boltObj instanceof IBasicBolt) {
            if (declarer == null) {
                declarer = builder.setBolt(stream.getTo(), (IBasicBolt) boltObj, topologyDefinition.parallelismForBolt(stream.getTo()));
                declarers.put(stream.getTo(), declarer);
            }
        } else if (boltObj instanceof IStatefulWindowedBolt) {
            if (declarer == null) {
                // noinspection unchecked
                declarer = builder.setBolt(stream.getTo(), (IStatefulWindowedBolt<K, V>) boltObj, topologyDefinition.parallelismForBolt(stream.getTo()));
                declarers.put(stream.getTo(), declarer);
            }
        } else if (boltObj instanceof IWindowedBolt) {
            if (declarer == null) {
                declarer = builder.setBolt(stream.getTo(), (IWindowedBolt) boltObj, topologyDefinition.parallelismForBolt(stream.getTo()));
                declarers.put(stream.getTo(), declarer);
            }
        } else {
            throw new IllegalArgumentException("Class does not appear to be a bolt: " + boltObj.getClass().getName());
        }
        GroupingDefinition grouping = stream.getGrouping();
        // if the streamId is defined, use it for the grouping,
        // otherwise assume default stream
        String streamId = grouping.getStreamId() == null ? Utils.DEFAULT_STREAM_ID : grouping.getStreamId();
        switch(grouping.getType()) {
            case SHUFFLE:
                declarer.shuffleGrouping(stream.getFrom(), streamId);
                break;
            case FIELDS:
                List<String> groupingArgs = grouping.getArgs();
                if (groupingArgs == null) {
                    throw new IllegalArgumentException("You must supply arguments for Fields grouping");
                }
                declarer.fieldsGrouping(stream.getFrom(), streamId, new Fields(groupingArgs));
                break;
            case ALL:
                declarer.allGrouping(stream.getFrom(), streamId);
                break;
            case GLOBAL:
                declarer.globalGrouping(stream.getFrom(), streamId);
                break;
            case NONE:
                declarer.noneGrouping(stream.getFrom(), streamId);
                break;
            case CUSTOM:
                declarer.customGrouping(stream.getFrom(), streamId, buildCustomStreamGrouping(stream.getGrouping().getCustomClass(), executionContext, objectBuilder));
                break;
            default:
                throw new UnsupportedOperationException("unsupported grouping type: " + grouping);
        }
    }
    executionContext.setStreams(componentStreams);
}
Also used : IRichBolt(org.apache.heron.api.bolt.IRichBolt) ComponentStream(org.apache.heron.eco.definition.ComponentStream) StreamDefinition(org.apache.heron.eco.definition.StreamDefinition) HashMap(java.util.HashMap) IStatefulWindowedBolt(org.apache.heron.api.bolt.IStatefulWindowedBolt) EcoTopologyDefinition(org.apache.heron.eco.definition.EcoTopologyDefinition) GroupingDefinition(org.apache.heron.eco.definition.GroupingDefinition) IBasicBolt(org.apache.heron.api.bolt.IBasicBolt) Fields(org.apache.heron.api.tuple.Fields) BoltDeclarer(org.apache.heron.api.topology.BoltDeclarer) IWindowedBolt(org.apache.heron.api.bolt.IWindowedBolt)

Aggregations

HashMap (java.util.HashMap)1 IBasicBolt (org.apache.heron.api.bolt.IBasicBolt)1 IRichBolt (org.apache.heron.api.bolt.IRichBolt)1 IStatefulWindowedBolt (org.apache.heron.api.bolt.IStatefulWindowedBolt)1 IWindowedBolt (org.apache.heron.api.bolt.IWindowedBolt)1 BoltDeclarer (org.apache.heron.api.topology.BoltDeclarer)1 Fields (org.apache.heron.api.tuple.Fields)1 ComponentStream (org.apache.heron.eco.definition.ComponentStream)1 EcoTopologyDefinition (org.apache.heron.eco.definition.EcoTopologyDefinition)1 GroupingDefinition (org.apache.heron.eco.definition.GroupingDefinition)1 StreamDefinition (org.apache.heron.eco.definition.StreamDefinition)1