sqoop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jar...@apache.org
Subject svn commit: r1345225 - in /sqoop/trunk/src: java/org/apache/sqoop/manager/SqlManager.java java/org/apache/sqoop/util/SqlTypeMap.java test/com/cloudera/sqoop/hive/TestTableDefWriter.java
Date Fri, 01 Jun 2012 15:12:25 GMT
Author: jarcec
Date: Fri Jun  1 15:12:25 2012
New Revision: 1345225

URL: http://svn.apache.org/viewvc?rev=1345225&view=rev
Log:
SQOOP-481. Sqoop import with --hive-import using wrong column names in --columns throws a
NPE.

(Cheolsoo Park via Jarek Jarcec Cecho)

Added:
    sqoop/trunk/src/java/org/apache/sqoop/util/SqlTypeMap.java
Modified:
    sqoop/trunk/src/java/org/apache/sqoop/manager/SqlManager.java
    sqoop/trunk/src/test/com/cloudera/sqoop/hive/TestTableDefWriter.java

Modified: sqoop/trunk/src/java/org/apache/sqoop/manager/SqlManager.java
URL: http://svn.apache.org/viewvc/sqoop/trunk/src/java/org/apache/sqoop/manager/SqlManager.java?rev=1345225&r1=1345224&r2=1345225&view=diff
==============================================================================
--- sqoop/trunk/src/java/org/apache/sqoop/manager/SqlManager.java (original)
+++ sqoop/trunk/src/java/org/apache/sqoop/manager/SqlManager.java Fri Jun  1 15:12:25 2012
@@ -51,6 +51,7 @@ import java.util.Properties;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.hadoop.util.StringUtils;
+import org.apache.sqoop.util.SqlTypeMap;
 
 /**
  * ConnManager implementation for generic SQL-compliant database.
@@ -195,7 +196,7 @@ public abstract class SqlManager
     }
 
     try {
-      Map<String, Integer> colTypes = new HashMap<String, Integer>();
+      Map<String, Integer> colTypes = new SqlTypeMap<String, Integer>();
 
       int cols = results.getMetaData().getColumnCount();
       ResultSetMetaData metadata = results.getMetaData();

Added: sqoop/trunk/src/java/org/apache/sqoop/util/SqlTypeMap.java
URL: http://svn.apache.org/viewvc/sqoop/trunk/src/java/org/apache/sqoop/util/SqlTypeMap.java?rev=1345225&view=auto
==============================================================================
--- sqoop/trunk/src/java/org/apache/sqoop/util/SqlTypeMap.java (added)
+++ sqoop/trunk/src/java/org/apache/sqoop/util/SqlTypeMap.java Fri Jun  1 15:12:25 2012
@@ -0,0 +1,58 @@
+/**
+ * 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.util;
+
+import java.util.HashMap;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * Using java.utils.HashMap to store primitive data types such as int can
+ * be unsafe because auto-unboxing a null value in the map can cause a NPE.
+ *
+ * SqlTypeMap is meant to be safer because it provides validation for arguments
+ * and fails fast with informative messages if invalid arguments are given.
+ */
+public class SqlTypeMap<K, V> extends HashMap<K, V> {
+
+  private static final long serialVersionUID = 1L;
+
+  public static final Log LOG = LogFactory.getLog(SqlTypeMap.class.getName());
+
+  @Override
+  public V get(Object col) {
+    V sqlType = super.get(col);
+    if (sqlType == null) {
+      LOG.error("It seems like you are looking up a column that does not");
+      LOG.error("exist in the table. Please ensure that you've specified");
+      LOG.error("correct column names in Sqoop options.");
+      throw new IllegalArgumentException("column not found: " + col);
+    }
+    return sqlType;
+  }
+
+  @Override
+  public V put(K col, V sqlType) {
+    if (sqlType == null) {
+      throw new IllegalArgumentException("sql type cannot be null");
+    }
+    return super.put(col, sqlType);
+  }
+}

