Jackie-Jiang commented on code in PR #8658: URL: https://github.com/apache/pinot/pull/8658#discussion_r868263751
########## pinot-plugins/pinot-input-format/pinot-csv/src/main/java/org/apache/pinot/plugin/inputformat/csv/CSVMessageDecoder.java: ########## @@ -0,0 +1,127 @@ +/** + * 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; + + +public class CSVMessageDecoder implements StreamMessageDecoder<byte[]> { + + private static final String CONFIG_FILE_FORMAT = "csv.fileFmt"; + private static final String CONFIG_HEADER = "csv.hdr"; + private static final String CONFIG_DELIMITER = "csv.delim"; + private static final String CONFIG_COMMENT_MARKER = "csv.commentMarker"; + private static final String CONFIG_CSV_ESCAPE_CHARACTER = "csv.EscChar"; + private static final String CONFIG_CSV_MULTI_VALUE_DELIMITER = "csv.multiValDelim"; + + 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: + format = CSVFormat.DEFAULT; Review Comment: Log a warning here stating that we cannot recognize the format, and fall back to the default ########## pinot-plugins/pinot-input-format/pinot-csv/src/main/java/org/apache/pinot/plugin/inputformat/csv/CSVMessageDecoder.java: ########## @@ -0,0 +1,127 @@ +/** + * 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; + + +public class CSVMessageDecoder implements StreamMessageDecoder<byte[]> { + + private static final String CONFIG_FILE_FORMAT = "csv.fileFmt"; Review Comment: Since the properties is under the context of the decoder, we may remove the `csv.` prefix for simplicity and consistency with other decoders. Also, suggest using the full name for the key, e.g. `fileFormat`, `header`, `delimiter` etc. ########## pinot-plugins/pinot-input-format/pinot-csv/src/test/java/org/apache/pinot/plugin/inputformat/csv/CSVMessageDecoderTest.java: ########## @@ -0,0 +1,161 @@ +/** + * 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.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; +import org.apache.pinot.spi.data.readers.GenericRow; +import org.testng.Assert; +import org.testng.annotations.Test; + + +public class CSVMessageDecoderTest { + + @Test + public void testHappyCase() + throws Exception { + Map<String, String> decoderProps = getStandardDecoderProps(); + CSVMessageDecoder messageDecoder = new CSVMessageDecoder(); + messageDecoder.init(decoderProps, ImmutableSet.of("name", "age", "gender"), ""); + String incomingRecord = "Alice;18;F"; + GenericRow destination = new GenericRow(); + messageDecoder.decode(incomingRecord.getBytes(StandardCharsets.UTF_8), destination); + Assert.assertNotNull(destination.getValue("name")); Review Comment: (minor) We may static import these `Assert` methods in tests -- 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]
