clazz) {
+ return new SpecificDatumWriter();
+ }
+
+}
diff --git a/src/java/org/apache/hadoop/io/serializer/avro/package.html b/src/java/org/apache/hadoop/io/serializer/avro/package.html
new file mode 100644
index 00000000000..ce565f6206b
--- /dev/null
+++ b/src/java/org/apache/hadoop/io/serializer/avro/package.html
@@ -0,0 +1,43 @@
+
+
+
+
+
+
+
+This package provides Avro serialization in Hadoop. This can be used to
+serialize/deserialize Avro types in Hadoop.
+
+
+
+Use {@link org.apache.hadoop.io.serializer.avro.AvroSpecificSerialization} for
+serialization of classes generated by Avro's 'specific' compiler.
+
+
+
+Use {@link org.apache.hadoop.io.serializer.avro.AvroReflectSerialization} for
+other classes.
+{@link org.apache.hadoop.io.serializer.avro.AvroReflectSerialization} work for
+any class which is either in the package list configured via
+{@link org.apache.hadoop.io.serializer.avro.AvroReflectSerialization#AVRO_REFLECT_PACKAGES}
+or implement {@link org.apache.hadoop.io.serializer.avro.AvroReflectSerializable}
+interface.
+
+
+
+
diff --git a/src/test/core/org/apache/hadoop/io/serializer/SerializationTestUtil.java b/src/test/core/org/apache/hadoop/io/serializer/SerializationTestUtil.java
new file mode 100644
index 00000000000..8d47db26cd2
--- /dev/null
+++ b/src/test/core/org/apache/hadoop/io/serializer/SerializationTestUtil.java
@@ -0,0 +1,57 @@
+/**
+ * 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.hadoop.io.serializer;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.io.DataInputBuffer;
+import org.apache.hadoop.io.DataOutputBuffer;
+import org.apache.hadoop.util.GenericsUtil;
+
+public class SerializationTestUtil {
+
+ /**
+ * A utility that tests serialization/deserialization.
+ * @param the class of the item
+ * @param conf configuration to use, "io.serializations" is read to
+ * determine the serialization
+ * @param before item to (de)serialize
+ * @return deserialized item
+ */
+ public static K testSerialization(Configuration conf, K before)
+ throws Exception {
+
+ SerializationFactory factory = new SerializationFactory(conf);
+ Serializer serializer
+ = factory.getSerializer(GenericsUtil.getClass(before));
+ Deserializer deserializer
+ = factory.getDeserializer(GenericsUtil.getClass(before));
+
+ DataOutputBuffer out = new DataOutputBuffer();
+ serializer.open(out);
+ serializer.serialize(before);
+ serializer.close();
+
+ DataInputBuffer in = new DataInputBuffer();
+ in.reset(out.getData(), out.getLength());
+ deserializer.open(in);
+ K after = deserializer.deserialize(null);
+ deserializer.close();
+ return after;
+ }
+
+}
diff --git a/src/test/core/org/apache/hadoop/io/serializer/TestWritableSerialization.java b/src/test/core/org/apache/hadoop/io/serializer/TestWritableSerialization.java
index 6a551753245..0d7c50b42bf 100644
--- a/src/test/core/org/apache/hadoop/io/serializer/TestWritableSerialization.java
+++ b/src/test/core/org/apache/hadoop/io/serializer/TestWritableSerialization.java
@@ -23,25 +23,18 @@ import static org.apache.hadoop.io.TestGenericWritable.CONF_TEST_VALUE;
import junit.framework.TestCase;
import org.apache.hadoop.conf.Configuration;
-import org.apache.hadoop.io.DataInputBuffer;
-import org.apache.hadoop.io.DataOutputBuffer;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.TestGenericWritable.Baz;
import org.apache.hadoop.io.TestGenericWritable.FooGenericWritable;
-import org.apache.hadoop.util.GenericsUtil;
public class TestWritableSerialization extends TestCase {
private static final Configuration conf = new Configuration();
-
- static {
- conf.set("io.serializations"
- , "org.apache.hadoop.io.serializer.WritableSerialization");
- }
-
+
public void testWritableSerialization() throws Exception {
Text before = new Text("test writable");
- testSerialization(conf, before);
+ Text after = SerializationTestUtil.testSerialization(conf, before);
+ assertEquals(before, after);
}
@@ -56,40 +49,8 @@ public class TestWritableSerialization extends TestCase {
generic.setConf(conf);
Baz baz = new Baz();
generic.set(baz);
- Baz result = testSerialization(conf, baz);
+ Baz result = SerializationTestUtil.testSerialization(conf, baz);
+ assertEquals(baz, result);
assertNotNull(result.getConf());
}
-
- /**
- * A utility that tests serialization/deserialization.
- * @param the class of the item
- * @param conf configuration to use, "io.serializations" is read to
- * determine the serialization
- * @param before item to (de)serialize
- * @return deserialized item
- */
- public static K testSerialization(Configuration conf, K before)
- throws Exception {
-
- SerializationFactory factory = new SerializationFactory(conf);
- Serializer serializer
- = factory.getSerializer(GenericsUtil.getClass(before));
- Deserializer deserializer
- = factory.getDeserializer(GenericsUtil.getClass(before));
-
- DataOutputBuffer out = new DataOutputBuffer();
- serializer.open(out);
- serializer.serialize(before);
- serializer.close();
-
- DataInputBuffer in = new DataInputBuffer();
- in.reset(out.getData(), out.getLength());
- deserializer.open(in);
- K after = deserializer.deserialize(null);
- deserializer.close();
-
- assertEquals(before, after);
- return after;
- }
-
}
diff --git a/src/test/core/org/apache/hadoop/io/serializer/avro/Record.java b/src/test/core/org/apache/hadoop/io/serializer/avro/Record.java
new file mode 100644
index 00000000000..275a0dc1e28
--- /dev/null
+++ b/src/test/core/org/apache/hadoop/io/serializer/avro/Record.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.hadoop.io.serializer.avro;
+
+public class Record {
+ public int x = 7;
+
+ public int hashCode() {
+ return x;
+ }
+
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ final Record other = (Record) obj;
+ if (x != other.x)
+ return false;
+ return true;
+ }
+}
\ No newline at end of file
diff --git a/src/test/core/org/apache/hadoop/io/serializer/avro/TestAvroSerialization.java b/src/test/core/org/apache/hadoop/io/serializer/avro/TestAvroSerialization.java
new file mode 100644
index 00000000000..e65a299fa3c
--- /dev/null
+++ b/src/test/core/org/apache/hadoop/io/serializer/avro/TestAvroSerialization.java
@@ -0,0 +1,104 @@
+/**
+ * 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.hadoop.io.serializer.avro;
+
+import junit.framework.TestCase;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.io.serializer.SerializationTestUtil;
+
+public class TestAvroSerialization extends TestCase {
+
+ private static final Configuration conf = new Configuration();
+
+ public void testSpecific() throws Exception {
+ AvroRecord before = new AvroRecord();
+ before.intField = 5;
+ AvroRecord after = SerializationTestUtil.testSerialization(conf, before);
+ assertEquals(before, after);
+ }
+
+ public void testReflectPkg() throws Exception {
+ Record before = new Record();
+ before.x = 10;
+ conf.set(AvroReflectSerialization.AVRO_REFLECT_PACKAGES,
+ before.getClass().getPackage().getName());
+ Record after = SerializationTestUtil.testSerialization(conf, before);
+ assertEquals(before, after);
+ }
+
+ public void testReflectInnerClass() throws Exception {
+ InnerRecord before = new InnerRecord();
+ before.x = 10;
+ conf.set(AvroReflectSerialization.AVRO_REFLECT_PACKAGES,
+ before.getClass().getPackage().getName());
+ InnerRecord after = SerializationTestUtil.testSerialization(conf, before);
+ assertEquals(before, after);
+ }
+
+ public void testReflect() throws Exception {
+ RefSerializable before = new RefSerializable();
+ before.x = 10;
+ RefSerializable after =
+ SerializationTestUtil.testSerialization(conf, before);
+ assertEquals(before, after);
+ }
+
+ public static class InnerRecord {
+ public int x = 7;
+
+ public int hashCode() {
+ return x;
+ }
+
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ final InnerRecord other = (InnerRecord) obj;
+ if (x != other.x)
+ return false;
+ return true;
+ }
+ }
+
+ public static class RefSerializable implements AvroReflectSerializable {
+ public int x = 7;
+
+ public int hashCode() {
+ return x;
+ }
+
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ final RefSerializable other = (RefSerializable) obj;
+ if (x != other.x)
+ return false;
+ return true;
+ }
+ }
+}
diff --git a/src/test/core/org/apache/hadoop/io/serializer/avro/avroRecord.avsc b/src/test/core/org/apache/hadoop/io/serializer/avro/avroRecord.avsc
new file mode 100644
index 00000000000..7e6ff617d7a
--- /dev/null
+++ b/src/test/core/org/apache/hadoop/io/serializer/avro/avroRecord.avsc
@@ -0,0 +1,23 @@
+// 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.
+
+
+{"type": "record", "name":"AvroRecord",
+ "namespace": "org.apache.hadoop.io.serializer.avro",
+ "fields": [
+ {"name": "intField", "type": "int"}
+ ]
+}