Modified: sqoop/trunk/src/test/com/cloudera/sqoop/hive/TestTableDefWriter.java
URL: http://svn.apache.org/viewvc/sqoop/trunk/src/test/com/cloudera/sqoop/hive/TestTableDefWriter.java?rev=1345225&r1=1345224&r2=1345225&view=diff
==============================================================================
--- sqoop/trunk/src/test/com/cloudera/sqoop/hive/TestTableDefWriter.java (original)
+++ sqoop/trunk/src/test/com/cloudera/sqoop/hive/TestTableDefWriter.java Fri Jun  1 15:12:25
2012
@@ -18,7 +18,6 @@
 
 package com.cloudera.sqoop.hive;
 
-import java.util.HashMap;
 import java.util.Map;
 
 import junit.framework.TestCase;
@@ -26,6 +25,7 @@ import junit.framework.TestCase;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.hadoop.conf.Configuration;
+import org.apache.sqoop.util.SqlTypeMap;
 
 import com.cloudera.sqoop.SqoopOptions;
 import com.cloudera.sqoop.tool.ImportTool;
@@ -67,7 +67,7 @@ public class TestTableDefWriter extends 
     TableDefWriter writer = new TableDefWriter(options, null,
         "inputTable", "outputTable", conf, false);
 
-    Map<String, Integer> colTypes = new HashMap<String, Integer>();
+    Map<String, Integer> colTypes = new SqlTypeMap<String, Integer>();
     writer.setColumnTypes(colTypes);
 
     String createTable = writer.getCreateTableStmt();
@@ -95,7 +95,7 @@ public class TestTableDefWriter extends 
     TableDefWriter writer = new TableDefWriter(options, null,
         inputTable, outputTable, conf, false);
 
-    Map<String, Integer> colTypes = new HashMap<String, Integer>();
+    Map<String, Integer> colTypes = new SqlTypeMap<String, Integer>();
     writer.setColumnTypes(colTypes);
 
     String createTable = writer.getCreateTableStmt();
@@ -122,7 +122,7 @@ public class TestTableDefWriter extends 
     TableDefWriter writer = new TableDefWriter(options,
         null, "inputTable", "outputTable", conf, false);
 
-    Map<String, Integer> colTypes = new HashMap<String, Integer>();
+    Map<String, Integer> colTypes = new SqlTypeMap<String, Integer>();
     writer.setColumnTypes(colTypes);
 
     String createTable = writer.getCreateTableStmt();
@@ -148,7 +148,7 @@ public class TestTableDefWriter extends 
     TableDefWriter writer = new TableDefWriter(options,
         null, "inputTable", "outputTable", conf, false);
 
-    Map<String, Integer> colTypes = new HashMap<String, Integer>();
+    Map<String, Integer> colTypes = new SqlTypeMap<String, Integer>();
     writer.setColumnTypes(colTypes);
 
     String createTable = writer.getCreateTableStmt();
@@ -175,7 +175,7 @@ public class TestTableDefWriter extends 
     TableDefWriter writer = new TableDefWriter(options,
         null, HsqldbTestServer.getTableName(), "outputTable", conf, false);
 
-    Map<String, Integer> colTypes = new HashMap<String, Integer>();
+    Map<String, Integer> colTypes = new SqlTypeMap<String, Integer>();
     colTypes.put("id", Types.INTEGER);
     colTypes.put("value", Types.VARCHAR);
     writer.setColumnTypes(colTypes);
@@ -201,7 +201,7 @@ public class TestTableDefWriter extends 
     TableDefWriter writer = new TableDefWriter(options,
         null, HsqldbTestServer.getTableName(), "outputTable", conf, false);
 
-    Map<String, Integer> colTypes = new HashMap<String, Integer>();
+    Map<String, Integer> colTypes = new SqlTypeMap<String, Integer>();
     colTypes.put("id", Types.INTEGER);
     writer.setColumnTypes(colTypes);
 



Mime
View raw message