openinx commented on a change in pull request #3498:
URL: https://github.com/apache/iceberg/pull/3498#discussion_r745335258



##########
File path: aliyun/src/main/java/org/apache/iceberg/aliyun/oss/OSSOutputFile.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.iceberg.aliyun.oss;
+
+import com.aliyun.oss.OSS;
+import org.apache.iceberg.aliyun.AliyunProperties;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.io.InputFile;
+import org.apache.iceberg.io.OutputFile;
+import org.apache.iceberg.io.PositionOutputStream;
+
+class OSSOutputFile extends BaseOSSFile implements OutputFile {
+
+  OSSOutputFile(OSS client, OSSURI uri, AliyunProperties aliyunProperties) {
+    super(client, uri, aliyunProperties);
+  }
+
+  public static OSSOutputFile fromLocation(OSS client, String location, 
AliyunProperties aliyunProperties) {

Review comment:
       As this `OSSOutputFile` is not marked as `public`,  do we need to add 
the extra `public` for this static method ?

##########
File path: 
aliyun/src/test/java/org/apache/iceberg/aliyun/oss/TestOSSOutputFile.java
##########
@@ -0,0 +1,137 @@
+/*
+ * 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.iceberg.aliyun.oss;
+
+import com.aliyun.oss.OSS;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Random;
+import java.util.UUID;
+import java.util.concurrent.ThreadLocalRandom;
+import org.apache.commons.io.IOUtils;
+import org.apache.iceberg.AssertHelpers;
+import org.apache.iceberg.aliyun.AliyunProperties;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.io.InputFile;
+import org.apache.iceberg.io.OutputFile;
+import org.apache.iceberg.relocated.com.google.common.io.ByteStreams;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestOSSOutputFile extends AliyunOSSTestBase {
+
+  private final OSS ossClient = ossClient().get();
+  private final Random random = ThreadLocalRandom.current();
+  private final AliyunProperties aliyunProperties = new AliyunProperties();
+
+  @Test
+  public void testWriteFile() throws IOException {
+    OSSURI uri = randomURI();
+
+    int dataSize = 1024 * 1024 * 10;
+    byte[] data = randomData(dataSize);
+
+    OutputFile out = OSSOutputFile.fromLocation(ossClient, uri.location(), 
aliyunProperties);
+    try (OutputStream os = out.createOrOverwrite()) {
+      IOUtils.write(data, os);
+    }
+    InputFile in = out.toInputFile();
+    Assert.assertTrue("OSS file should exist", in.exists());
+
+    byte[] actual = new byte[dataSize];
+    ByteStreams.readFully(ossClient.getObject(uri.bucket(), 
uri.key()).getObjectContent(), actual);
+    Assert.assertArrayEquals("Object content should match", data, actual);
+  }
+
+  @Test
+  public void testFromLocation() {
+    AssertHelpers.assertThrows(
+        "Should catch null location when creating oss output file",
+        NullPointerException.class,
+        "location cannot be null",
+        () -> OSSOutputFile.fromLocation(ossClient, null, aliyunProperties));
+  }
+
+  @Test
+  public void testCreate() {
+    OSSURI uri = randomURI();
+
+    int dataSize = 8;
+    byte[] data = randomData(dataSize);
+    writeOSSData(uri, data);
+
+    OutputFile out = OSSOutputFile.fromLocation(ossClient, uri.location(), 
aliyunProperties);
+    AssertHelpers.assertThrows("Should complain about location already 
exists", AlreadyExistsException.class,
+        "Location already exists",
+        () -> out.create());
+  }
+
+  @Test
+  public void testCreateOrOverwrite() throws Exception {
+    OSSURI uri = randomURI();
+
+    int dataSize = 8;
+    byte[] data = randomData(dataSize);
+    writeOSSData(uri, data);
+
+    int actualSize = 1024;
+    byte[] actual = randomData(actualSize);
+    OutputFile out = OSSOutputFile.fromLocation(ossClient, uri.location(), 
aliyunProperties);
+    try (OutputStream os = out.createOrOverwrite()) {
+      IOUtils.write(actual, os);
+    }
+
+    Assert.assertEquals(String.format("Should overwrite object length from %d 
to %d", dataSize, actualSize),
+        ossClient.getObject(uri.bucket(), 
uri.key()).getObjectMetadata().getContentLength(), actualSize);
+
+    byte[] expect = new byte[actualSize];
+    ByteStreams.readFully(ossClient.getObject(uri.bucket(), 
uri.key()).getObjectContent(), expect);
+    Assert.assertArrayEquals("Should overwrite object content", actual, 
expect);
+  }
+
+  @Test
+  public void testLocation() {
+    OSSURI uri = randomURI();
+    OutputFile out = new OSSOutputFile(ossClient, uri, aliyunProperties);
+    Assert.assertEquals("Location should match", uri.location(), 
out.location());
+  }
+
+  @Test
+  public void testToInputFile() {
+    OutputFile out = new OSSOutputFile(ossClient, randomURI(), 
aliyunProperties);
+    InputFile in = out.toInputFile();
+    Assert.assertTrue("Should be an instance of OSSInputFile", in instanceof 
OSSInputFile);

Review comment:
       I think we'd better to assert more informations such as: 
   * the location of output file and input file;
   * the length of the output file and input file;
   * the existence of the input file.

##########
File path: 
aliyun/src/test/java/org/apache/iceberg/aliyun/oss/TestOSSOutputFile.java
##########
@@ -0,0 +1,137 @@
+/*
+ * 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.iceberg.aliyun.oss;
+
+import com.aliyun.oss.OSS;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Random;
+import java.util.UUID;
+import java.util.concurrent.ThreadLocalRandom;
+import org.apache.commons.io.IOUtils;
+import org.apache.iceberg.AssertHelpers;
+import org.apache.iceberg.aliyun.AliyunProperties;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.io.InputFile;
+import org.apache.iceberg.io.OutputFile;
+import org.apache.iceberg.relocated.com.google.common.io.ByteStreams;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestOSSOutputFile extends AliyunOSSTestBase {
+
+  private final OSS ossClient = ossClient().get();
+  private final Random random = ThreadLocalRandom.current();
+  private final AliyunProperties aliyunProperties = new AliyunProperties();
+
+  @Test
+  public void testWriteFile() throws IOException {
+    OSSURI uri = randomURI();
+
+    int dataSize = 1024 * 1024 * 10;
+    byte[] data = randomData(dataSize);
+
+    OutputFile out = OSSOutputFile.fromLocation(ossClient, uri.location(), 
aliyunProperties);
+    try (OutputStream os = out.createOrOverwrite()) {
+      IOUtils.write(data, os);
+    }
+    InputFile in = out.toInputFile();
+    Assert.assertTrue("OSS file should exist", in.exists());
+
+    byte[] actual = new byte[dataSize];
+    ByteStreams.readFully(ossClient.getObject(uri.bucket(), 
uri.key()).getObjectContent(), actual);
+    Assert.assertArrayEquals("Object content should match", data, actual);
+  }
+
+  @Test
+  public void testFromLocation() {
+    AssertHelpers.assertThrows(
+        "Should catch null location when creating oss output file",
+        NullPointerException.class,
+        "location cannot be null",
+        () -> OSSOutputFile.fromLocation(ossClient, null, aliyunProperties));
+  }
+
+  @Test
+  public void testCreate() {
+    OSSURI uri = randomURI();
+
+    int dataSize = 8;
+    byte[] data = randomData(dataSize);
+    writeOSSData(uri, data);
+
+    OutputFile out = OSSOutputFile.fromLocation(ossClient, uri.location(), 
aliyunProperties);
+    AssertHelpers.assertThrows("Should complain about location already 
exists", AlreadyExistsException.class,
+        "Location already exists",
+        () -> out.create());
+  }
+
+  @Test
+  public void testCreateOrOverwrite() throws Exception {
+    OSSURI uri = randomURI();
+
+    int dataSize = 8;
+    byte[] data = randomData(dataSize);
+    writeOSSData(uri, data);
+
+    int actualSize = 1024;
+    byte[] actual = randomData(actualSize);
+    OutputFile out = OSSOutputFile.fromLocation(ossClient, uri.location(), 
aliyunProperties);
+    try (OutputStream os = out.createOrOverwrite()) {
+      IOUtils.write(actual, os);
+    }
+
+    Assert.assertEquals(String.format("Should overwrite object length from %d 
to %d", dataSize, actualSize),
+        ossClient.getObject(uri.bucket(), 
uri.key()).getObjectMetadata().getContentLength(), actualSize);

Review comment:
       I think this `ossClient.getObject(uri.bucket(), 
uri.key()).getObjectMetadata().getContentLength()` can be simplified as 
`out.toInputFile().getLength()`.

##########
File path: 
aliyun/src/test/java/org/apache/iceberg/aliyun/oss/TestOSSOutputFile.java
##########
@@ -0,0 +1,137 @@
+/*
+ * 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.iceberg.aliyun.oss;
+
+import com.aliyun.oss.OSS;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Random;
+import java.util.UUID;
+import java.util.concurrent.ThreadLocalRandom;
+import org.apache.commons.io.IOUtils;
+import org.apache.iceberg.AssertHelpers;
+import org.apache.iceberg.aliyun.AliyunProperties;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.io.InputFile;
+import org.apache.iceberg.io.OutputFile;
+import org.apache.iceberg.relocated.com.google.common.io.ByteStreams;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestOSSOutputFile extends AliyunOSSTestBase {
+
+  private final OSS ossClient = ossClient().get();
+  private final Random random = ThreadLocalRandom.current();
+  private final AliyunProperties aliyunProperties = new AliyunProperties();
+
+  @Test
+  public void testWriteFile() throws IOException {
+    OSSURI uri = randomURI();
+
+    int dataSize = 1024 * 1024 * 10;
+    byte[] data = randomData(dataSize);
+
+    OutputFile out = OSSOutputFile.fromLocation(ossClient, uri.location(), 
aliyunProperties);
+    try (OutputStream os = out.createOrOverwrite()) {
+      IOUtils.write(data, os);
+    }
+    InputFile in = out.toInputFile();
+    Assert.assertTrue("OSS file should exist", in.exists());
+
+    byte[] actual = new byte[dataSize];
+    ByteStreams.readFully(ossClient.getObject(uri.bucket(), 
uri.key()).getObjectContent(), actual);
+    Assert.assertArrayEquals("Object content should match", data, actual);
+  }
+
+  @Test
+  public void testFromLocation() {
+    AssertHelpers.assertThrows(
+        "Should catch null location when creating oss output file",
+        NullPointerException.class,
+        "location cannot be null",
+        () -> OSSOutputFile.fromLocation(ossClient, null, aliyunProperties));
+  }
+
+  @Test
+  public void testCreate() {
+    OSSURI uri = randomURI();
+
+    int dataSize = 8;
+    byte[] data = randomData(dataSize);
+    writeOSSData(uri, data);
+
+    OutputFile out = OSSOutputFile.fromLocation(ossClient, uri.location(), 
aliyunProperties);
+    AssertHelpers.assertThrows("Should complain about location already 
exists", AlreadyExistsException.class,
+        "Location already exists",
+        () -> out.create());

Review comment:
       Nit:  Lambda can be replaced with method reference.

##########
File path: 
aliyun/src/test/java/org/apache/iceberg/aliyun/oss/TestOSSOutputFile.java
##########
@@ -0,0 +1,137 @@
+/*
+ * 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.iceberg.aliyun.oss;
+
+import com.aliyun.oss.OSS;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Random;
+import java.util.UUID;
+import java.util.concurrent.ThreadLocalRandom;
+import org.apache.commons.io.IOUtils;
+import org.apache.iceberg.AssertHelpers;
+import org.apache.iceberg.aliyun.AliyunProperties;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.io.InputFile;
+import org.apache.iceberg.io.OutputFile;
+import org.apache.iceberg.relocated.com.google.common.io.ByteStreams;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestOSSOutputFile extends AliyunOSSTestBase {
+
+  private final OSS ossClient = ossClient().get();
+  private final Random random = ThreadLocalRandom.current();
+  private final AliyunProperties aliyunProperties = new AliyunProperties();
+
+  @Test
+  public void testWriteFile() throws IOException {
+    OSSURI uri = randomURI();
+
+    int dataSize = 1024 * 1024 * 10;
+    byte[] data = randomData(dataSize);
+
+    OutputFile out = OSSOutputFile.fromLocation(ossClient, uri.location(), 
aliyunProperties);
+    try (OutputStream os = out.createOrOverwrite()) {
+      IOUtils.write(data, os);
+    }
+    InputFile in = out.toInputFile();
+    Assert.assertTrue("OSS file should exist", in.exists());
+
+    byte[] actual = new byte[dataSize];
+    ByteStreams.readFully(ossClient.getObject(uri.bucket(), 
uri.key()).getObjectContent(), actual);
+    Assert.assertArrayEquals("Object content should match", data, actual);
+  }
+
+  @Test
+  public void testFromLocation() {
+    AssertHelpers.assertThrows(
+        "Should catch null location when creating oss output file",
+        NullPointerException.class,
+        "location cannot be null",
+        () -> OSSOutputFile.fromLocation(ossClient, null, aliyunProperties));
+  }
+
+  @Test
+  public void testCreate() {
+    OSSURI uri = randomURI();
+
+    int dataSize = 8;
+    byte[] data = randomData(dataSize);
+    writeOSSData(uri, data);
+
+    OutputFile out = OSSOutputFile.fromLocation(ossClient, uri.location(), 
aliyunProperties);
+    AssertHelpers.assertThrows("Should complain about location already 
exists", AlreadyExistsException.class,
+        "Location already exists",
+        () -> out.create());
+  }
+
+  @Test
+  public void testCreateOrOverwrite() throws Exception {
+    OSSURI uri = randomURI();
+
+    int dataSize = 8;
+    byte[] data = randomData(dataSize);
+    writeOSSData(uri, data);
+
+    int actualSize = 1024;
+    byte[] actual = randomData(actualSize);
+    OutputFile out = OSSOutputFile.fromLocation(ossClient, uri.location(), 
aliyunProperties);
+    try (OutputStream os = out.createOrOverwrite()) {
+      IOUtils.write(actual, os);

Review comment:
       Nit:  Replace the apache common io class with guava's `ByteStream#copy`

##########
File path: aliyun/src/main/java/org/apache/iceberg/aliyun/oss/BaseOSSFile.java
##########
@@ -23,16 +23,19 @@
 import com.aliyun.oss.OSSErrorCode;
 import com.aliyun.oss.OSSException;
 import com.aliyun.oss.model.SimplifiedObjectMeta;
+import org.apache.iceberg.aliyun.AliyunProperties;
 import org.apache.iceberg.relocated.com.google.common.base.MoreObjects;
 
 abstract class BaseOSSFile {
   private final OSS client;
   private final OSSURI uri;
+  private final AliyunProperties aliyunProperties;
   private SimplifiedObjectMeta metadata;
 
-  BaseOSSFile(OSS client, OSSURI uri) {
+  BaseOSSFile(OSS client, OSSURI uri, AliyunProperties aliyunProperties) {

Review comment:
       Could we keep the original constructor and introduce an extra 
`BaseOSSFile` constructor with the newly introduced  `aliyunProperties` 
argument so that we don't need to change the unrelated classes (such as 
`TestOSSInputFile`, `OSSInputFile` etc) ? 

##########
File path: 
aliyun/src/test/java/org/apache/iceberg/aliyun/oss/TestOSSOutputFile.java
##########
@@ -0,0 +1,137 @@
+/*
+ * 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.iceberg.aliyun.oss;
+
+import com.aliyun.oss.OSS;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Random;
+import java.util.UUID;
+import java.util.concurrent.ThreadLocalRandom;
+import org.apache.commons.io.IOUtils;
+import org.apache.iceberg.AssertHelpers;
+import org.apache.iceberg.aliyun.AliyunProperties;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.io.InputFile;
+import org.apache.iceberg.io.OutputFile;
+import org.apache.iceberg.relocated.com.google.common.io.ByteStreams;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestOSSOutputFile extends AliyunOSSTestBase {
+
+  private final OSS ossClient = ossClient().get();
+  private final Random random = ThreadLocalRandom.current();
+  private final AliyunProperties aliyunProperties = new AliyunProperties();
+
+  @Test
+  public void testWriteFile() throws IOException {
+    OSSURI uri = randomURI();
+
+    int dataSize = 1024 * 1024 * 10;
+    byte[] data = randomData(dataSize);
+
+    OutputFile out = OSSOutputFile.fromLocation(ossClient, uri.location(), 
aliyunProperties);
+    try (OutputStream os = out.createOrOverwrite()) {
+      IOUtils.write(data, os);

Review comment:
       Same comment as this one: 
https://github.com/apache/iceberg/pull/3404#discussion_r739861968




-- 
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]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to