sqoop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a..@apache.org
Subject sqoop git commit: SQOOP-2214: Sqoop2: Add ability to easily iterate over entries stored in Context
Date Mon, 16 Mar 2015 21:01:57 GMT
Repository: sqoop
Updated Branches:
  refs/heads/sqoop2 4fb5d6a92 -> 118007106


SQOOP-2214: Sqoop2: Add ability to easily iterate over entries stored in Context

(Jarek Jarcec Cecho via Abraham Elmahrek)


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

Branch: refs/heads/sqoop2
Commit: 118007106a629916c884d74178dd2b8a06c37c3b
Parents: 4fb5d6a
Author: Abraham Elmahrek <abe@apache.org>
Authored: Mon Mar 16 13:13:38 2015 -0700
Committer: Abraham Elmahrek <abe@apache.org>
Committed: Mon Mar 16 13:13:38 2015 -0700

----------------------------------------------------------------------
 .../apache/sqoop/common/ImmutableContext.java   |   4 +-
 .../org/apache/sqoop/common/MapContext.java     |   6 +
 .../org/apache/sqoop/common/PrefixContext.java  |  23 ++++
 .../org/apache/sqoop/common/TestMapContext.java |  29 +++++
 .../apache/sqoop/common/TestPrefixContext.java  | 113 +++++++++++++++++++
 5 files changed, 174 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/sqoop/blob/11800710/common/src/main/java/org/apache/sqoop/common/ImmutableContext.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/sqoop/common/ImmutableContext.java b/common/src/main/java/org/apache/sqoop/common/ImmutableContext.java
index 7eed27f..2245ba9 100644
--- a/common/src/main/java/org/apache/sqoop/common/ImmutableContext.java
+++ b/common/src/main/java/org/apache/sqoop/common/ImmutableContext.java
@@ -20,6 +20,8 @@ package org.apache.sqoop.common;
 import org.apache.sqoop.classification.InterfaceAudience;
 import org.apache.sqoop.classification.InterfaceStability;
 
+import java.util.Map;
+
 /**
  * Immutable context interface for key value pairs.
  *
@@ -27,7 +29,7 @@ import org.apache.sqoop.classification.InterfaceStability;
  */
 @InterfaceAudience.Public
 @InterfaceStability.Unstable
-public interface ImmutableContext {
+public interface ImmutableContext extends Iterable<Map.Entry<String,String>>
{
 
   /**
    * Return string for given key or null by default.

http://git-wip-us.apache.org/repos/asf/sqoop/blob/11800710/common/src/main/java/org/apache/sqoop/common/MapContext.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/sqoop/common/MapContext.java b/common/src/main/java/org/apache/sqoop/common/MapContext.java
index d23ff1a..abf5634 100644
--- a/common/src/main/java/org/apache/sqoop/common/MapContext.java
+++ b/common/src/main/java/org/apache/sqoop/common/MapContext.java
@@ -21,6 +21,7 @@ import org.apache.sqoop.classification.InterfaceAudience;
 import org.apache.sqoop.classification.InterfaceStability;
 
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -138,4 +139,9 @@ public class MapContext implements ImmutableContext {
     }
     return result;
   }
+
+  @Override
+  public Iterator<Map.Entry<String, String>> iterator() {
+    return options.entrySet().iterator();
+  }
 }

http://git-wip-us.apache.org/repos/asf/sqoop/blob/11800710/common/src/main/java/org/apache/sqoop/common/PrefixContext.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/sqoop/common/PrefixContext.java b/common/src/main/java/org/apache/sqoop/common/PrefixContext.java
index 8d33861..c78616d 100644
--- a/common/src/main/java/org/apache/sqoop/common/PrefixContext.java
+++ b/common/src/main/java/org/apache/sqoop/common/PrefixContext.java
@@ -22,6 +22,10 @@ import org.apache.sqoop.classification.InterfaceStability;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.sqoop.common.ImmutableContext;
 
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
 /**
  * Implementation of immutable context that is based on Hadoop configuration
  * object. Each context property is prefixed with special prefix and loaded
@@ -71,4 +75,23 @@ public class PrefixContext implements ImmutableContext {
   public Configuration getConfiguration() {
     return configuration;
   }
+
+  /*
+   * There is no good way to get iterator from the underlying Configuration object that would
+   * filter only the prefixed properties, so we create new Context/Map that contains only
the
+   * relevant properties.
+   */
+  @Override
+  public Iterator<Map.Entry<String, String>> iterator() {
+    Map<String, String> intermediateMap = new HashMap<String, String>();
+    for(Map.Entry<String, String> entry : configuration) {
+      String key = entry.getKey();
+
+      if(key.startsWith(prefix)) {
+        intermediateMap.put(key.replaceFirst(prefix, ""), entry.getValue());
+      }
+    }
+
+    return intermediateMap.entrySet().iterator();
+  }
 }

http://git-wip-us.apache.org/repos/asf/sqoop/blob/11800710/common/src/test/java/org/apache/sqoop/common/TestMapContext.java
----------------------------------------------------------------------
diff --git a/common/src/test/java/org/apache/sqoop/common/TestMapContext.java b/common/src/test/java/org/apache/sqoop/common/TestMapContext.java
index f9ed37b..0bd6a3b 100644
--- a/common/src/test/java/org/apache/sqoop/common/TestMapContext.java
+++ b/common/src/test/java/org/apache/sqoop/common/TestMapContext.java
@@ -23,6 +23,9 @@ import java.util.Map;
 import org.testng.Assert;
 import org.testng.annotations.Test;
 
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
+
 /**
  * Test class for org.apache.sqoop.common.MapContext
  */
@@ -106,4 +109,30 @@ public class TestMapContext {
     Assert.assertFalse(result.containsKey("testsqoop1"));
     Assert.assertFalse(result.containsKey("testsqoop2"));
   }
+
+  /**
+   * Test iteration
+   */
+  @Test
+  public void testIterator() {
+    Map<String, String> options = new HashMap<String, String>();
+    options.put("sqooptest1", "value");
+    options.put("sqooptest2", "value");
+
+    MapContext mc = new MapContext(options);
+    boolean seenSqooptest1 = false;
+    boolean seenSqooptest2 = false;
+    for(Map.Entry<String, String> entry : mc) {
+      if("sqooptest1".equals(entry.getKey()) && "value".equals(entry.getValue()))
{
+        seenSqooptest1 = true;
+      } else if("sqooptest2".equals(entry.getKey()) && "value".equals(entry.getValue()))
{
+        seenSqooptest2 = true;
+      } else {
+        fail("Found unexpected property: " + entry.getKey() + " with value " + entry.getValue());
+      }
+    }
+
+    assertTrue(seenSqooptest1);
+    assertTrue(seenSqooptest2);
+  }
 }

