You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@inlong.apache.org by GitBox <gi...@apache.org> on 2022/08/02 03:20:16 UTC

[GitHub] [inlong] EMsnap commented on a diff in pull request #5261: [INLONG-5259][Agent] File data sources support custom end-of-line symbols

EMsnap commented on code in PR #5261:
URL: https://github.com/apache/inlong/pull/5261#discussion_r935077609


##########
inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/sources/reader/TextFileReader.java:
##########
@@ -170,14 +210,92 @@ public void init(JobProfile jobConf) {
             if (StringUtils.isNotBlank(this.md5) && !this.md5.equals(md5)) {
                 LOGGER.warn("md5 is differ from origin, origin: {}, new {}", this.md5, md5);
             }
+            localSnapshot = new LocalSnapshot(file.getPath().concat(POSITION_SUFFIX));
+            setSnapshot(jobConf);
             LOGGER.info("file name for task is {}, md5 is {}", file, md5);
-            stream = Files.newBufferedReader(file.toPath()).lines().skip(position);
-            iterator = stream.iterator();
+            //split line and column
+            getFileStream(jobConf);
+            if (Objects.nonNull(stream)) {
+                iterator = stream.iterator();
+            }
         } catch (Exception ex) {
             throw new FileException("error init stream for " + file.getPath(), ex);
         }
     }
 
+    private void getFileStream(JobProfile jobConf) throws IOException {
+        List<String> lines = Files.newBufferedReader(file.toPath()).lines().skip(position).collect(Collectors.toList());
+        List<String> resultLines = new ArrayList<>();
+        //TODO line regular expression matching
+        if (jobConf.hasKey(JOB_FILE_LINE_END_PATTERN)) {
+            Pattern pattern = Pattern.compile(jobConf.get(JOB_FILE_LINE_END_PATTERN));
+            lines.forEach(line -> {
+                lineStringBuffer.put(file,
+                        lineStringBuffer.isEmpty() ? line : lineStringBuffer.get(file).concat(" ").concat(line));
+                String data = lineStringBuffer.get(file);
+                Matcher matcher = pattern.matcher(data);
+                if (matcher.find() && StringUtils.isNoneBlank(matcher.group())) {
+                    String[] splitLines = data.split(matcher.group());
+                    int length = splitLines.length;
+                    for (int i = 0; i < length; i++) {
+                        if (i > 0 && i == length - 1 && null != splitLines[i]) {
+                            lineStringBuffer.put(file, splitLines[i]);
+                            break;
+                        }
+                        resultLines.add(splitLines[i].trim());
+                    }
+                    if (0 == length - 1) {

Review Comment:
   length == 1



-- 
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