From dc3a82f3d9ea606f9205b033b13a25afb937b757 Mon Sep 17 00:00:00 2001 From: lhldyf Date: Thu, 5 Jul 2018 11:32:55 +0800 Subject: [PATCH] Update HdfsWriter.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 解决Windows下HdfsWriter的异常问题 --- .../plugin/writer/hdfswriter/HdfsWriter.java | 54 ++++--------------- 1 file changed, 11 insertions(+), 43 deletions(-) diff --git a/hdfswriter/src/main/java/com/alibaba/datax/plugin/writer/hdfswriter/HdfsWriter.java b/hdfswriter/src/main/java/com/alibaba/datax/plugin/writer/hdfswriter/HdfsWriter.java index 0119be2b..2911e545 100644 --- a/hdfswriter/src/main/java/com/alibaba/datax/plugin/writer/hdfswriter/HdfsWriter.java +++ b/hdfswriter/src/main/java/com/alibaba/datax/plugin/writer/hdfswriter/HdfsWriter.java @@ -260,21 +260,9 @@ public class HdfsWriter extends Writer { } private String buildFilePath() { - boolean isEndWithSeparator = false; - switch (IOUtils.DIR_SEPARATOR) { - case IOUtils.DIR_SEPARATOR_UNIX: - isEndWithSeparator = this.path.endsWith(String - .valueOf(IOUtils.DIR_SEPARATOR)); - break; - case IOUtils.DIR_SEPARATOR_WINDOWS: - isEndWithSeparator = this.path.endsWith(String - .valueOf(IOUtils.DIR_SEPARATOR_WINDOWS)); - break; - default: - break; - } + boolean isEndWithSeparator = this.path.endsWith(String.valueOf(IOUtils.DIR_SEPARATOR_UNIX)); if (!isEndWithSeparator) { - this.path = this.path + IOUtils.DIR_SEPARATOR; + this.path = this.path + IOUtils.DIR_SEPARATOR_UNIX; } return this.path; } @@ -286,38 +274,18 @@ public class HdfsWriter extends Writer { */ private String buildTmpFilePath(String userPath) { String tmpFilePath; - boolean isEndWithSeparator = false; - switch (IOUtils.DIR_SEPARATOR) { - case IOUtils.DIR_SEPARATOR_UNIX: - isEndWithSeparator = userPath.endsWith(String - .valueOf(IOUtils.DIR_SEPARATOR)); - break; - case IOUtils.DIR_SEPARATOR_WINDOWS: - isEndWithSeparator = userPath.endsWith(String - .valueOf(IOUtils.DIR_SEPARATOR_WINDOWS)); - break; - default: - break; - } + boolean isEndWithSeparator = userPath.endsWith(String.valueOf(IOUtils.DIR_SEPARATOR_UNIX)); String tmpSuffix; - tmpSuffix = UUID.randomUUID().toString().replace('-', '_'); - if (!isEndWithSeparator) { - tmpFilePath = String.format("%s__%s%s", userPath, tmpSuffix, IOUtils.DIR_SEPARATOR); - }else if("/".equals(userPath)){ - tmpFilePath = String.format("%s__%s%s", userPath, tmpSuffix, IOUtils.DIR_SEPARATOR); - }else{ - tmpFilePath = String.format("%s__%s%s", userPath.substring(0,userPath.length()-1), tmpSuffix, IOUtils.DIR_SEPARATOR); - } - while(hdfsHelper.isPathexists(tmpFilePath)){ + do { tmpSuffix = UUID.randomUUID().toString().replace('-', '_'); - if (!isEndWithSeparator) { - tmpFilePath = String.format("%s__%s%s", userPath, tmpSuffix, IOUtils.DIR_SEPARATOR); - }else if("/".equals(userPath)){ - tmpFilePath = String.format("%s__%s%s", userPath, tmpSuffix, IOUtils.DIR_SEPARATOR); - }else{ - tmpFilePath = String.format("%s__%s%s", userPath.substring(0,userPath.length()-1), tmpSuffix, IOUtils.DIR_SEPARATOR); + if (!isEndWithSeparator || String.valueOf(IOUtils.DIR_SEPARATOR_UNIX).equals(userPath)) { + tmpFilePath = String.format("%s__%s%s", userPath, tmpSuffix, IOUtils.DIR_SEPARATOR_UNIX); + } else { + tmpFilePath = String.format("%s__%s%s", userPath.substring(0,userPath.length()-1), tmpSuffix, + IOUtils.DIR_SEPARATOR_UNIX); } - } + } while(hdfsHelper.isPathexists(tmpFilePath)); + return tmpFilePath; } }