crunch-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gr...@apache.org
Subject git commit: CRUNCH-344 URL-encode paths in CrunchInputs
Date Sat, 15 Feb 2014 20:15:08 GMT
Repository: crunch
Updated Branches:
  refs/heads/apache-crunch-0.8 2b30a49b0 -> d34302b9b


CRUNCH-344 URL-encode paths in CrunchInputs

Use URL-encoding on paths when serializing input information to
the Configuration to avoid issues with paths that contain the
separator characters used for separating the serialized structure
stored in the Configuration.


Project: http://git-wip-us.apache.org/repos/asf/crunch/repo
Commit: http://git-wip-us.apache.org/repos/asf/crunch/commit/d34302b9
Tree: http://git-wip-us.apache.org/repos/asf/crunch/tree/d34302b9
Diff: http://git-wip-us.apache.org/repos/asf/crunch/diff/d34302b9

Branch: refs/heads/apache-crunch-0.8
Commit: d34302b9ba22ac8997d80da901f72ba64b03cc68
Parents: 2b30a49
Author: Gabriel Reid <greid@apache.org>
Authored: Sat Feb 15 18:05:34 2014 +0100
Committer: Gabriel Reid <greid@apache.org>
Committed: Sat Feb 15 20:43:15 2014 +0100

----------------------------------------------------------------------
 .../java/org/apache/crunch/io/CrunchInputs.java | 48 +++++++----
 .../org/apache/crunch/io/CrunchInputsTest.java  | 87 ++++++++++++++++++++
 2 files changed, 120 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/crunch/blob/d34302b9/crunch-core/src/main/java/org/apache/crunch/io/CrunchInputs.java
----------------------------------------------------------------------
diff --git a/crunch-core/src/main/java/org/apache/crunch/io/CrunchInputs.java b/crunch-core/src/main/java/org/apache/crunch/io/CrunchInputs.java
index 27bd696..a8290f8 100644
--- a/crunch-core/src/main/java/org/apache/crunch/io/CrunchInputs.java
+++ b/crunch-core/src/main/java/org/apache/crunch/io/CrunchInputs.java
@@ -17,24 +17,26 @@
  */
 package org.apache.crunch.io;
 
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.net.URLEncoder;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 
+import com.google.common.base.Joiner;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Splitter;
 import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.Path;
 import org.apache.hadoop.mapreduce.InputFormat;
 import org.apache.hadoop.mapreduce.Job;
 import org.apache.hadoop.mapreduce.JobContext;
 
