Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

AVRO-1183. Java: Provide a better error message when the schema for a…

… Pair cannot be inferred.

git-svn-id: https://svn.apache.org/repos/asf/avro/trunk@1409395 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
commit 1638ebb5ea88f277fe065babd90551fbf74439df 1 parent 957ccf7
@cutting cutting authored
View
3  CHANGES.txt
@@ -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.
View
47 lang/java/mapred/src/main/java/org/apache/avro/mapred/Pair.java
@@ -25,6 +25,7 @@
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 void put(int i, Object o) {
@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 Pair(GenericContainer key, Void value) {
}
@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 Pair(CharSequence key, Void value) {
}
@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 Pair(ByteBuffer key, Void value) {
}
@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 Pair(Integer key, Void value) {
}
@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 Pair(Long key, Void value) {
}
@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 Pair(Float key, Void value) {
}
@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 Pair(Double key, Void value) {
}
@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 Pair(Void key, Void value) {
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"},
View
40 lang/java/mapred/src/test/java/org/apache/avro/mapred/TestPair.java
@@ -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");
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.