[
https://issues.apache.org/jira/browse/AVRO-2916?focusedWorklogId=679520&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-679520
]
ASF GitHub Bot logged work on AVRO-2916:
----------------------------------------
Author: ASF GitHub Bot
Created on: 10/Nov/21 08:50
Start Date: 10/Nov/21 08:50
Worklog Time Spent: 10m
Work Description: martin-g commented on a change in pull request #944:
URL: https://github.com/apache/avro/pull/944#discussion_r746361781
##########
File path: lang/java/avro/src/test/java/org/apache/avro/TestDataFileAppend.java
##########
@@ -0,0 +1,130 @@
+/*
+ * 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
+ *
+ * https://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 static org.junit.Assert.assertEquals;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Iterator;
+
+import org.apache.avro.file.CodecFactory;
+import org.apache.avro.file.DataFileReader;
+import org.apache.avro.file.DataFileStream.Header;
+import org.apache.avro.file.DataFileWriter;
+import org.apache.avro.generic.GenericDatumReader;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.avro.util.RandomData;
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+public class TestDataFileAppend {
+
+ @Rule
+ public TemporaryFolder DIR = new TemporaryFolder();
+
+ private static final String SCHEMA_JSON = "{\"type\": \"record\", \"name\":
\"Test\", \"fields\": ["
+ + "{\"name\":\"stringField\", \"type\":\"string\"}," +
"{\"name\":\"longField\", \"type\":\"long\"}]}";
+ private static final Schema SCHEMA = new Schema.Parser().parse(SCHEMA_JSON);
+ private static final long SEED = System.currentTimeMillis();
+ private static final int COUNT =
Integer.parseInt(System.getProperty("test.count", "200"));
+ private static final boolean VALIDATE =
!"false".equals(System.getProperty("test.validate", "true"));
+
+ /*
+ *
Review comment:
Please add a link to AVRO-2916 in the javadoc
##########
File path: lang/java/avro/src/main/java/org/apache/avro/file/DataFileWriter.java
##########
@@ -234,6 +235,28 @@ public boolean isFlushOnEveryBlock() {
return this;
}
+ /**
Review comment:
` public DataFileWriter<D> appendTo(SeekableInput in, OutputStream out)
` could delegate to the new `public DataFileWriter<D> appendTo(Header header,
OutputStream out)` after it gets the `header` from the `reader`.
##########
File path: lang/java/avro/src/test/java/org/apache/avro/TestDataFileAppend.java
##########
@@ -0,0 +1,130 @@
+/*
+ * 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
+ *
+ * https://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 static org.junit.Assert.assertEquals;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Iterator;
+
+import org.apache.avro.file.CodecFactory;
+import org.apache.avro.file.DataFileReader;
+import org.apache.avro.file.DataFileStream.Header;
+import org.apache.avro.file.DataFileWriter;
+import org.apache.avro.generic.GenericDatumReader;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.avro.util.RandomData;
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+public class TestDataFileAppend {
+
+ @Rule
+ public TemporaryFolder DIR = new TemporaryFolder();
+
+ private static final String SCHEMA_JSON = "{\"type\": \"record\", \"name\":
\"Test\", \"fields\": ["
+ + "{\"name\":\"stringField\", \"type\":\"string\"}," +
"{\"name\":\"longField\", \"type\":\"long\"}]}";
+ private static final Schema SCHEMA = new Schema.Parser().parse(SCHEMA_JSON);
+ private static final long SEED = System.currentTimeMillis();
+ private static final int COUNT =
Integer.parseInt(System.getProperty("test.count", "200"));
+ private static final boolean VALIDATE =
!"false".equals(System.getProperty("test.validate", "true"));
+
+ /*
+ *
+ */
+ @Test
+ public void testAppendTo() throws Exception {
+ File file = new File(DIR.getRoot().getPath(), "testAppend.avro");
Review comment:
Should this file be deleted at the end ?
Recently I've had a bad experience with kSQLDB generating 30GB of test data
on each test run.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 679520)
Time Spent: 0.5h (was: 20m)
> add DataFileWriter.appendTo(Header,OutputStream) to be able to append data to
> non-local file
> --------------------------------------------------------------------------------------------
>
> Key: AVRO-2916
> URL: https://issues.apache.org/jira/browse/AVRO-2916
> Project: Apache Avro
> Issue Type: Improvement
> Components: java
> Affects Versions: 1.11.0
> Reporter: Arnaud Nauwynck
> Priority: Trivial
> Labels: pull-request-available
> Time Spent: 0.5h
> Remaining Estimate: 0h
>
> It is not practical to append records to a remote DataFile (azure blob, aws,
> ..), not using java.io.File, but in-memory byte array to append to an
> existing remote data.
> The proposal is simply to add an equivalent method
> "DataFileWriter.appendTo(Header, OutputStream)" as follow:
> {code:java}
> /**
> * Open a writer appending to an existing stream.
> *
> * @param header the header from the existing data to append.
> * @param out positioned at the end of the existing file.
> */
> public DataFileWriter<D> appendTo(Header header, OutputStream out) throws
> IOException {
> assertNotOpen();
> this.schema = header.schema;
> this.sync = header.sync;
> this.meta.putAll(header.meta);
> byte[] codecBytes = this.meta.get(DataFileConstants.CODEC);
> if (codecBytes != null) {
> String strCodec = new String(codecBytes, StandardCharsets.UTF_8);
> this.codec = CodecFactory.fromString(strCodec).createInstance();
> } else {
> this.codec = CodecFactory.nullCodec().createInstance();
> }
> init(out);
> return this;
> }
> {code}
> in addition to the similar existing method:
> {code:java}
> public DataFileWriter<D> appendTo(SeekableInput in, OutputStream out)
> throws IOException {
> assertNotOpen();
> DataFileReader<D> reader = new DataFileReader<>(in, new
> GenericDatumReader<>());
> this.schema = reader.getSchema();
> this.sync = reader.getHeader().sync;
> this.meta.putAll(reader.getHeader().meta);
> byte[] codecBytes = this.meta.get(DataFileConstants.CODEC);
> if (codecBytes != null) {
> String strCodec = new String(codecBytes, StandardCharsets.UTF_8);
> this.codec = CodecFactory.fromString(strCodec).createInstance();
> } else {
> this.codec = CodecFactory.nullCodec().createInstance();
> }
> init(out);
> return this;
> }
> {code}
> Technically, we could call "DataFileWriter.appendTo(seekableInput, output)",
> but this is both complex and inneficient to pass the "seekableInput" fragment
> of an existing local file header.
> {code:java}
> byte[] inArrayHeader = ... fetch once the header of a remote file...
> User userToAppend = ...
>
> ByteArrayOutputStream out = new ByteArrayOutputStream();
> DatumWriter<User> writer = new
> SpecificDatumWriter<User>(User.getClassSchema());
> try (DataFileWriter<User> dataFileWriter = new DataFileWriter<User>(writer)) {
> dataFileWriter.setCodec(CodecFactory.nullCodec());
> try (SeekableByteArrayInput in = new
> SeekableByteArrayInput(inArrayHeader)) {
> dataFileWriter.appendTo(in, out); // ... inneficient: will
> reparse header schema,sync,meta each time!
> }
>
> dataFileWriter.append(userToAppend);
>
> }
> byte[] serializedBytes = out.toByteArray();
> // then use serializedBytes to append to remote file (azure blob, aws..)
> {code}
> Using the new proposed helper method, you could more simply and efficiently
> compute datablock to append to a remote data file:
> {code:java}
> // run once at startup
> Header header;
> {
> SeekableByteArrayInput in = new SeekableByteArrayInput(inArrayHeader);
> DataFileReader<Object> reader = new DataFileReader<>(in, new
> GenericDatumReader<>());
> header = reader.getHeader();
> reader.close();
> }
> // streaming code to append+flush rows to remote data
> for(;;) {
> User userToAppend = ...
> ByteArrayOutputStream out = new ByteArrayOutputStream();
> DatumWriter<User> writer = new
> SpecificDatumWriter<User>(User.getClassSchema());
> try (DataFileWriter<User> dataFileWriter = new
> DataFileWriter<User>(writer)) {
> dataFileWriter.setCodec(CodecFactory.nullCodec());
> dataFileWriter.appendTo(header, out); // efficient: no reparse
> schema,sync,meta
>
> dataFileWriter.append(userToAppend);
> }
> byte[] serializedBytes = out.toByteArray();
> // then use serializedBytes to append to remote file (azure blob, aws..)
> ... remoteAzureFile.append(.. serializedBytes) .. remoteAzureFile.flush()
> }
> {code}
--
This message was sent by Atlassian Jira
(v8.20.1#820001)