将前列表对象转换为Storm

时间:2016-01-18 02:37:21

标签: java apache-storm

我正在研究Storm。

在Storm中,对于那些你可能不知道的人,它会接受任何值类型并作为对象类型发出。

我的问题是,我需要使用列表;应该能够通过索引访问每个项目。但无论如何它最终会作为一个对象被发送。

我尝试将ex-list对象转换为String类型,用“,”解析每个项目,并将其作为countermeature存储在另一个列表中。它工作正常,但看起来像一个混乱的方式。

我尝试过的另一种方法就是将对象类型转换为列表。它没有编译错误,但新列表的大小只有1,该列表中唯一的项目是我需要转换为的ex-list对象。

如何以复杂的方式将ex-list对象转换为列表?

如果这是一个少年问题,我很抱歉,并提前感谢。

为了具体通知您,以下代码段是转换必须发生的情况。

public class TridentSpoutTest implements IBatchSpout{
    emitBatch(...) {
        List<String> list = new ArrayList<String>();
        list.add("This");
        list.add("is");
        list.add("test");

        collector.emit(new Values(list));
    }
}

public class TridentFuncTest extends BaseFunction {
    public void execute(TridentTuple tuple, TridentCollector collector){
         OrdinaryClass.put(tuple.getValueByField("data"));   
         //getValueByField returns value in Object type
    }
}

public class OrdinaryClass {
     public void put(Object o) {
         //How to convert o back to list?
     }
}

1 个答案:

答案 0 :(得分:0)

只要你的list-type实现public static IDbConnection GetConnection(IDbConnection connection) { connection.ConnectionString = "connectionString; Connection Timeout=2;"; connection.Open(); return connection; } ,就没有问题。让我们假设一个Spout发出一个列表,一个螺栓接收它:

Serializable

在你的情况下,你需要使用正确的类型并添加一个演员(类似于我的例子):

// Spout.nextTuple()
public void  nextTuple() {
    List<String> l = new ArrayList<String();
    l.add("a");
    l.add("b");
    l.add("c");
    // l gets stored at index zero
    collector.emit(new Values(l));
}

// Bolt.execute()
public void execute(Tuple input) {
    // receive from index zero and cast
    // (cast to ArrayList<String> would also work)
    List<String> l = (List<Array>)input.getValue(0));
    // you can access "a", "b", and "c" via l.get(0), l.get(1), l.get(2)
}