You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@inlong.apache.org by "luchunliang (via GitHub)" <gi...@apache.org> on 2024/04/28 12:39:47 UTC
[PR] [INLONG-10093][SDK] Support to transform from CSV protocol to CSV/KV protocol by single SQL [inlong]
luchunliang opened a new pull request, #10097:
URL: https://github.com/apache/inlong/pull/10097
Fixes #10093
Parent issue: #10023
### Motivation
<!--Explain here the context, and why you're making that change. What is the problem you're trying to solve.-->
### Modifications
<!--Describe the modifications you've done.-->
### Verifying this change
*(Please pick either of the following options)*
- [ ] This change is a trivial rework/code cleanup without any test coverage.
- [ ] This change is already covered by existing tests, such as:
*(please describe tests)*
- [ ] This change added tests and can be verified as follows:
*(example:)*
- *Added integration tests for end-to-end deployment with large payloads (10MB)*
- *Extended integration test for recovery after broker failure*
### Documentation
- Does this pull request introduce a new feature? (yes / no)
- If yes, how is the feature documented? (not applicable / docs / JavaDocs / not documented)
- If a feature is not applicable for documentation, explain why?
- If a feature is not documented yet in this PR, please create a follow-up issue for adding the documentation
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@inlong.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [INLONG-10093][SDK] Support to transform from CSV protocol to CSV/KV protocol by single SQL [inlong]
Posted by "luchunliang (via GitHub)" <gi...@apache.org>.
luchunliang commented on code in PR #10097:
URL: https://github.com/apache/inlong/pull/10097#discussion_r1582753620
##########
inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/decode/KvSourceDecoder.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.inlong.sdk.transform.decode;
+
+import org.apache.inlong.sdk.transform.pojo.FieldInfo;
+import org.apache.inlong.sdk.transform.pojo.KvSourceInfo;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.nio.charset.Charset;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * KvSourceDecoder
+ *
+ */
+public class KvSourceDecoder implements SourceDecoder {
+
+ protected KvSourceInfo sourceInfo;
+ private Charset srcCharset = Charset.defaultCharset();
+ private List<FieldInfo> fields;
+
+ public KvSourceDecoder(KvSourceInfo sourceInfo) {
+ this.sourceInfo = sourceInfo;
+ if (!StringUtils.isBlank(sourceInfo.getCharset())) {
+ this.srcCharset = Charset.forName(sourceInfo.getCharset());
+ }
+ this.fields = sourceInfo.getFields();
+ }
+
+ @Override
+ public SourceData decode(byte[] srcBytes, Map<String, Object> extParams) {
+ String srcString = new String(srcBytes, srcCharset);
+ return this.decode(srcString, extParams);
+ }
+
+ @Override
+ public SourceData decode(String srcString, Map<String, Object> extParams) {
+ List<Map<String, String>> rowValues = KvUtils.splitKv(srcString, '&', '=', '\\', '\"', '\n');
Review Comment:
It is fixed for KV format.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@inlong.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [INLONG-10093][SDK] Support to transform from CSV protocol to CSV/KV protocol by single SQL [inlong]
Posted by "justinwwhuang (via GitHub)" <gi...@apache.org>.
justinwwhuang commented on code in PR #10097:
URL: https://github.com/apache/inlong/pull/10097#discussion_r1582721332
##########
inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/decode/SplitUtils.java:
##########
@@ -0,0 +1,192 @@
+/*
+ * 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.inlong.sdk.transform.decode;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * SplitUtils
+ *
+ */
+public class SplitUtils {
+
+ public static final int STATE_NORMAL = 0;
+ public static final int STATE_KEY = 2;
+ public static final int STATE_VALUE = 4;
+ public static final int STATE_ESCAPING = 8;
+ public static final int STATE_QUOTING = 16;
+
+ public static String[] splitCsv(@Nonnull String text, @Nonnull Character delimiter, @Nullable Character escapeChar,
+ @Nullable Character quoteChar, boolean hasEscapeProcess) {
+ String[][] splitResult = splitCsv(text, delimiter, escapeChar, quoteChar, null, hasEscapeProcess);
+ if (splitResult.length == 0) {
+ return new String[0];
+ }
+ return splitResult[0];
+ }
+
+ public static String[][] splitCsv(@Nonnull String text, @Nonnull Character delimiter,
+ @Nullable Character escapeChar, @Nullable Character quoteChar, @Nullable Character lineDelimiter,
+ boolean hasEscapeProcess) {
+ return splitCsv(text, delimiter, escapeChar, quoteChar, lineDelimiter, false, hasEscapeProcess);
+ }
+
+ public static String[][] splitCsv(@Nonnull String text, @Nonnull Character delimiter,
+ @Nullable Character escapeChar, @Nullable Character quoteChar, @Nullable Character lineDelimiter,
+ boolean deleteHeadDelimiter, boolean hasEscapeProcess) {
+ char deli = delimiter.charValue();
+ char escape = (escapeChar == null) ? '\\' : escapeChar.charValue();
+ char quote = (quoteChar == null) ? '\"' : quoteChar.charValue();
+ char line = (lineDelimiter == null) ? '\n' : lineDelimiter.charValue();
+ List<String[]> lines = new ArrayList<>();
+ List<String> fields = new ArrayList<>();
+
+ // StringBuilder stringBuilder = STRING_BUILDER.get();
+ // if (stringBuilder == null) {
+ // stringBuilder = new StringBuilder();
+ // STRING_BUILDER.set(stringBuilder);
+ // }
+ // stringBuilder.delete(0, stringBuilder.length());
+ int state = STATE_NORMAL;
+
+ char[] srcValue = text.toCharArray();
+ char[] fieldValue = new char[srcValue.length];
+ int fieldIndex = 0;
+ for (int i = 0; i < text.length(); i++) {
+ char ch = srcValue[i];
+
+ if (ch == deli) {
+ switch (state) {
+ case STATE_NORMAL:
+ if (fieldIndex == 0 && deleteHeadDelimiter && fields.isEmpty()) {
+ break;
+ }
+ fields.add(new String(fieldValue, 0, fieldIndex));
+ fieldIndex = 0;
+ break;
+ case STATE_ESCAPING:
+ fieldValue[fieldIndex++] = ch;
+ state = STATE_NORMAL;
+ break;
+ case STATE_QUOTING:
+ fieldValue[fieldIndex++] = ch;
+ break;
+ default:
+ break;
+ }
+ } else if (escapeChar != null && ch == escape) {
+ switch (state) {
+ case STATE_NORMAL:
+ state = STATE_ESCAPING;
+ break;
+ case STATE_ESCAPING:
+ if (!hasEscapeProcess) {
+ fieldValue[fieldIndex++] = escapeChar;
+ }
+ fieldValue[fieldIndex++] = ch;
+ state = STATE_NORMAL;
+ break;
+ case STATE_QUOTING:
+ fieldValue[fieldIndex++] = ch;
+ break;
+ default:
+ break;
+ }
+ } else if (quoteChar != null && ch == quote) {
+ switch (state) {
+ case STATE_NORMAL:
+ state = STATE_QUOTING;
+ break;
+ case STATE_ESCAPING:
+ if (!hasEscapeProcess) {
+ fieldValue[fieldIndex++] = escapeChar;
+ }
+ fieldValue[fieldIndex++] = ch;
+ state = STATE_NORMAL;
+ break;
+ case STATE_QUOTING:
+ state = STATE_NORMAL;
+ break;
+ default:
+ break;
+ }
+ } else if (lineDelimiter != null && ch == line) {
+ switch (state) {
+ case STATE_NORMAL:
+ fields.add(new String(fieldValue, 0, fieldIndex));
+ fieldIndex = 0;
+ lines.add(fields.toArray(new String[0]));
+ fields.clear();
+ break;
+ case STATE_ESCAPING:
+ fieldValue[fieldIndex++] = ch;
+ state = STATE_NORMAL;
+ break;
+ case STATE_QUOTING:
+ fieldValue[fieldIndex++] = ch;
+ break;
+ default:
+ break;
+ }
+ } else {
+ if (state == STATE_ESCAPING) {
+ if (!hasEscapeProcess) {
+ fieldValue[fieldIndex++] = escapeChar;
+ }
+ state = STATE_NORMAL;
+ }
+ fieldValue[fieldIndex++] = ch;
+ }
+ }
+ fields.add(new String(fieldValue, 0, fieldIndex));
+ fieldIndex = 0;
+ lines.add(fields.toArray(new String[0]));
+
+ String[][] result = new String[lines.size()][];
+ for (int i = 0; i < lines.size(); i++) {
+ result[i] = lines.get(i);
+ }
+ return result;
+ // switch (state) {
Review Comment:
Unuseful code, please delete it
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@inlong.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [INLONG-10093][SDK] Support to transform from CSV protocol to CSV/KV protocol by single SQL [inlong]
Posted by "vernedeng (via GitHub)" <gi...@apache.org>.
vernedeng merged PR #10097:
URL: https://github.com/apache/inlong/pull/10097
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@inlong.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [INLONG-10093][SDK] Support to transform from CSV protocol to CSV/KV protocol by single SQL [inlong]
Posted by "justinwwhuang (via GitHub)" <gi...@apache.org>.
justinwwhuang commented on code in PR #10097:
URL: https://github.com/apache/inlong/pull/10097#discussion_r1582512300
##########
inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/TransformProcessor.java:
##########
@@ -0,0 +1,145 @@
+/*
+ * 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.inlong.sdk.transform.process;
+
+import org.apache.inlong.sdk.transform.decode.CsvSourceDecoder;
+import org.apache.inlong.sdk.transform.decode.KvSourceDecoder;
+import org.apache.inlong.sdk.transform.decode.SourceData;
+import org.apache.inlong.sdk.transform.decode.SourceDecoder;
+import org.apache.inlong.sdk.transform.encode.CsvSinkEncoder;
+import org.apache.inlong.sdk.transform.encode.DefaultSinkData;
+import org.apache.inlong.sdk.transform.encode.KvSinkEncoder;
+import org.apache.inlong.sdk.transform.encode.SinkData;
+import org.apache.inlong.sdk.transform.encode.SinkEncoder;
+import org.apache.inlong.sdk.transform.pojo.CsvSinkInfo;
+import org.apache.inlong.sdk.transform.pojo.CsvSourceInfo;
+import org.apache.inlong.sdk.transform.pojo.KvSinkInfo;
+import org.apache.inlong.sdk.transform.pojo.KvSourceInfo;
+import org.apache.inlong.sdk.transform.pojo.SinkInfo;
+import org.apache.inlong.sdk.transform.pojo.SourceInfo;
+import org.apache.inlong.sdk.transform.pojo.TransformConfig;
+import org.apache.inlong.sdk.transform.process.operator.ExpressionOperator;
+import org.apache.inlong.sdk.transform.process.operator.OperatorTools;
+import org.apache.inlong.sdk.transform.process.parser.ValueParser;
+
+import net.sf.jsqlparser.JSQLParserException;
+import net.sf.jsqlparser.parser.CCJSqlParserManager;
+import net.sf.jsqlparser.statement.select.PlainSelect;
+import net.sf.jsqlparser.statement.select.Select;
+import net.sf.jsqlparser.statement.select.SelectExpressionItem;
+import net.sf.jsqlparser.statement.select.SelectItem;
+import org.apache.commons.lang3.StringUtils;
+
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+/**
+ * TransformProcessor
+ *
+ */
+public class TransformProcessor {
+
+ private TransformConfig config;
+ private SourceDecoder decoder;
+ private SinkEncoder encoder;
+ private Charset srcCharset = Charset.defaultCharset();
+ protected Charset sinkCharset = Charset.defaultCharset();
+
+ private PlainSelect transformSelect;
+ private ExpressionOperator where;
+ private Map<String, ValueParser> selectItemMap;
+
+ public TransformProcessor(TransformConfig config) throws JSQLParserException {
Review Comment:
Does this mean that the user needs to construct the TransformConfig properly? Can it be changed to passing in a JSON string
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@inlong.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [INLONG-10093][SDK] Support to transform from CSV protocol to CSV/KV protocol by single SQL [inlong]
Posted by "justinwwhuang (via GitHub)" <gi...@apache.org>.
justinwwhuang commented on code in PR #10097:
URL: https://github.com/apache/inlong/pull/10097#discussion_r1582660491
##########
inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/TransformProcessor.java:
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.inlong.sdk.transform.process;
+
+import org.apache.inlong.sdk.transform.decode.CsvSourceDecoder;
+import org.apache.inlong.sdk.transform.decode.KvSourceDecoder;
+import org.apache.inlong.sdk.transform.decode.SourceData;
+import org.apache.inlong.sdk.transform.decode.SourceDecoder;
+import org.apache.inlong.sdk.transform.encode.CsvSinkEncoder;
+import org.apache.inlong.sdk.transform.encode.DefaultSinkData;
+import org.apache.inlong.sdk.transform.encode.KvSinkEncoder;
+import org.apache.inlong.sdk.transform.encode.SinkData;
+import org.apache.inlong.sdk.transform.encode.SinkEncoder;
+import org.apache.inlong.sdk.transform.pojo.CsvSinkInfo;
+import org.apache.inlong.sdk.transform.pojo.CsvSourceInfo;
+import org.apache.inlong.sdk.transform.pojo.KvSinkInfo;
+import org.apache.inlong.sdk.transform.pojo.KvSourceInfo;
+import org.apache.inlong.sdk.transform.pojo.SinkInfo;
+import org.apache.inlong.sdk.transform.pojo.SourceInfo;
+import org.apache.inlong.sdk.transform.pojo.TransformConfig;
+import org.apache.inlong.sdk.transform.process.operator.ExpressionOperator;
+import org.apache.inlong.sdk.transform.process.operator.OperatorTools;
+import org.apache.inlong.sdk.transform.process.parser.ValueParser;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import net.sf.jsqlparser.JSQLParserException;
+import net.sf.jsqlparser.parser.CCJSqlParserManager;
+import net.sf.jsqlparser.statement.select.PlainSelect;
+import net.sf.jsqlparser.statement.select.Select;
+import net.sf.jsqlparser.statement.select.SelectExpressionItem;
+import net.sf.jsqlparser.statement.select.SelectItem;
+import org.apache.commons.lang3.StringUtils;
+
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+/**
+ * TransformProcessor
+ *
+ */
+public class TransformProcessor {
+
+ private TransformConfig config;
+ private SourceDecoder decoder;
+ private SinkEncoder encoder;
+ private Charset srcCharset = Charset.defaultCharset();
+ protected Charset sinkCharset = Charset.defaultCharset();
+
+ private PlainSelect transformSelect;
+ private ExpressionOperator where;
+ private Map<String, ValueParser> selectItemMap;
+
+ private ObjectMapper objectMapper = new ObjectMapper();
+
+ public TransformProcessor(String configString)
+ throws JsonMappingException, JsonProcessingException, JSQLParserException {
+ TransformConfig config = this.objectMapper.readValue(configString, TransformConfig.class);
+ this.init(config);
+ }
+
+ public TransformProcessor(TransformConfig config) throws JSQLParserException {
+ this.init(config);
+ }
+
+ private void init(TransformConfig config) throws JSQLParserException {
+ this.config = config;
+ if (!StringUtils.isBlank(config.getSourceInfo().getCharset())) {
+ this.srcCharset = Charset.forName(config.getSourceInfo().getCharset());
+ }
+ if (!StringUtils.isBlank(config.getSinkInfo().getCharset())) {
+ this.sinkCharset = Charset.forName(config.getSinkInfo().getCharset());
+ }
+ this.initDecoder(config);
+ this.initEncoder(config);
+ this.initTransformSql();
+ }
+
+ private void initDecoder(TransformConfig config) {
+ SourceInfo sourceInfo = config.getSourceInfo();
+ if (sourceInfo instanceof CsvSourceInfo) {
+ this.decoder = new CsvSourceDecoder((CsvSourceInfo) sourceInfo);
+ } else if (sourceInfo instanceof KvSourceInfo) {
+ this.decoder = new KvSourceDecoder((KvSourceInfo) sourceInfo);
+ }
+ }
+
+ private void initEncoder(TransformConfig config) {
+ SinkInfo sinkInfo = config.getSinkInfo();
+ if (sinkInfo instanceof CsvSinkInfo) {
+ this.encoder = new CsvSinkEncoder((CsvSinkInfo) sinkInfo);
+ } else if (sinkInfo instanceof KvSinkInfo) {
+ this.encoder = new KvSinkEncoder((KvSinkInfo) sinkInfo);
+ }
+ }
+
+ private void initTransformSql() throws JSQLParserException {
+ CCJSqlParserManager parserManager = new CCJSqlParserManager();
+ Select select = (Select) parserManager.parse(new StringReader(config.getTransformSql()));
+ this.transformSelect = (PlainSelect) select.getSelectBody();
+ this.where = OperatorTools.buildOperator(this.transformSelect.getWhere());
+ List<SelectItem> items = this.transformSelect.getSelectItems();
+ this.selectItemMap = new HashMap<>(items.size());
+ for (SelectItem item : items) {
+ if (item instanceof SelectExpressionItem) {
+ SelectExpressionItem exprItem = (SelectExpressionItem) item;
+ if (exprItem.getAlias() == null) {
+ this.selectItemMap.put(exprItem.toString(),
+ OperatorTools.buildParser(exprItem.getExpression()));
+ } else {
+ this.selectItemMap.put(exprItem.getAlias().getName(),
+ OperatorTools.buildParser(exprItem.getExpression()));
+ }
+ }
+ }
+ }
+
+ public List<String> transform(byte[] srcBytes, Map<String, Object> extParams) {
+ SourceData sourceData = this.decoder.decode(srcBytes, extParams);
+ List<String> sinkDatas = new ArrayList<>(sourceData.getRowCount());
+ for (int i = 1; i <= sourceData.getRowCount(); i++) {
+ if (!this.where.check(sourceData, i)) {
Review Comment:
'where' maybe null here
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@inlong.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [INLONG-10093][SDK] Support to transform from CSV protocol to CSV/KV protocol by single SQL [inlong]
Posted by "luchunliang (via GitHub)" <gi...@apache.org>.
luchunliang commented on code in PR #10097:
URL: https://github.com/apache/inlong/pull/10097#discussion_r1582575087
##########
inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/TransformProcessor.java:
##########
@@ -0,0 +1,145 @@
+/*
+ * 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.inlong.sdk.transform.process;
+
+import org.apache.inlong.sdk.transform.decode.CsvSourceDecoder;
+import org.apache.inlong.sdk.transform.decode.KvSourceDecoder;
+import org.apache.inlong.sdk.transform.decode.SourceData;
+import org.apache.inlong.sdk.transform.decode.SourceDecoder;
+import org.apache.inlong.sdk.transform.encode.CsvSinkEncoder;
+import org.apache.inlong.sdk.transform.encode.DefaultSinkData;
+import org.apache.inlong.sdk.transform.encode.KvSinkEncoder;
+import org.apache.inlong.sdk.transform.encode.SinkData;
+import org.apache.inlong.sdk.transform.encode.SinkEncoder;
+import org.apache.inlong.sdk.transform.pojo.CsvSinkInfo;
+import org.apache.inlong.sdk.transform.pojo.CsvSourceInfo;
+import org.apache.inlong.sdk.transform.pojo.KvSinkInfo;
+import org.apache.inlong.sdk.transform.pojo.KvSourceInfo;
+import org.apache.inlong.sdk.transform.pojo.SinkInfo;
+import org.apache.inlong.sdk.transform.pojo.SourceInfo;
+import org.apache.inlong.sdk.transform.pojo.TransformConfig;
+import org.apache.inlong.sdk.transform.process.operator.ExpressionOperator;
+import org.apache.inlong.sdk.transform.process.operator.OperatorTools;
+import org.apache.inlong.sdk.transform.process.parser.ValueParser;
+
+import net.sf.jsqlparser.JSQLParserException;
+import net.sf.jsqlparser.parser.CCJSqlParserManager;
+import net.sf.jsqlparser.statement.select.PlainSelect;
+import net.sf.jsqlparser.statement.select.Select;
+import net.sf.jsqlparser.statement.select.SelectExpressionItem;
+import net.sf.jsqlparser.statement.select.SelectItem;
+import org.apache.commons.lang3.StringUtils;
+
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+/**
+ * TransformProcessor
+ *
+ */
+public class TransformProcessor {
+
+ private TransformConfig config;
+ private SourceDecoder decoder;
+ private SinkEncoder encoder;
+ private Charset srcCharset = Charset.defaultCharset();
+ protected Charset sinkCharset = Charset.defaultCharset();
+
+ private PlainSelect transformSelect;
+ private ExpressionOperator where;
+ private Map<String, ValueParser> selectItemMap;
+
+ public TransformProcessor(TransformConfig config) throws JSQLParserException {
Review Comment:
Fixed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@inlong.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [INLONG-10093][SDK] Support to transform from CSV protocol to CSV/KV protocol by single SQL [inlong]
Posted by "justinwwhuang (via GitHub)" <gi...@apache.org>.
justinwwhuang commented on code in PR #10097:
URL: https://github.com/apache/inlong/pull/10097#discussion_r1582705158
##########
inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/decode/KvSourceDecoder.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.inlong.sdk.transform.decode;
+
+import org.apache.inlong.sdk.transform.pojo.FieldInfo;
+import org.apache.inlong.sdk.transform.pojo.KvSourceInfo;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.nio.charset.Charset;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * KvSourceDecoder
+ *
+ */
+public class KvSourceDecoder implements SourceDecoder {
+
+ protected KvSourceInfo sourceInfo;
+ private Charset srcCharset = Charset.defaultCharset();
+ private List<FieldInfo> fields;
+
+ public KvSourceDecoder(KvSourceInfo sourceInfo) {
+ this.sourceInfo = sourceInfo;
+ if (!StringUtils.isBlank(sourceInfo.getCharset())) {
+ this.srcCharset = Charset.forName(sourceInfo.getCharset());
+ }
+ this.fields = sourceInfo.getFields();
+ }
+
+ @Override
+ public SourceData decode(byte[] srcBytes, Map<String, Object> extParams) {
+ String srcString = new String(srcBytes, srcCharset);
+ return this.decode(srcString, extParams);
+ }
+
+ @Override
+ public SourceData decode(String srcString, Map<String, Object> extParams) {
+ List<Map<String, String>> rowValues = KvUtils.splitKv(srcString, '&', '=', '\\', '\"', '\n');
Review Comment:
Are these characters fixed?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@inlong.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [INLONG-10093][SDK] Support to transform from CSV protocol to CSV/KV protocol by single SQL [inlong]
Posted by "luchunliang (via GitHub)" <gi...@apache.org>.
luchunliang commented on code in PR #10097:
URL: https://github.com/apache/inlong/pull/10097#discussion_r1582663790
##########
inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/TransformProcessor.java:
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.inlong.sdk.transform.process;
+
+import org.apache.inlong.sdk.transform.decode.CsvSourceDecoder;
+import org.apache.inlong.sdk.transform.decode.KvSourceDecoder;
+import org.apache.inlong.sdk.transform.decode.SourceData;
+import org.apache.inlong.sdk.transform.decode.SourceDecoder;
+import org.apache.inlong.sdk.transform.encode.CsvSinkEncoder;
+import org.apache.inlong.sdk.transform.encode.DefaultSinkData;
+import org.apache.inlong.sdk.transform.encode.KvSinkEncoder;
+import org.apache.inlong.sdk.transform.encode.SinkData;
+import org.apache.inlong.sdk.transform.encode.SinkEncoder;
+import org.apache.inlong.sdk.transform.pojo.CsvSinkInfo;
+import org.apache.inlong.sdk.transform.pojo.CsvSourceInfo;
+import org.apache.inlong.sdk.transform.pojo.KvSinkInfo;
+import org.apache.inlong.sdk.transform.pojo.KvSourceInfo;
+import org.apache.inlong.sdk.transform.pojo.SinkInfo;
+import org.apache.inlong.sdk.transform.pojo.SourceInfo;
+import org.apache.inlong.sdk.transform.pojo.TransformConfig;
+import org.apache.inlong.sdk.transform.process.operator.ExpressionOperator;
+import org.apache.inlong.sdk.transform.process.operator.OperatorTools;
+import org.apache.inlong.sdk.transform.process.parser.ValueParser;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import net.sf.jsqlparser.JSQLParserException;
+import net.sf.jsqlparser.parser.CCJSqlParserManager;
+import net.sf.jsqlparser.statement.select.PlainSelect;
+import net.sf.jsqlparser.statement.select.Select;
+import net.sf.jsqlparser.statement.select.SelectExpressionItem;
+import net.sf.jsqlparser.statement.select.SelectItem;
+import org.apache.commons.lang3.StringUtils;
+
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+/**
+ * TransformProcessor
+ *
+ */
+public class TransformProcessor {
+
+ private TransformConfig config;
+ private SourceDecoder decoder;
+ private SinkEncoder encoder;
+ private Charset srcCharset = Charset.defaultCharset();
+ protected Charset sinkCharset = Charset.defaultCharset();
+
+ private PlainSelect transformSelect;
+ private ExpressionOperator where;
+ private Map<String, ValueParser> selectItemMap;
+
+ private ObjectMapper objectMapper = new ObjectMapper();
+
+ public TransformProcessor(String configString)
+ throws JsonMappingException, JsonProcessingException, JSQLParserException {
+ TransformConfig config = this.objectMapper.readValue(configString, TransformConfig.class);
+ this.init(config);
+ }
+
+ public TransformProcessor(TransformConfig config) throws JSQLParserException {
+ this.init(config);
+ }
+
+ private void init(TransformConfig config) throws JSQLParserException {
+ this.config = config;
+ if (!StringUtils.isBlank(config.getSourceInfo().getCharset())) {
+ this.srcCharset = Charset.forName(config.getSourceInfo().getCharset());
+ }
+ if (!StringUtils.isBlank(config.getSinkInfo().getCharset())) {
+ this.sinkCharset = Charset.forName(config.getSinkInfo().getCharset());
+ }
+ this.initDecoder(config);
+ this.initEncoder(config);
+ this.initTransformSql();
+ }
+
+ private void initDecoder(TransformConfig config) {
+ SourceInfo sourceInfo = config.getSourceInfo();
+ if (sourceInfo instanceof CsvSourceInfo) {
+ this.decoder = new CsvSourceDecoder((CsvSourceInfo) sourceInfo);
+ } else if (sourceInfo instanceof KvSourceInfo) {
+ this.decoder = new KvSourceDecoder((KvSourceInfo) sourceInfo);
+ }
+ }
+
+ private void initEncoder(TransformConfig config) {
+ SinkInfo sinkInfo = config.getSinkInfo();
+ if (sinkInfo instanceof CsvSinkInfo) {
+ this.encoder = new CsvSinkEncoder((CsvSinkInfo) sinkInfo);
+ } else if (sinkInfo instanceof KvSinkInfo) {
+ this.encoder = new KvSinkEncoder((KvSinkInfo) sinkInfo);
+ }
+ }
+
+ private void initTransformSql() throws JSQLParserException {
+ CCJSqlParserManager parserManager = new CCJSqlParserManager();
+ Select select = (Select) parserManager.parse(new StringReader(config.getTransformSql()));
+ this.transformSelect = (PlainSelect) select.getSelectBody();
+ this.where = OperatorTools.buildOperator(this.transformSelect.getWhere());
+ List<SelectItem> items = this.transformSelect.getSelectItems();
+ this.selectItemMap = new HashMap<>(items.size());
+ for (SelectItem item : items) {
+ if (item instanceof SelectExpressionItem) {
+ SelectExpressionItem exprItem = (SelectExpressionItem) item;
+ if (exprItem.getAlias() == null) {
+ this.selectItemMap.put(exprItem.toString(),
+ OperatorTools.buildParser(exprItem.getExpression()));
+ } else {
+ this.selectItemMap.put(exprItem.getAlias().getName(),
+ OperatorTools.buildParser(exprItem.getExpression()));
+ }
+ }
+ }
+ }
+
+ public List<String> transform(byte[] srcBytes, Map<String, Object> extParams) {
+ SourceData sourceData = this.decoder.decode(srcBytes, extParams);
+ List<String> sinkDatas = new ArrayList<>(sourceData.getRowCount());
+ for (int i = 1; i <= sourceData.getRowCount(); i++) {
+ if (!this.where.check(sourceData, i)) {
Review Comment:
Fixed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@inlong.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [INLONG-10093][SDK] Support to transform from CSV protocol to CSV/KV protocol by single SQL [inlong]
Posted by "luchunliang (via GitHub)" <gi...@apache.org>.
luchunliang commented on code in PR #10097:
URL: https://github.com/apache/inlong/pull/10097#discussion_r1582754879
##########
inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/decode/SplitUtils.java:
##########
@@ -0,0 +1,192 @@
+/*
+ * 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.inlong.sdk.transform.decode;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * SplitUtils
+ *
+ */
+public class SplitUtils {
+
+ public static final int STATE_NORMAL = 0;
+ public static final int STATE_KEY = 2;
+ public static final int STATE_VALUE = 4;
+ public static final int STATE_ESCAPING = 8;
+ public static final int STATE_QUOTING = 16;
+
+ public static String[] splitCsv(@Nonnull String text, @Nonnull Character delimiter, @Nullable Character escapeChar,
+ @Nullable Character quoteChar, boolean hasEscapeProcess) {
+ String[][] splitResult = splitCsv(text, delimiter, escapeChar, quoteChar, null, hasEscapeProcess);
+ if (splitResult.length == 0) {
+ return new String[0];
+ }
+ return splitResult[0];
+ }
+
+ public static String[][] splitCsv(@Nonnull String text, @Nonnull Character delimiter,
+ @Nullable Character escapeChar, @Nullable Character quoteChar, @Nullable Character lineDelimiter,
+ boolean hasEscapeProcess) {
+ return splitCsv(text, delimiter, escapeChar, quoteChar, lineDelimiter, false, hasEscapeProcess);
+ }
+
+ public static String[][] splitCsv(@Nonnull String text, @Nonnull Character delimiter,
+ @Nullable Character escapeChar, @Nullable Character quoteChar, @Nullable Character lineDelimiter,
+ boolean deleteHeadDelimiter, boolean hasEscapeProcess) {
+ char deli = delimiter.charValue();
+ char escape = (escapeChar == null) ? '\\' : escapeChar.charValue();
+ char quote = (quoteChar == null) ? '\"' : quoteChar.charValue();
+ char line = (lineDelimiter == null) ? '\n' : lineDelimiter.charValue();
+ List<String[]> lines = new ArrayList<>();
+ List<String> fields = new ArrayList<>();
+
+ // StringBuilder stringBuilder = STRING_BUILDER.get();
+ // if (stringBuilder == null) {
+ // stringBuilder = new StringBuilder();
+ // STRING_BUILDER.set(stringBuilder);
+ // }
+ // stringBuilder.delete(0, stringBuilder.length());
+ int state = STATE_NORMAL;
+
+ char[] srcValue = text.toCharArray();
+ char[] fieldValue = new char[srcValue.length];
+ int fieldIndex = 0;
+ for (int i = 0; i < text.length(); i++) {
+ char ch = srcValue[i];
+
+ if (ch == deli) {
+ switch (state) {
+ case STATE_NORMAL:
+ if (fieldIndex == 0 && deleteHeadDelimiter && fields.isEmpty()) {
+ break;
+ }
+ fields.add(new String(fieldValue, 0, fieldIndex));
+ fieldIndex = 0;
+ break;
+ case STATE_ESCAPING:
+ fieldValue[fieldIndex++] = ch;
+ state = STATE_NORMAL;
+ break;
+ case STATE_QUOTING:
+ fieldValue[fieldIndex++] = ch;
+ break;
+ default:
+ break;
+ }
+ } else if (escapeChar != null && ch == escape) {
+ switch (state) {
+ case STATE_NORMAL:
+ state = STATE_ESCAPING;
+ break;
+ case STATE_ESCAPING:
+ if (!hasEscapeProcess) {
+ fieldValue[fieldIndex++] = escapeChar;
+ }
+ fieldValue[fieldIndex++] = ch;
+ state = STATE_NORMAL;
+ break;
+ case STATE_QUOTING:
+ fieldValue[fieldIndex++] = ch;
+ break;
+ default:
+ break;
+ }
+ } else if (quoteChar != null && ch == quote) {
+ switch (state) {
+ case STATE_NORMAL:
+ state = STATE_QUOTING;
+ break;
+ case STATE_ESCAPING:
+ if (!hasEscapeProcess) {
+ fieldValue[fieldIndex++] = escapeChar;
+ }
+ fieldValue[fieldIndex++] = ch;
+ state = STATE_NORMAL;
+ break;
+ case STATE_QUOTING:
+ state = STATE_NORMAL;
+ break;
+ default:
+ break;
+ }
+ } else if (lineDelimiter != null && ch == line) {
+ switch (state) {
+ case STATE_NORMAL:
+ fields.add(new String(fieldValue, 0, fieldIndex));
+ fieldIndex = 0;
+ lines.add(fields.toArray(new String[0]));
+ fields.clear();
+ break;
+ case STATE_ESCAPING:
+ fieldValue[fieldIndex++] = ch;
+ state = STATE_NORMAL;
+ break;
+ case STATE_QUOTING:
+ fieldValue[fieldIndex++] = ch;
+ break;
+ default:
+ break;
+ }
+ } else {
+ if (state == STATE_ESCAPING) {
+ if (!hasEscapeProcess) {
+ fieldValue[fieldIndex++] = escapeChar;
+ }
+ state = STATE_NORMAL;
+ }
+ fieldValue[fieldIndex++] = ch;
+ }
+ }
+ fields.add(new String(fieldValue, 0, fieldIndex));
+ fieldIndex = 0;
+ lines.add(fields.toArray(new String[0]));
+
+ String[][] result = new String[lines.size()][];
+ for (int i = 0; i < lines.size(); i++) {
+ result[i] = lines.get(i);
+ }
+ return result;
+ // switch (state) {
Review Comment:
Removed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@inlong.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org