Jackie-Jiang commented on code in PR #8658: URL: https://github.com/apache/pinot/pull/8658#discussion_r869673912
########## pinot-plugins/pinot-input-format/pinot-csv/src/main/java/org/apache/pinot/plugin/inputformat/csv/CSVMessageDecoder.java: ########## @@ -0,0 +1,133 @@ +/** + * 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.pinot.plugin.inputformat.csv; + +import com.google.common.collect.ImmutableSet; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Iterator; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVRecord; +import org.apache.commons.lang.StringUtils; +import org.apache.pinot.spi.data.readers.GenericRow; +import org.apache.pinot.spi.stream.StreamMessageDecoder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class CSVMessageDecoder implements StreamMessageDecoder<byte[]> { + + private static final Logger LOGGER = LoggerFactory.getLogger(CSVMessageDecoder.class); + + private static final String CONFIG_FILE_FORMAT = "fileFormat"; + private static final String CONFIG_HEADER = "header"; + private static final String CONFIG_DELIMITER = "delimiter"; + private static final String CONFIG_COMMENT_MARKER = "commentMarker"; + private static final String CONFIG_CSV_ESCAPE_CHARACTER = "escapeCharacter"; + private static final String CONFIG_CSV_MULTI_VALUE_DELIMITER = "multiValueDelimiter"; + + private CSVFormat _format; + private CSVRecordExtractor _recordExtractor; + + @Override + public void init(Map<String, String> props, Set<String> fieldsToRead, String topicName) + throws Exception { + String csvFormat = props.get(CONFIG_FILE_FORMAT); + CSVFormat format; + if (csvFormat == null) { + format = CSVFormat.DEFAULT; + } else { + switch (csvFormat.toUpperCase()) { Review Comment: Add a case for the `"DEFAULT"` ########## pinot-plugins/pinot-input-format/pinot-csv/src/main/java/org/apache/pinot/plugin/inputformat/csv/CSVMessageDecoder.java: ########## @@ -0,0 +1,133 @@ +/** + * 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.pinot.plugin.inputformat.csv; + +import com.google.common.collect.ImmutableSet; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Iterator; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVRecord; +import org.apache.commons.lang.StringUtils; +import org.apache.pinot.spi.data.readers.GenericRow; +import org.apache.pinot.spi.stream.StreamMessageDecoder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class CSVMessageDecoder implements StreamMessageDecoder<byte[]> { + + private static final Logger LOGGER = LoggerFactory.getLogger(CSVMessageDecoder.class); + + private static final String CONFIG_FILE_FORMAT = "fileFormat"; + private static final String CONFIG_HEADER = "header"; + private static final String CONFIG_DELIMITER = "delimiter"; + private static final String CONFIG_COMMENT_MARKER = "commentMarker"; + private static final String CONFIG_CSV_ESCAPE_CHARACTER = "escapeCharacter"; + private static final String CONFIG_CSV_MULTI_VALUE_DELIMITER = "multiValueDelimiter"; + + private CSVFormat _format; + private CSVRecordExtractor _recordExtractor; + + @Override + public void init(Map<String, String> props, Set<String> fieldsToRead, String topicName) + throws Exception { + String csvFormat = props.get(CONFIG_FILE_FORMAT); + CSVFormat format; + if (csvFormat == null) { + format = CSVFormat.DEFAULT; + } else { + switch (csvFormat.toUpperCase()) { + case "EXCEL": + format = CSVFormat.EXCEL; + break; + case "MYSQL": + format = CSVFormat.MYSQL; + break; + case "RFC4180": + format = CSVFormat.RFC4180; + break; + case "TDF": + format = CSVFormat.TDF; + break; + default: + LOGGER.info("Could not recognise the configured CSV file format: {}, falling back to DEFAULT format", + csvFormat); + format = CSVFormat.DEFAULT; + break; + } + } + + String csvDelimiter = props.get(CONFIG_DELIMITER); + if (csvDelimiter != null) { + format = format.withDelimiter(csvDelimiter.charAt(0)); + } + String csvHeader = props.get(CONFIG_HEADER); + if (csvHeader == null) { + //parse the header automatically from the input + format = format.withHeader(); + } else { + format = format.withHeader(StringUtils.split(csvHeader, csvDelimiter)); + } + if (props.containsKey(CONFIG_COMMENT_MARKER)) { Review Comment: (minor) Follow the same way aw `csvDelimiter` to reduce one map lookup (get and check if it is `null`), same for other configs ########## pinot-plugins/pinot-input-format/pinot-csv/src/main/java/org/apache/pinot/plugin/inputformat/csv/CSVMessageDecoder.java: ########## @@ -0,0 +1,133 @@ +/** + * 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.pinot.plugin.inputformat.csv; + +import com.google.common.collect.ImmutableSet; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Iterator; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVRecord; +import org.apache.commons.lang.StringUtils; +import org.apache.pinot.spi.data.readers.GenericRow; +import org.apache.pinot.spi.stream.StreamMessageDecoder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class CSVMessageDecoder implements StreamMessageDecoder<byte[]> { + + private static final Logger LOGGER = LoggerFactory.getLogger(CSVMessageDecoder.class); + + private static final String CONFIG_FILE_FORMAT = "fileFormat"; + private static final String CONFIG_HEADER = "header"; + private static final String CONFIG_DELIMITER = "delimiter"; + private static final String CONFIG_COMMENT_MARKER = "commentMarker"; + private static final String CONFIG_CSV_ESCAPE_CHARACTER = "escapeCharacter"; + private static final String CONFIG_CSV_MULTI_VALUE_DELIMITER = "multiValueDelimiter"; + + private CSVFormat _format; + private CSVRecordExtractor _recordExtractor; + + @Override + public void init(Map<String, String> props, Set<String> fieldsToRead, String topicName) + throws Exception { + String csvFormat = props.get(CONFIG_FILE_FORMAT); + CSVFormat format; + if (csvFormat == null) { + format = CSVFormat.DEFAULT; + } else { + switch (csvFormat.toUpperCase()) { + case "EXCEL": + format = CSVFormat.EXCEL; + break; + case "MYSQL": + format = CSVFormat.MYSQL; + break; + case "RFC4180": + format = CSVFormat.RFC4180; + break; + case "TDF": + format = CSVFormat.TDF; + break; + default: + LOGGER.info("Could not recognise the configured CSV file format: {}, falling back to DEFAULT format", Review Comment: Log `warning` for unrecognized format ########## pinot-plugins/pinot-input-format/pinot-csv/src/main/java/org/apache/pinot/plugin/inputformat/csv/CSVMessageDecoder.java: ########## @@ -0,0 +1,133 @@ +/** + * 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.pinot.plugin.inputformat.csv; + +import com.google.common.collect.ImmutableSet; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Iterator; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVRecord; +import org.apache.commons.lang.StringUtils; +import org.apache.pinot.spi.data.readers.GenericRow; +import org.apache.pinot.spi.stream.StreamMessageDecoder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class CSVMessageDecoder implements StreamMessageDecoder<byte[]> { + + private static final Logger LOGGER = LoggerFactory.getLogger(CSVMessageDecoder.class); + + private static final String CONFIG_FILE_FORMAT = "fileFormat"; + private static final String CONFIG_HEADER = "header"; + private static final String CONFIG_DELIMITER = "delimiter"; + private static final String CONFIG_COMMENT_MARKER = "commentMarker"; + private static final String CONFIG_CSV_ESCAPE_CHARACTER = "escapeCharacter"; + private static final String CONFIG_CSV_MULTI_VALUE_DELIMITER = "multiValueDelimiter"; + + private CSVFormat _format; + private CSVRecordExtractor _recordExtractor; + + @Override + public void init(Map<String, String> props, Set<String> fieldsToRead, String topicName) + throws Exception { + String csvFormat = props.get(CONFIG_FILE_FORMAT); + CSVFormat format; + if (csvFormat == null) { + format = CSVFormat.DEFAULT; + } else { + switch (csvFormat.toUpperCase()) { + case "EXCEL": + format = CSVFormat.EXCEL; + break; + case "MYSQL": + format = CSVFormat.MYSQL; + break; + case "RFC4180": + format = CSVFormat.RFC4180; + break; + case "TDF": + format = CSVFormat.TDF; + break; + default: + LOGGER.info("Could not recognise the configured CSV file format: {}, falling back to DEFAULT format", + csvFormat); + format = CSVFormat.DEFAULT; + break; + } + } + + String csvDelimiter = props.get(CONFIG_DELIMITER); + if (csvDelimiter != null) { + format = format.withDelimiter(csvDelimiter.charAt(0)); + } + String csvHeader = props.get(CONFIG_HEADER); + if (csvHeader == null) { + //parse the header automatically from the input + format = format.withHeader(); + } else { + format = format.withHeader(StringUtils.split(csvHeader, csvDelimiter)); + } + if (props.containsKey(CONFIG_COMMENT_MARKER)) { + Character commentMarker = props.get(CONFIG_COMMENT_MARKER).charAt(0); Review Comment: (minor) no need to boxing, same for other configs -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
