You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by cu...@apache.org on 2012/11/14 22:00:04 UTC

svn commit: r1409395 - in /avro/trunk: CHANGES.txt lang/java/mapred/src/main/java/org/apache/avro/mapred/Pair.java lang/java/mapred/src/test/java/org/apache/avro/mapred/TestPair.java

Author: cutting
Date: Wed Nov 14 21:00:03 2012
New Revision: 1409395

URL: http://svn.apache.org/viewvc?rev=1409395&view=rev
Log:
AVRO-1183. Java: Provide a better error message when the schema for a Pair cannot be inferred.

Added:
    avro/trunk/lang/java/mapred/src/test/java/org/apache/avro/mapred/TestPair.java   (with props)
Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/mapred/src/main/java/org/apache/avro/mapred/Pair.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1409395&r1=1409394&r2=1409395&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Wed Nov 14 21:00:03 2012
@@ -23,6 +23,9 @@ Trunk (not yet released)
     AVRO-1169. Java: Reduce memory footprint of resolver.
     (Hernan Otero via cutting)
 
+    AVRO-1183. Java: Provide a better error message when the schema
+    for a Pair cannot be inferred. (cutting)
+
   BUG FIXES
 
     AVRO-1171. Java: Don't call configure() twice on mappers & reducers.

Modified: avro/trunk/lang/java/mapred/src/main/java/org/apache/avro/mapred/Pair.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/mapred/src/main/java/org/apache/avro/mapred/Pair.java?rev=1409395&r1=1409394&r2=1409395&view=diff
==============================================================================
--- avro/trunk/lang/java/mapred/src/main/java/org/apache/avro/mapred/Pair.java (original)
+++ avro/trunk/lang/java/mapred/src/main/java/org/apache/avro/mapred/Pair.java Wed Nov 14 21:00:03 2012
@@ -25,6 +25,7 @@ import java.util.WeakHashMap;
 import java.nio.ByteBuffer;
 
 import org.apache.avro.Schema;
+import org.apache.avro.AvroRuntimeException;
 import org.apache.avro.Schema.Type;
 import org.apache.avro.Schema.Field;
 import org.apache.avro.generic.GenericData;