http://git-wip-us.apache.org/repos/asf/sqoop/blob/11800710/common/src/test/java/org/apache/sqoop/common/TestPrefixContext.java
----------------------------------------------------------------------
diff --git a/common/src/test/java/org/apache/sqoop/common/TestPrefixContext.java b/common/src/test/java/org/apache/sqoop/common/TestPrefixContext.java
new file mode 100644
index 0000000..b4a4b00
--- /dev/null
+++ b/common/src/test/java/org/apache/sqoop/common/TestPrefixContext.java
@@ -0,0 +1,113 @@
+/**
+ * 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.sqoop.common;
+
+import org.apache.hadoop.conf.Configuration;
+import org.testng.annotations.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
+
+public class TestPrefixContext {
+
+  @Test
+  public void testBlankPrefix() {
+    Configuration configuration = new Configuration();
+    configuration.set("testkey", "testvalue");
+
+    PrefixContext context = new PrefixContext(configuration, "");
+    assertEquals("testvalue", context.getString("testkey"));
+  }
+
+  @Test
+  public void testNonBlankPrefix() {
+    Configuration configuration = new Configuration();
+    configuration.set("prefix.testkey", "testvalue");
+
+    PrefixContext context = new PrefixContext(configuration, "prefix.");
+    assertEquals("testvalue", context.getString("testkey"));
+  }
+
+  @Test
+  public void testGetString() {
+    Configuration configuration = new Configuration();
+    configuration.set("p.testkey", "testvalue");
+
+    PrefixContext context = new PrefixContext(configuration, "p.");
+    assertEquals("testvalue", context.getString("testkey"));
+    assertEquals("testvalue", context.getString("testkey", "defaultValue"));
+    assertEquals("defaultValue", context.getString("wrongKey", "defaultValue"));
+  }
+
+  @Test
+  public void testGetBoolean() {
+    Configuration configuration = new Configuration();
+    configuration.set("p.testkey", "true");
+
+    PrefixContext context = new PrefixContext(configuration, "p.");
+    assertEquals(true, context.getBoolean("testkey", false));
+    assertEquals(false, context.getBoolean("wrongKey", false));
+  }
+
+  @Test
+  public void testGetInt() {
+    Configuration configuration = new Configuration();
+    configuration.set("p.testkey", "123");
+
+    PrefixContext context = new PrefixContext(configuration, "p.");
+    assertEquals(123, context.getInt("testkey", 456));
+    assertEquals(456, context.getInt("wrongKey", 456));
+  }
+
+  @Test
+  public void testGetLong() {
+    Configuration configuration = new Configuration();
+    configuration.set("p.testkey", "123");
+
+    PrefixContext context = new PrefixContext(configuration, "p.");
+    assertEquals(123l, context.getLong("testkey", 456l));
+    assertEquals(456l, context.getLong("wrongKey", 456l));
+  }
+
+  @Test
+  public void testIterator() {
+    Configuration configuration = new Configuration();
+    configuration.set("p.sqooptest1", "value");
+    configuration.set("p.sqooptest2", "value");
+
+    PrefixContext context = new PrefixContext(configuration, "p.");
+    boolean seenSqooptest1 = false;
+    boolean seenSqooptest2 = false;
+    for(Map.Entry<String, String> entry : context) {
+      if("sqooptest1".equals(entry.getKey()) && "value".equals(entry.getValue()))
{
+        seenSqooptest1 = true;
+      } else if("sqooptest2".equals(entry.getKey()) && "value".equals(entry.getValue()))
{
+        seenSqooptest2 = true;
+      } else {
+        fail("Found unexpected property: " + entry.getKey() + " with value " + entry.getValue());
+      }
+    }
+
+    assertTrue(seenSqooptest1);
+    assertTrue(seenSqooptest2);
+  }
+}


Mime
View raw message