-import com.google.common.base.Joiner;
-import com.google.common.base.Preconditions;
-import com.google.common.base.Splitter;
-import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
-
 /**
  * Helper functions for configuring multiple {@code InputFormat} instances within a single
  * Crunch MapReduce job.
@@ -54,13 +56,13 @@ public class CrunchInputs {
 
   public static void addInputPaths(Job job, Collection<Path> paths, FormatBundle inputBundle,
int nodeIndex) {
     Configuration conf = job.getConfiguration();
-    List<String> pathStrs = Lists.newArrayListWithExpectedSize(paths.size());
+    List<String> encodedPathStrs = Lists.newArrayListWithExpectedSize(paths.size());
     for (Path path : paths) {
-      String pathStr = path.toString();
+      String pathStr = encodePath(path);
       Preconditions.checkArgument(pathStr.indexOf(RECORD_SEP) == -1 && pathStr.indexOf(FIELD_SEP)
== -1 && pathStr.indexOf(PATH_SEP) == -1);
-      pathStrs.add(pathStr);
+      encodedPathStrs.add(pathStr);
     }
-    String inputs = JOINER.join(inputBundle.serialize(), String.valueOf(nodeIndex), Joiner.on(PATH_SEP).join(pathStrs));
+    String inputs = JOINER.join(inputBundle.serialize(), String.valueOf(nodeIndex), Joiner.on(PATH_SEP).join(encodedPathStrs));
     String existing = conf.get(CRUNCH_INPUTS);
     conf.set(CRUNCH_INPUTS, existing == null ? inputs : existing + RECORD_SEP + inputs);
   }
@@ -76,19 +78,35 @@ public class CrunchInputs {
       List<String> fields = Lists.newArrayList(SPLITTER.split(input));
       FormatBundle<InputFormat> inputBundle = FormatBundle.fromSerialized(fields.get(0),
job.getConfiguration());
       if (!formatNodeMap.containsKey(inputBundle)) {
-        formatNodeMap.put(inputBundle, Maps.<Integer, List<Path>> newHashMap());
+        formatNodeMap.put(inputBundle, Maps.<Integer, List<Path>>newHashMap());
       }
       Integer nodeIndex = Integer.valueOf(fields.get(1));
       if (!formatNodeMap.get(inputBundle).containsKey(nodeIndex)) {
-        formatNodeMap.get(inputBundle).put(nodeIndex, Lists.<Path> newLinkedList());
+        formatNodeMap.get(inputBundle).put(nodeIndex, Lists.<Path>newLinkedList());
       }
       List<Path> formatNodePaths = formatNodeMap.get(inputBundle).get(nodeIndex);
-      String paths = fields.get(2);
-      for (String path : Splitter.on(PATH_SEP).split(paths)) {
-        formatNodePaths.add(new Path(path));
+      String encodedPaths = fields.get(2);
+      for (String encodedPath : Splitter.on(PATH_SEP).split(encodedPaths)) {
+        formatNodePaths.add(decodePath(encodedPath));
       }
     }
     return formatNodeMap;
   }
 
+  private static String encodePath(Path path) {
+    try {
+      return URLEncoder.encode(path.toString(), "UTF-8");
+    } catch (UnsupportedEncodingException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  private static Path decodePath(String encodedPath) {
+    try {
+      return new Path(URLDecoder.decode(encodedPath, "UTF-8"));
+    } catch (UnsupportedEncodingException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/crunch/blob/d34302b9/crunch-core/src/test/java/org/apache/crunch/io/CrunchInputsTest.java
----------------------------------------------------------------------
diff --git a/crunch-core/src/test/java/org/apache/crunch/io/CrunchInputsTest.java b/crunch-core/src/test/java/org/apache/crunch/io/CrunchInputsTest.java
new file mode 100644
index 0000000..e04fd0b
--- /dev/null
+++ b/crunch-core/src/test/java/org/apache/crunch/io/CrunchInputsTest.java
@@ -0,0 +1,87 @@
+/**
+ * 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.crunch.io;
+
+import java.io.IOException;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.mapreduce.Job;
+import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class CrunchInputsTest {
+
+  private Job job;
+  private FormatBundle formatBundle;
+
+  @Before
+  public void setUp() throws IOException {
+    job = new Job();
+    formatBundle = FormatBundle.forInput(TextInputFormat.class);
+  }
+
+  @Test
+  public void testAddAndGetPaths() {
+    Path inputPath = new Path("/tmp");
+    CrunchInputs.addInputPath(job, inputPath, formatBundle, 0);
+
+    assertEquals(
+        ImmutableMap.of(formatBundle, ImmutableMap.of(0, ImmutableList.of(inputPath))),
+        CrunchInputs.getFormatNodeMap(job));
+
+  }
+
+  @Test
+  public void testAddAndGetPaths_GlobPath() {
+    Path globPath = new Path("/tmp/file{1,2,3}.txt");
+    CrunchInputs.addInputPath(job, globPath, formatBundle, 0);
+
+    assertEquals(
+        ImmutableMap.of(formatBundle, ImmutableMap.of(0, ImmutableList.of(globPath))),
+        CrunchInputs.getFormatNodeMap(job));
+  }
+
+  @Test
+  public void testAddAndGetPaths_PathUsingSeparators() {
+    Path pathUsingRecordSeparators = new Path("/tmp/,;|.txt");
+    CrunchInputs.addInputPath(job, pathUsingRecordSeparators, formatBundle, 0);
+
+    assertEquals(
+        ImmutableMap.of(formatBundle, ImmutableMap.of(0, ImmutableList.of(pathUsingRecordSeparators))),
+        CrunchInputs.getFormatNodeMap(job));
+  }
+
+  @Test
+  public void testAddAndGetPaths_FullyQualifiedPath() throws IOException {
+    Path fullyQualifiedPath = new Path("/tmp").makeQualified(FileSystem.getLocal(new Configuration()));
+    System.out.println("Fully qualified: " + fullyQualifiedPath);
+    CrunchInputs.addInputPath(job, fullyQualifiedPath, formatBundle, 0);
+
+    assertEquals(
+        ImmutableMap.of(formatBundle, ImmutableMap.of(0, ImmutableList.of(fullyQualifiedPath))),
+        CrunchInputs.getFormatNodeMap(job));
+
+  }
+}


Mime
View raw message