avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r1174323 - in /avro/trunk: ./ lang/java/avro/src/main/java/org/apache/avro/generic/ lang/java/avro/src/main/java/org/apache/avro/specific/ lang/java/ipc/src/test/java/org/apache/avro/
Date Thu, 22 Sep 2011 19:07:08 GMT
Author: cutting
Date: Thu Sep 22 19:07:08 2011
New Revision: 1174323

URL: http://svn.apache.org/viewvc?rev=1174323&view=rev
Log:
AVRO-891. Java: In SpecificDatumReader, when no reader schema is specified, use schema of
currently loaded class.

Added:
    avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/TestDataFileSpecific.java
Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericDatumReader.java
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificDatumReader.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1174323&r1=1174322&r2=1174323&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Thu Sep 22 19:07:08 2011
@@ -71,6 +71,9 @@ Avro 1.6.0 (unreleased)
 
     AVRO-874. Remove experimental disclaimer from IDL documentation. (cutting)
 
+    AVRO-891. Java: In SpecificDatumReader, when no reader schema is
+    specified, use schema of currently loaded class.  (cutting) 
+
   BUG FIXES
 
     AVRO-824. Java: Fix usage message of BinaryFragmentToJsonTool.

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericDatumReader.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericDatumReader.java?rev=1174323&r1=1174322&r2=1174323&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericDatumReader.java
(original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericDatumReader.java
Thu Sep 22 19:07:08 2011
@@ -66,6 +66,9 @@ public class GenericDatumReader<D> imple
   /** Return the {@link GenericData} implementation. */
   public GenericData getData() { return data; }
 
+  /** Return the writer's schema. */
+  public Schema getSchema() { return actual; }
+
   @Override
   public void setSchema(Schema writer) {
     this.actual = writer;
@@ -75,8 +78,11 @@ public class GenericDatumReader<D> imple
     creatorResolver = null;
   }
 
+  /** Get the reader's schema. */
+  public Schema getExpected() { return expected; }
+
   /** Set the reader's schema. */
-  public void setExpected(Schema reader) throws IOException {
+  public void setExpected(Schema reader) {
     this.expected = reader;
     creatorResolver = null;
   }

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificDatumReader.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificDatumReader.java?rev=1174323&r1=1174322&r2=1174323&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificDatumReader.java
(original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificDatumReader.java
Thu Sep 22 19:07:08 2011
@@ -51,6 +51,20 @@ public class SpecificDatumReader<T> exte
   public SpecificData getSpecificData() { return (SpecificData)getData(); }
 
   @Override
+  public void setSchema(Schema actual) {
+    // if expected is unset and actual is a specific record,
+    // then default expected to schema of currently loaded class
+    if (getExpected() == null && actual != null
+        && actual.getType() == Schema.Type.RECORD) {
+      SpecificData data = getSpecificData();
+      Class c = data.getClass(actual);
+      if (c != null && SpecificRecord.class.isAssignableFrom(c))
+        setExpected(data.getSchema(c));
+    }
+    super.setSchema(actual);
+  }
+
+  @Override
   @SuppressWarnings("unchecked")
   protected Object createEnum(String symbol, Schema schema) {
     Class c = getSpecificData().getClass(schema);

Added: avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/TestDataFileSpecific.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/TestDataFileSpecific.java?rev=1174323&view=auto
==============================================================================
--- avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/TestDataFileSpecific.java (added)
+++ avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/TestDataFileSpecific.java Thu Sep
22 19:07:08 2011
@@ -0,0 +1,77 @@
+/**
+ * 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;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.avro.file.DataFileReader;
+import org.apache.avro.file.DataFileWriter;
+import org.apache.avro.generic.GenericData.Record;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.avro.specific.SpecificDatumReader;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import org.apache.avro.Foo;
+
+public class TestDataFileSpecific {
+
+  private static final File DIR =
+    new File(System.getProperty("test.dir","/tmp"));
+  private static final File FILE = new File(DIR, "specific.avro");
+
+  /* Test when using SpecificDatumReader<T>() constructor to read from a file
+   * with a different schema that both reader & writer schemas are found.*/
+  @Test
+  public void testSpecificDatumReaderDefaultCtor() throws IOException {
+    // like the specific Foo, but with another field
+    Schema s1 = Schema.parse("{\"type\":\"record\",\"name\":\"Foo\","
+                             +"\"namespace\":\"org.apache.avro\",\"fields\":["
+                             +"{\"name\":\"label\",\"type\":\"string\"},"
+                             +"{\"name\":\"id\",\"type\":\"int\"}]}");
+
+    // write a file using generic objects
+    DataFileWriter<Record> writer
+      = new DataFileWriter<Record>(new GenericDatumWriter<Record>(s1))
+      .create(s1, FILE);
+    for (int i = 0; i < 10; i++) {
+      Record r = new Record(s1);
+      r.put("label", ""+i);
+      r.put("id", i);
+      writer.append(r);
+    }
+    writer.close();
+
+    // read using a 'new SpecificDatumReader<T>()' to force inference of
+    // reader's schema from runtime
+    DataFileReader<Foo> reader =
+      new DataFileReader<Foo>(FILE, new SpecificDatumReader<Foo>());
+    int i = 0;
+    for (Foo f : reader) 
+      Assert.assertEquals(""+(i++), f.getLabel().toString());
+    Assert.assertEquals(10, i);
+    reader.close();
+  }
+
+}



Mime
View raw message