Flink类型擦除异常与元组问题

时间:2019-04-03 00:00:19

标签: java apache-flink type-erasure

我试图将KeyedStream与Tuple一起使用,以处理不同类型的Tuple,包括Tuple6。 不断收到异常: 线程“主”中的异常 org.apache.flink.api.common.functions.InvalidTypesException:不允许将Tuple类用作一种类型。

使用具体的子类(例如Tuple1,Tuple2等)。

在这里可以消除Type Erasure吗?

我想使用KeyedStream,以便可以将其传递给Tuple6,将其作为元组,就像MonitoringTupleKeyedStream一样。

以下代码:

KeyedStream<Monitoring, Tuple> monitoringTupleKeyedStream = null;
String keyOperationType = ....;//provided        
if (StringUtils.isNotEmpty(keyOperationType)) {
    if (keyOperationType.equalsIgnoreCase(Utils.COMPONENT_OPERATION)) {
        monitoringTupleKeyedStream = kinesisStream.keyBy("deployment", "gameId", "eventName", "component");
    } else if (keyOperationType.equalsIgnoreCase(Utils.COMPONENT_INSTANCE_OPERATION)) {
        monitoringTupleKeyedStream = kinesisStream.keyBy("deployment", "gameId", "eventName", "component", "instance");
    } else if (keyOperationType.equalsIgnoreCase(Utils.COMPONENT_KEY_OPERATION)) {
        TypeInformation<Tuple6<String, String, String, String, String, String>> info = TypeInformation.of(new TypeHint<Tuple6<String, String, String, String, String, String>>(){});
        monitoringTupleKeyedStream = kinesisStream.keyBy(new KeySelector<Monitoring, Tuple>() {
            public Tuple getKey(Monitoring mon) throws Exception {
                String key = "";
                String keyName = "";
                final String eventName = mon.getEventName();
                if (eventName != null && ((eventName.equalsIgnoreCase(INGRESS_FPS)))
                )) {
                    keyName = PCAM_ID;
                    key = mon.getEventDataMap() != null ? (String) mon.getEventDataMap().get(PCAM_ID) : "";
                } else if (eventName != null && (eventName.equalsIgnoreCase(EGRESS_FPS))) {
                    keyName = OUT_BITRATE;
                    key = mon.getEventDataMap() != null ? (String) mon.getEventDataMap().get(OUT_BITRATE) : ""; //TODO: identify key to use
                }
                mon.setKeyName(keyName);
                mon.setKeyValue(key);
                return new Tuple6<>(mon.getDeployment(), mon.getGameId(), eventName, mon.getComponent(), mon.getKeyName(), mon.getKeyValue());
            }
        }); //, info)
    } else if (keyOperationType.equalsIgnoreCase(COMPONENT_CONTAINER_OPERATION)) {
        monitoringTupleKeyedStream = kinesisStream.keyBy("deployment", "gameId", "eventName", "component", "instance", "container"); //<== this is also a Tuple6 but no complaints ?
    }
}

下面的示例需要把monitorTupleKeyedStream设置为[Monitoring,Tuple6 [String,String,String,String,String,String]]类型的KeyedStream

TypeInformation<Tuple6<String, String, String, String, String, String>> info = TypeInformation.of(new TypeHint<Tuple6<String, String, String, String, String, String>>(){});
monitoringTupleKeyedStream = kinesisStream.keyBy(new KeySelector<Monitoring, Tuple6<String, String, String, String, String, String>>() {
                    @Override
                    public Tuple6<String, String, String, String, String, String> getKey(Monitoring mon) throws Exception {
                        String key = "";
                        String keyName = "";
                        //TODO: extract to a method to pull key to use from a config file
                        final String eventName = mon.getEventName();
                        if (eventName != null && ((eventName.equalsIgnoreCase(INGRESS_FPS)))
                        )) {
                            keyName = PCAM_ID;
                            key = mon.getEventDataMap() != null ? (String) mon.getEventDataMap().get(PCAM_ID) : "";
                        } else if (eventName != null && (eventName.equalsIgnoreCase(EGRESS_FPS))) {
                            keyName = OUT_BITRATE;
                            key = mon.getEventDataMap() != null ? (String) mon.getEventDataMap().get(OUT_BITRATE) : ""; //TODO: identify key to use
                        }
                        mon.setKeyName(keyName);
                        mon.setKeyValue(key);
                        return new Tuple6<>(mon.getDeployment(), mon.getGameId(), eventName, mon.getComponent(), mon.getKeyName(), mon.getKeyValue());
                    }
                }, info);

TIA,

0 个答案:

没有答案