You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2020/01/22 16:43:16 UTC

[GitHub] [nifi] mattyb149 commented on a change in pull request #3735: NIFI-6670: Add TextLineReader to read lines of text as single-field records

mattyb149 commented on a change in pull request #3735: NIFI-6670: Add TextLineReader to read lines of text as single-field records
URL: https://github.com/apache/nifi/pull/3735#discussion_r369674224
 
 

 ##########
 File path: nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/text/TextLineRecordReader.java
 ##########
 @@ -0,0 +1,125 @@
+/*
+ * 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.nifi.text;
+
+import org.apache.nifi.logging.ComponentLog;
+import org.apache.nifi.serialization.MalformedRecordException;
+import org.apache.nifi.serialization.RecordReader;
+import org.apache.nifi.serialization.SimpleRecordSchema;
+import org.apache.nifi.serialization.record.MapRecord;
+import org.apache.nifi.serialization.record.Record;
+import org.apache.nifi.serialization.record.RecordField;
+import org.apache.nifi.serialization.record.RecordFieldType;
+import org.apache.nifi.serialization.record.RecordSchema;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Collections;
+import java.util.List;
+import java.util.Scanner;
+
+public class TextLineRecordReader implements RecordReader {
+
+    private final ComponentLog log;
+    private final RecordSchema schema;
+    private final String fieldName;
+    private final Scanner lineReader;
+    private final String lineDelimiter;
+    private int linesLeftToSkip;
+    private final int linesPerGroup;
+    private final boolean ignoreEmptyLines;
+
+    public TextLineRecordReader(final InputStream in, final ComponentLog logger, final String fieldName,
+                                int skipLineCount, int linesPerGroup, boolean ignoreEmptyLines, String lineDelimiter, String charSet) {
+        this.log = logger;
+        this.fieldName = fieldName;
+        this.lineReader = new Scanner(in, charSet).useDelimiter("(?<=" + lineDelimiter + ")");
+        this.lineDelimiter = lineDelimiter;
+        this.linesLeftToSkip = skipLineCount;
+        this.linesPerGroup = linesPerGroup;
+        this.ignoreEmptyLines = ignoreEmptyLines;
+        final List<RecordField> fieldList = Collections.singletonList(new RecordField(fieldName, RecordFieldType.STRING.getDataType()));
 
 Review comment:
   The benefit here is that you don't need to provide a schema. Otherwise I would think a GrokReader would suffice?

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services