-
Notifications
You must be signed in to change notification settings - Fork 4.1k
STORM-1778: scheme extension framework for KafkaSourceProvider and csv s… #1410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
chuanlei
wants to merge
2
commits into
apache:master
from
chuanlei:feature-scheme-extension-framework-and-csv-support
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
external/sql/storm-sql-kafka/src/jvm/org/apache/storm/sql/kafka/CsvScheme.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * 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 | ||
| * <p> | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * 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.sql.kafka; | ||
|
|
||
| import org.apache.storm.spout.Scheme; | ||
| import org.apache.storm.sql.runtime.FieldInfo; | ||
| import org.apache.storm.tuple.Fields; | ||
|
|
||
| import java.lang.reflect.Constructor; | ||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.nio.ByteBuffer; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class CsvScheme implements Scheme { | ||
| List<FieldInfo> fieldsInfo = null; | ||
| List<String> fields = null; | ||
|
|
||
| public CsvScheme(List<FieldInfo> fieldsInfo) { | ||
| this.fieldsInfo = fieldsInfo; | ||
| fields = new ArrayList<>(); | ||
| for (FieldInfo info : fieldsInfo) { | ||
| fields.add(info.name()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public List<Object> deserialize(ByteBuffer ser) { | ||
| String str = ser.toString(); | ||
| String[] parts = str.split(","); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not standard CSV format, something like |
||
| if (parts.length != this.fields.size()) { | ||
| throw new RuntimeException("Kafka CSV message does not match the designed schema"); | ||
| } | ||
| List<Object> ret = new ArrayList<>(this.fields.size()); | ||
| for (int i = 0; i < this.fieldsInfo.size(); i++) { | ||
| FieldInfo fieldInfo = this.fieldsInfo.get(i); | ||
| String part = parts[i]; | ||
| Class<?> type = fieldInfo.type(); | ||
| if (type == String.class) { | ||
| ret.add(part); | ||
| } else { | ||
| try { | ||
| Constructor cons = type.getDeclaredConstructor(String.class); | ||
| ret.add(cons.newInstance(part)); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| @Override | ||
| public Fields getOutputFields() { | ||
| return new Fields(this.fields); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -171,12 +171,23 @@ public ISqlTridentDataSource constructTrident(URI uri, String inputFormatClass, | |
| } | ||
| } | ||
| Preconditions.checkState(primaryIndex != -1, "Kafka stream table must have a primary key"); | ||
| conf.scheme = new SchemeAsMultiScheme(new JsonScheme(fieldNames)); | ||
| ObjectMapper mapper = new ObjectMapper(); | ||
| Properties producerProp = new Properties(); | ||
| try { | ||
| @SuppressWarnings("unchecked") | ||
| HashMap<String, Object> map = mapper.readValue(properties, HashMap.class); | ||
| if(!map.containsKey("scheme")){ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please fix spaces between L179 and L187. |
||
| conf.scheme = new SchemeAsMultiScheme(new JsonScheme(fieldNames)); | ||
| }else{ | ||
| String scheme = (String)map.get("scheme"); | ||
| if(scheme.toUpperCase().equals("JSON")){ | ||
| conf.scheme = new SchemeAsMultiScheme(new JsonScheme(fieldNames)); | ||
| }else if(scheme.toUpperCase().equals("CSV")){ | ||
| conf.scheme = new SchemeAsMultiScheme(new CsvScheme(fields)); | ||
| }else{ | ||
| throw new UnsupportedOperationException(String.format("%s: Unsupport scheme",scheme)); | ||
| } | ||
| } | ||
| @SuppressWarnings("unchecked") | ||
| HashMap<String, Object> producerConfig = (HashMap<String, Object>) map.get("producer"); | ||
| Preconditions.checkNotNull(producerConfig, "Kafka Table must contain producer config"); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.