Search in sources :

Example 1 with CustomStreamGrouping

use of backtype.storm.grouping.CustomStreamGrouping in project jstorm by alibaba.

the class MkGrouper method parseGroupType.

private GrouperType parseGroupType(WorkerData workerData) {
    GrouperType grouperType = null;
    if (Grouping._Fields.FIELDS.equals(fields)) {
        if (Thrift.isGlobalGrouping(thrift_grouping)) {
            // global grouping, just send tuple to first task
            grouperType = GrouperType.global;
        } else {
            List<String> fields_group = Thrift.fieldGrouping(thrift_grouping);
            Fields fields = new Fields(fields_group);
            fields_grouper = new MkFieldsGrouper(out_fields, fields, out_tasks);
            // hashcode by fields
            grouperType = GrouperType.fields;
        }
    } else if (Grouping._Fields.ALL.equals(fields)) {
        // send to every task
        grouperType = GrouperType.all;
    } else if (Grouping._Fields.SHUFFLE.equals(fields)) {
        grouperType = GrouperType.shuffle;
        shuffer = new MkShuffer(topology_context.getThisComponentId(), targetComponent, workerData);
    } else if (Grouping._Fields.NONE.equals(fields)) {
        // random send one task
        this.random = new Random();
        grouperType = GrouperType.none;
    } else if (Grouping._Fields.CUSTOM_OBJECT.equals(fields)) {
        // user custom grouping by JavaObject
        JavaObject jobj = thrift_grouping.get_custom_object();
        CustomStreamGrouping g = Thrift.instantiateJavaObject(jobj);
        int myTaskId = topology_context.getThisTaskId();
        String componentId = topology_context.getComponentId(myTaskId);
        GlobalStreamId stream = new GlobalStreamId(componentId, streamId);
        custom_grouper = new MkCustomGrouper(topology_context, g, stream, out_tasks, myTaskId);
        grouperType = GrouperType.custom_obj;
    } else if (Grouping._Fields.CUSTOM_SERIALIZED.equals(fields)) {
        // user custom group by serialized Object
        byte[] obj = thrift_grouping.get_custom_serialized();
        CustomStreamGrouping g = (CustomStreamGrouping) Utils.javaDeserialize(obj);
        int myTaskId = topology_context.getThisTaskId();
        String componentId = topology_context.getComponentId(myTaskId);
        GlobalStreamId stream = new GlobalStreamId(componentId, streamId);
        custom_grouper = new MkCustomGrouper(topology_context, g, stream, out_tasks, myTaskId);
        grouperType = GrouperType.custom_serialized;
    } else if (Grouping._Fields.DIRECT.equals(fields)) {
        // directly send to a special task
        grouperType = GrouperType.direct;
    } else if (Grouping._Fields.LOCAL_OR_SHUFFLE.equals(fields)) {
        grouperType = GrouperType.shuffle;
        shuffer = new MkShuffer(topology_context.getThisComponentId(), targetComponent, workerData);
    } else if (Grouping._Fields.LOCAL_FIRST.equals(fields)) {
        grouperType = GrouperType.shuffle;
        shuffer = new MkShuffer(topology_context.getThisComponentId(), targetComponent, workerData);
    }
    return grouperType;
}
Also used : CustomStreamGrouping(backtype.storm.grouping.CustomStreamGrouping) Fields(backtype.storm.tuple.Fields) Random(java.util.Random) JavaObject(backtype.storm.generated.JavaObject) GlobalStreamId(backtype.storm.generated.GlobalStreamId)

Example 2 with CustomStreamGrouping

use of backtype.storm.grouping.CustomStreamGrouping in project jstorm by alibaba.

the class Thrift method instantiateJavaObject.

public static CustomStreamGrouping instantiateJavaObject(JavaObject obj) {
    List<JavaObjectArg> args = obj.get_args_list();
    Class[] paraTypes = new Class[args.size()];
    Object[] paraValues = new Object[args.size()];
    for (int i = 0; i < args.size(); i++) {
        JavaObjectArg arg = args.get(i);
        paraValues[i] = arg.getFieldValue();
        if (arg.getSetField().equals(JavaObjectArg._Fields.INT_ARG)) {
            paraTypes[i] = Integer.class;
        } else if (arg.getSetField().equals(JavaObjectArg._Fields.LONG_ARG)) {
            paraTypes[i] = Long.class;
        } else if (arg.getSetField().equals(JavaObjectArg._Fields.STRING_ARG)) {
            paraTypes[i] = String.class;
        } else if (arg.getSetField().equals(JavaObjectArg._Fields.BOOL_ARG)) {
            paraTypes[i] = Boolean.class;
        } else if (arg.getSetField().equals(JavaObjectArg._Fields.BINARY_ARG)) {
            paraTypes[i] = ByteBuffer.class;
        } else if (arg.getSetField().equals(JavaObjectArg._Fields.DOUBLE_ARG)) {
            paraTypes[i] = Double.class;
        } else {
            paraTypes[i] = Object.class;
        }
    }
    try {
        Class clas = Class.forName(obj.get_full_class_name());
        Constructor cons = clas.getConstructor(paraTypes);
        return (CustomStreamGrouping) cons.newInstance(paraValues);
    } catch (Exception e) {
        LOG.error("instantiate_java_object fail", e);
    }
    return null;
}
Also used : Constructor(java.lang.reflect.Constructor) CustomStreamGrouping(backtype.storm.grouping.CustomStreamGrouping)

Aggregations

CustomStreamGrouping (backtype.storm.grouping.CustomStreamGrouping)2 GlobalStreamId (backtype.storm.generated.GlobalStreamId)1 JavaObject (backtype.storm.generated.JavaObject)1 Fields (backtype.storm.tuple.Fields)1 Constructor (java.lang.reflect.Constructor)1 Random (java.util.Random)1