@@ -165,43 +166,43 @@ public class Pair<K,V>
 
   @SuppressWarnings("unchecked")
   public Pair(Object key, Object value) {
-    this((K)key, ReflectData.get().getSchema(key.getClass()), (V)value, ReflectData.get().getSchema(value.getClass()));
+    this((K)key, getSchema(key), (V)value, getSchema(value));
   }
   @SuppressWarnings("unchecked")
   public Pair(Object key, GenericContainer value) {
-    this((K)key, ReflectData.get().getSchema(key.getClass()), (V)value, value.getSchema());
+    this((K)key, getSchema(key), (V)value, value.getSchema());
   }
   @SuppressWarnings("unchecked")
   public Pair(Object key, CharSequence value) {
-    this((K)key, ReflectData.get().getSchema(key.getClass()), (V)value, STRING_SCHEMA);
+    this((K)key, getSchema(key), (V)value, STRING_SCHEMA);
   }
   @SuppressWarnings("unchecked")
   public Pair(Object key, ByteBuffer value) {
-    this((K)key, ReflectData.get().getSchema(key.getClass()), (V)value, BYTES_SCHEMA);
+    this((K)key, getSchema(key), (V)value, BYTES_SCHEMA);
   }
   @SuppressWarnings("unchecked")
   public Pair(Object key, Integer value) {
-    this((K)key, ReflectData.get().getSchema(key.getClass()), (V)value, INT_SCHEMA);
+    this((K)key, getSchema(key), (V)value, INT_SCHEMA);
   }
   @SuppressWarnings("unchecked")
   public Pair(Object key, Long value) {
-    this((K)key, ReflectData.get().getSchema(key.getClass()), (V)value, LONG_SCHEMA);
+    this((K)key, getSchema(key), (V)value, LONG_SCHEMA);
   }
   @SuppressWarnings("unchecked")
   public Pair(Object key, Float value) {
-    this((K)key, ReflectData.get().getSchema(key.getClass()), (V)value, FLOAT_SCHEMA);
+    this((K)key, getSchema(key), (V)value, FLOAT_SCHEMA);
   }
   @SuppressWarnings("unchecked")
   public Pair(Object key, Double value) {
-    this((K)key, ReflectData.get().getSchema(key.getClass()), (V)value, DOUBLE_SCHEMA);
+    this((K)key, getSchema(key), (V)value, DOUBLE_SCHEMA);
   }
   @SuppressWarnings("unchecked")
   public Pair(Object key, Void value) {
-    this((K)key, ReflectData.get().getSchema(key.getClass()), (V)value, NULL_SCHEMA);
+    this((K)key, getSchema(key), (V)value, NULL_SCHEMA);
   }
   @SuppressWarnings("unchecked")
   public Pair(GenericContainer key, Object value) {
-    this((K)key, key.getSchema(), (V)value, ReflectData.get().getSchema(value.getClass()));
+    this((K)key, key.getSchema(), (V)value, getSchema(value));
   }
   @SuppressWarnings("unchecked")
   public Pair(GenericContainer key, GenericContainer value) {
@@ -237,7 +238,7 @@ public class Pair<K,V>
   }
   @SuppressWarnings("unchecked")
   public Pair(CharSequence key, Object value) {
-    this((K)key, STRING_SCHEMA, (V)value, ReflectData.get().getSchema(value.getClass()));
+    this((K)key, STRING_SCHEMA, (V)value, getSchema(value));
   }
   @SuppressWarnings("unchecked")
   public Pair(CharSequence key, GenericContainer value) {
@@ -273,7 +274,7 @@ public class Pair<K,V>
   }
   @SuppressWarnings("unchecked")
   public Pair(ByteBuffer key, Object value) {
-    this((K)key, BYTES_SCHEMA, (V)value, ReflectData.get().getSchema(value.getClass()));
+    this((K)key, BYTES_SCHEMA, (V)value, getSchema(value));
   }
   @SuppressWarnings("unchecked")
   public Pair(ByteBuffer key, GenericContainer value) {
@@ -309,7 +310,7 @@ public class Pair<K,V>
   }
   @SuppressWarnings("unchecked")
   public Pair(Integer key, Object value) {
-    this((K)key, INT_SCHEMA, (V)value, ReflectData.get().getSchema(value.getClass()));
+    this((K)key, INT_SCHEMA, (V)value, getSchema(value));
   }
   @SuppressWarnings("unchecked")
   public Pair(Integer key, GenericContainer value) {
@@ -345,7 +346,7 @@ public class Pair<K,V>
   }
   @SuppressWarnings("unchecked")
   public Pair(Long key, Object value) {
-    this((K)key, LONG_SCHEMA, (V)value, ReflectData.get().getSchema(value.getClass()));
+    this((K)key, LONG_SCHEMA, (V)value, getSchema(value));
   }
   @SuppressWarnings("unchecked")
   public Pair(Long key, GenericContainer value) {
@@ -381,7 +382,7 @@ public class Pair<K,V>
   }
   @SuppressWarnings("unchecked")
   public Pair(Float key, Object value) {
-    this((K)key, FLOAT_SCHEMA, (V)value, ReflectData.get().getSchema(value.getClass()));
+    this((K)key, FLOAT_SCHEMA, (V)value, getSchema(value));
   }
   @SuppressWarnings("unchecked")
   public Pair(Float key, GenericContainer value) {
@@ -417,7 +418,7 @@ public class Pair<K,V>
   }
   @SuppressWarnings("unchecked")
   public Pair(Double key, Object value) {
-    this((K)key, DOUBLE_SCHEMA, (V)value, ReflectData.get().getSchema(value.getClass()));
+    this((K)key, DOUBLE_SCHEMA, (V)value, getSchema(value));
   }
   @SuppressWarnings("unchecked")
   public Pair(Double key, GenericContainer value) {
@@ -453,7 +454,7 @@ public class Pair<K,V>
   }
   @SuppressWarnings("unchecked")
   public Pair(Void key, Object value) {
-    this((K)key, NULL_SCHEMA, (V)value, ReflectData.get().getSchema(value.getClass()));
+    this((K)key, NULL_SCHEMA, (V)value, getSchema(value));
   }
   @SuppressWarnings("unchecked")
   public Pair(Void key, GenericContainer value) {
@@ -488,8 +489,18 @@ public class Pair<K,V>
     this((K)key, NULL_SCHEMA, (V)value, NULL_SCHEMA);
   }
 
+  private static Schema getSchema(Object o) {
+    try {
+      return ReflectData.get().getSchema(o.getClass());
+    } catch (AvroRuntimeException e) {
+      throw new AvroRuntimeException
+        ("Cannot infer schema for : " + o.getClass()
+         + ".  Must create Pair with explicit key and value schemas.", e);
+    }
+  }
+
   // private static final String[][] TABLE = new String[][] {
-  //   {"Object", "ReflectData.get().getSchema({0}.getClass())"},
+  //   {"Object", "getSchema({0})"},
   //   {"GenericContainer", "{0}.getSchema()"},
   //   {"CharSequence", "STRING_SCHEMA"},
   //   {"ByteBuffer", "BYTES_SCHEMA"},

Added: avro/trunk/lang/java/mapred/src/test/java/org/apache/avro/mapred/TestPair.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/mapred/src/test/java/org/apache/avro/mapred/TestPair.java?rev=1409395&view=auto
==============================================================================
--- avro/trunk/lang/java/mapred/src/test/java/org/apache/avro/mapred/TestPair.java (added)
+++ avro/trunk/lang/java/mapred/src/test/java/org/apache/avro/mapred/TestPair.java Wed Nov 14 21:00:03 2012
@@ -0,0 +1,40 @@
+/**
+ * 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.avro.mapred;
+
+import java.util.ArrayList;
+
+import org.apache.avro.AvroRuntimeException;
+
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class TestPair {
+
+  @Test public void testCollectionFailure() throws Exception {
+    try {
+      new Pair("foo", new ArrayList());
+    } catch (AvroRuntimeException e) {
+      assertTrue(e.getMessage().startsWith("Cannot infer schema"));
+      return;
+    }
+    fail("Expected an AvroRuntimeException");
+  }
+
+}

Propchange: avro/trunk/lang/java/mapred/src/test/java/org/apache/avro/mapred/TestPair.java
------------------------------------------------------------------------------
    svn:eol-style = native