Github user hmcl commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1808#discussion_r96751858
  
    --- Diff: 
external/storm-kafka-client/src/main/java/org/apache/storm/kafka/spout/ByTopicRecordTranslator.java
 ---
    @@ -0,0 +1,93 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + *   or more contributor license agreements.  See the NOTICE file
    + *   distributed with this work for additional information
    + *   regarding copyright ownership.  The ASF licenses this file
    + *   to you under the Apache License, Version 2.0 (the
    + *   "License"); you may not use this file except in compliance
    + *   with the License.  You may obtain a copy of the License at
    + *
    + *   http://www.apache.org/licenses/LICENSE-2.0
    + *
    + *   Unless required by applicable law or agreed to in writing, software
    + *   distributed under the License is distributed on an "AS IS" BASIS,
    + *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
implied.
    + *   See the License for the specific language governing permissions and
    + *   limitations under the License.
    + */
    +package org.apache.storm.kafka.spout;
    +
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +
    +import org.apache.kafka.clients.consumer.ConsumerRecord;
    +import org.apache.storm.tuple.Fields;
    +
    +public class ByTopicRecordTranslator<K, V> implements RecordTranslator<K, 
V> {
    +    private static final long serialVersionUID = -121699733778988688L;
    +    private final RecordTranslator<K,V> defaultTranslator;
    +    private final Map<String, RecordTranslator<K,V>> topicToTranslator = 
new HashMap<>();
    +    private final Map<String, Fields> streamToFields = new HashMap<>();
    +    
    +    public ByTopicRecordTranslator(Func<ConsumerRecord<K, V>, 
List<Object>> func, Fields fields, String stream) {
    +        this(new SimpleRecordTranslator<>(func, fields, stream));
    +    }
    +    
    +    public ByTopicRecordTranslator(Func<ConsumerRecord<K, V>, 
List<Object>> func, Fields fields) {
    +        this(new SimpleRecordTranslator<>(func, fields));
    +    }
    +    
    +    public ByTopicRecordTranslator(RecordTranslator<K,V> 
defaultTranslator) {
    +        this.defaultTranslator = defaultTranslator;
    +        cacheNCheckFields(defaultTranslator);
    +    }
    +    
    +    public ByTopicRecordTranslator<K, V> forTopic(String topic, 
Func<ConsumerRecord<K, V>, List<Object>> func, Fields fields) {
    +        return forTopic(topic, new SimpleRecordTranslator<>(func, fields));
    +    }
    +    
    +    public ByTopicRecordTranslator<K, V> forTopic(String topic, 
Func<ConsumerRecord<K, V>, List<Object>> func, Fields fields, String stream) {
    +        return forTopic(topic, new SimpleRecordTranslator<>(func, fields, 
stream));
    +    }
    +    
    +    public ByTopicRecordTranslator<K, V> forTopic(String topic, 
RecordTranslator<K,V> translator) {
    +        if (topicToTranslator.containsKey(topic)) {
    +            throw new IllegalStateException("Topic " + topic + " is 
already registered");
    +        }
    +        topicToTranslator.put(topic, translator);
    +        cacheNCheckFields(translator);
    +        return this;
    +    }
    +    
    +    private void cacheNCheckFields(RecordTranslator<K, V> translator) {
    +        for (String stream : translator.streams()) {
    +            Fields fromTrans = translator.getFieldsFor(stream);
    +            Fields cached = streamToFields.get(stream);
    +            if (cached != null && !fromTrans.equals(cached)) {
    +                throw new IllegalArgumentException("Stream " + stream + " 
currently has Fields of " + cached + " which is not the same as those being 
added in " + fromTrans);
    --- End diff --
    
    `IllegalStateException` ?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to