Search in sources :

Example 1 with BaseRichBolt

use of org.apache.storm.topology.base.BaseRichBolt in project main by JohnPeng739.

the class ETLTopologyBuilder method createBolts.

/**
 * 创建拓扑中的处理Bolt单元
 *
 * @param boltArray Bolt配置信息
 * @param builder   拓扑创建者对象
 */
private void createBolts(JSONArray boltArray, TopologyBuilder builder) {
    for (int index = 0; index < boltArray.size(); index++) {
        JSONObject jsonBolt = boltArray.getJSONObject(index);
        String name = jsonBolt.getString("name");
        String type = jsonBolt.getString("type");
        int parallelism = jsonBolt.getInteger("parallelism") != null ? jsonBolt.getInteger("parallelism") : 1;
        if (StringUtils.isBlank(name)) {
            String message = "Bolt configuration error, not define the name of bolt.";
            if (logger.isErrorEnabled()) {
                logger.error(message);
            }
            throw new IllegalArgumentException(message);
        }
        if (StringUtils.isBlank(type)) {
            String message = "Bolt configuration error, not define the type of bolt.";
            if (logger.isErrorEnabled()) {
                logger.error(message);
            }
            throw new IllegalArgumentException(message);
        }
        BaseRichBolt bolt = null;
        switch(type) {
            case "STRUCTURE":
                bolt = new StructureBolt();
                break;
            case "VALIDATE":
                bolt = new ValidateBolt();
                break;
            case "TRANSFORM":
                bolt = new TransformBolt();
                break;
            case "ERROR":
                bolt = new ErrorOperateBolt();
                break;
            case "JDBC":
                bolt = new JdbcBolt(jsonBolt.getJSONObject("configuration"));
                break;
            case "MONGO":
                JSONObject mongoConf = jsonBolt.getJSONObject("configuration");
                bolt = new MongoBolt(mongoConf);
                break;
            case "JMS":
                String method = jsonBolt.getString("method");
                JmsManager.Supported supported = JmsManager.Supported.valueOf(method);
                JSONObject jmsConf = jsonBolt.getJSONObject("configuration");
                try {
                    bolt = JmsManager.createJmsBolt(supported, jmsConf);
                } catch (JMSException ex) {
                    if (logger.isErrorEnabled()) {
                        logger.error(String.format("Create JmsBolt fail, name: %s, method: %s, configuration: %s.", name, method, jmsConf.toJSONString()), ex);
                    }
                    throw new IllegalArgumentException(ex.getMessage(), ex);
                }
                break;
            default:
                throw new IllegalArgumentException(String.format("Unsupported bolt's type: %s.", type));
        }
        BoltDeclarer bd = builder.setBolt(name, bolt, parallelism);
        setBoltGrouping(name, bd, jsonBolt.getJSONArray("groups"));
    }
}
Also used : JMSException(javax.jms.JMSException) BaseRichBolt(org.apache.storm.topology.base.BaseRichBolt) JSONObject(com.alibaba.fastjson.JSONObject) BoltDeclarer(org.apache.storm.topology.BoltDeclarer) JmsManager(com.ds.retl.jms.JmsManager)

Example 2 with BaseRichBolt

use of org.apache.storm.topology.base.BaseRichBolt in project main by JohnPeng739.

the class JmsManager method createJmsBolt.

/**
 * 创建一个JMS数据持久化Bolt,用于存储中间数据
 *
 * @param supported 支持的JMS类型
 * @param config    初始化配置信息
 * @return 创建好的数据持久化器Bolt
 * @throws JMSException 创建过程中发生的异常
 */
public static BaseRichBolt createJmsBolt(Supported supported, JSONObject config) throws JMSException {
    if (config == null) {
        throw new NullPointerException("The Jms config is null.");
    }
    // TODO 根据destinations的不同内容创建JmsBolt或JmsDistributeBolt
    JSONArray destinations = config.getJSONArray("destinations");
    BaseRichBolt bolt;
    if (destinations.size() > 1) {
        // 创建JmsDistributeBolt
        bolt = new JmsDistributeBolt(destinations);
        ((JmsDistributeBolt) bolt).setJmsMultiProvider(createProvider(supported, config));
    } else {
        // 创建JmsBolt
        bolt = new JmsBolt();
        ((JmsBolt) bolt).setJmsProvider(createProvider(supported, config));
        ((JmsBolt) bolt).setJmsMessageProducer(new JmsMessageProducer() {

            @Override
            public Message toMessage(Session session, ITuple input) throws JMSException {
                // 从元组中读取json字段的值,然后作为一个TextMessage发送
                JSONObject managedJson = (JSONObject) input.getValueByField("managedJson");
                JSONObject data = (JSONObject) input.getValueByField("data");
                if (managedJson == null || data == null) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("The ManagedJson or Data is null.");
                    }
                    return null;
                }
                JSONObject json = new JSONObject();
                json.put("managedJson", managedJson);
                json.put("data", data);
                return session.createTextMessage(json.toJSONString());
            }
        });
    }
    return bolt;
}
Also used : JmsMessageProducer(org.apache.storm.jms.JmsMessageProducer) JmsBolt(org.apache.storm.jms.bolt.JmsBolt) Message(javax.jms.Message) JSONObject(com.alibaba.fastjson.JSONObject) ITuple(org.apache.storm.tuple.ITuple) JSONArray(com.alibaba.fastjson.JSONArray) JmsDistributeBolt(com.ds.retl.bolt.JmsDistributeBolt) JMSException(javax.jms.JMSException) BaseRichBolt(org.apache.storm.topology.base.BaseRichBolt) Session(javax.jms.Session)

Aggregations

JSONObject (com.alibaba.fastjson.JSONObject)2 JMSException (javax.jms.JMSException)2 BaseRichBolt (org.apache.storm.topology.base.BaseRichBolt)2 JSONArray (com.alibaba.fastjson.JSONArray)1 JmsDistributeBolt (com.ds.retl.bolt.JmsDistributeBolt)1 JmsManager (com.ds.retl.jms.JmsManager)1 Message (javax.jms.Message)1 Session (javax.jms.Session)1 JmsMessageProducer (org.apache.storm.jms.JmsMessageProducer)1 JmsBolt (org.apache.storm.jms.bolt.JmsBolt)1 BoltDeclarer (org.apache.storm.topology.BoltDeclarer)1 ITuple (org.apache.storm.tuple.ITuple)1