Search in sources :

Example 1 with BoltDeclarer

use of org.apache.heron.api.topology.BoltDeclarer in project heron by twitter.

the class CustomStreamlet method doBuild.

/**
 * Connect this streamlet to TopologyBuilder.
 * @param bldr The TopologyBuilder for the topology
 * @param stageNames The existing stage names
 * @return True if successful
 */
@Override
public boolean doBuild(TopologyBuilder bldr, Set<String> stageNames) {
    // Create and set bolt
    BoltDeclarer declarer;
    if (operator instanceof IStreamletBasicOperator) {
        setDefaultNameIfNone(StreamletNamePrefix.CUSTOM, stageNames);
        IStreamletBasicOperator<R, T> op = (IStreamletBasicOperator<R, T>) operator;
        bldr.setBolt(getName(), op, getNumPartitions()).grouping(parent.getName(), parent.getStreamId(), grouper);
    } else if (operator instanceof IStreamletRichOperator) {
        setDefaultNameIfNone(StreamletNamePrefix.CUSTOM_BASIC, stageNames);
        IStreamletRichOperator<R, T> op = (IStreamletRichOperator<R, T>) operator;
        bldr.setBolt(getName(), op, getNumPartitions()).grouping(parent.getName(), parent.getStreamId(), grouper);
    } else if (operator instanceof IStreamletWindowOperator) {
        setDefaultNameIfNone(StreamletNamePrefix.CUSTOM_WINDOW, stageNames);
        IStreamletWindowOperator<R, T> op = (IStreamletWindowOperator<R, T>) operator;
        bldr.setBolt(getName(), op, getNumPartitions()).grouping(parent.getName(), parent.getStreamId(), grouper);
    } else {
        throw new RuntimeException("Unhandled operator class is found!");
    }
    return true;
}
Also used : IStreamletRichOperator(org.apache.heron.streamlet.IStreamletRichOperator) BoltDeclarer(org.apache.heron.api.topology.BoltDeclarer) IStreamletBasicOperator(org.apache.heron.streamlet.IStreamletBasicOperator) IStreamletWindowOperator(org.apache.heron.streamlet.IStreamletWindowOperator)

Example 2 with BoltDeclarer

use of org.apache.heron.api.topology.BoltDeclarer in project heron by twitter.

the class TopologyTests method createTopologyWithConnection.

/**
 * Create Topology proto object using HeronSubmitter API.
 *
 * @param heronConfig desired config params.
 * @param spouts spoutName -&gt; parallelism
 * @param bolts boltName -&gt; parallelism
 * @param connections connect default stream from value to key.
 * @return topology proto.
 */
public static TopologyAPI.Topology createTopologyWithConnection(String topologyName, Config heronConfig, Map<String, Integer> spouts, Map<String, Integer> bolts, Map<String, String> connections) {
    TopologyBuilder builder = new TopologyBuilder();
    BaseRichSpout baseSpout = new BaseRichSpout() {

        private static final long serialVersionUID = -719523487475322625L;

        public void declareOutputFields(OutputFieldsDeclarer declarer) {
            declarer.declare(new Fields("field1"));
        }

        public void open(Map<String, Object> conf, TopologyContext context, SpoutOutputCollector collector) {
        }

        public void nextTuple() {
        }
    };
    BaseBasicBolt basicBolt = new BaseBasicBolt() {

        private static final long serialVersionUID = 2544765902130713628L;

        public void execute(Tuple input, BasicOutputCollector collector) {
        }

        public void declareOutputFields(OutputFieldsDeclarer declarer) {
        }
    };
    for (String spout : spouts.keySet()) {
        builder.setSpout(spout, baseSpout, spouts.get(spout));
    }
    for (String bolt : bolts.keySet()) {
        BoltDeclarer boltDeclarer = builder.setBolt(bolt, basicBolt, bolts.get(bolt));
        if (connections.containsKey(bolt)) {
            boltDeclarer.shuffleGrouping(connections.get(bolt));
        }
    }
    HeronTopology heronTopology = builder.createTopology();
    return heronTopology.setName(topologyName).setConfig(heronConfig).setState(TopologyAPI.TopologyState.RUNNING).getTopology();
}
Also used : BaseBasicBolt(org.apache.heron.api.bolt.BaseBasicBolt) TopologyBuilder(org.apache.heron.api.topology.TopologyBuilder) HeronTopology(org.apache.heron.api.HeronTopology) OutputFieldsDeclarer(org.apache.heron.api.topology.OutputFieldsDeclarer) BasicOutputCollector(org.apache.heron.api.bolt.BasicOutputCollector) Fields(org.apache.heron.api.tuple.Fields) BoltDeclarer(org.apache.heron.api.topology.BoltDeclarer) SpoutOutputCollector(org.apache.heron.api.spout.SpoutOutputCollector) TopologyContext(org.apache.heron.api.topology.TopologyContext) HashMap(java.util.HashMap) Map(java.util.Map) Tuple(org.apache.heron.api.tuple.Tuple) BaseRichSpout(org.apache.heron.api.spout.BaseRichSpout)

Example 3 with BoltDeclarer

use of org.apache.heron.api.topology.BoltDeclarer 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

BoltDeclarer (org.apache.heron.api.topology.BoltDeclarer)3 HashMap (java.util.HashMap)2 Fields (org.apache.heron.api.tuple.Fields)2 Map (java.util.Map)1 HeronTopology (org.apache.heron.api.HeronTopology)1 BaseBasicBolt (org.apache.heron.api.bolt.BaseBasicBolt)1 BasicOutputCollector (org.apache.heron.api.bolt.BasicOutputCollector)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 BaseRichSpout (org.apache.heron.api.spout.BaseRichSpout)1 SpoutOutputCollector (org.apache.heron.api.spout.SpoutOutputCollector)1 OutputFieldsDeclarer (org.apache.heron.api.topology.OutputFieldsDeclarer)1 TopologyBuilder (org.apache.heron.api.topology.TopologyBuilder)1 TopologyContext (org.apache.heron.api.topology.TopologyContext)1 Tuple (org.apache.heron.api.tuple.Tuple)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