You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2022/08/04 17:20:51 UTC

[GitHub] [iceberg] Fokko commented on a diff in pull request #5332: [Python] S3fsFileIO, a FileIO that wraps s3fs

Fokko commented on code in PR #5332:
URL: https://github.com/apache/iceberg/pull/5332#discussion_r938059573


##########
python/pyiceberg/io/s3fs.py:
##########
@@ -0,0 +1,235 @@
+# 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.
+"""FileIO implementation for reading and writing table files that uses s3fs"""
+
+from typing import Optional, Union
+
+from s3fs import S3FileSystem
+
+from pyiceberg.io.base import (
+    FileIO,
+    InputFile,
+    InputStream,
+    OutputFile,
+    OutputStream,
+)
+
+
+class S3fsInputStream(InputStream):
+    """A seekable wrapper for reading an S3 Object that abides by the InputStream protocol
+
+    Args:
+        s3_object(s3fs.core.S3File): An s3 object
+    """
+
+    def __init__(self, s3_object):
+        self._s3_object = s3_object
+
+    def read(self, size: int = -1) -> bytes:
+        """Read the byte content of the s3 object
+
+        Args:
+            size (int, optional): The number of bytes to read. Defaults to -1 which reads the entire file.
+
+        Returns:
+            bytes: The byte content of the file
+        """
+        return self._s3_object.read(length=size)
+
+    def seek(self, offset: int, whence: int = 0) -> int:
+        return self._s3_object.seek(loc=offset, whence=whence)
+
+    def tell(self) -> int:
+        return self._s3_object.tell()
+
+    def closed(self) -> bool:
+        return self._s3_object.closed
+
+    def close(self) -> None:
+        self._s3_object.close()
+
+
+class S3fsOutputStream(OutputStream):

Review Comment:
   Same as above, the calls map 1-to-1.



##########
python/pyiceberg/io/s3fs.py:
##########
@@ -0,0 +1,235 @@
+# 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.
+"""FileIO implementation for reading and writing table files that uses s3fs"""
+
+from typing import Optional, Union
+
+from s3fs import S3FileSystem
+
+from pyiceberg.io.base import (
+    FileIO,
+    InputFile,
+    InputStream,
+    OutputFile,
+    OutputStream,
+)
+
+
+class S3fsInputStream(InputStream):

Review Comment:
   Ideally, we don't want this class since it is almost a copy of the Stream of S3FS itself.



##########
python/pyiceberg/io/s3fs.py:
##########
@@ -0,0 +1,235 @@
+# 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.
+"""FileIO implementation for reading and writing table files that uses s3fs"""
+
+from typing import Optional, Union
+
+from s3fs import S3FileSystem
+
+from pyiceberg.io.base import (
+    FileIO,
+    InputFile,
+    InputStream,
+    OutputFile,
+    OutputStream,
+)
+
+
+class S3fsInputStream(InputStream):
+    """A seekable wrapper for reading an S3 Object that abides by the InputStream protocol
+
+    Args:
+        s3_object(s3fs.core.S3File): An s3 object
+    """
+
+    def __init__(self, s3_object):
+        self._s3_object = s3_object
+
+    def read(self, size: int = -1) -> bytes:
+        """Read the byte content of the s3 object
+
+        Args:
+            size (int, optional): The number of bytes to read. Defaults to -1 which reads the entire file.
+
+        Returns:
+            bytes: The byte content of the file
+        """
+        return self._s3_object.read(length=size)
+
+    def seek(self, offset: int, whence: int = 0) -> int:
+        return self._s3_object.seek(loc=offset, whence=whence)
+
+    def tell(self) -> int:
+        return self._s3_object.tell()
+
+    def closed(self) -> bool:
+        return self._s3_object.closed
+
+    def close(self) -> None:
+        self._s3_object.close()
+
+
+class S3fsOutputStream(OutputStream):
+    """A wrapper for writing an S3 Object that abides by the OutputStream protocol
+
+    Args:
+        s3_object(s3fs.core.S3FileSystem): An s3 object
+    """
+
+    def __init__(self, s3_object):
+        self._s3_object = s3_object
+
+    def write(self, b: bytes) -> int:
+        """Write to the S3 Object
+
+        Args:
+            b(bytes): The bytes to write to the S3 Object
+
+        Returns:
+            int: The number of bytes written
+
+        Raises:
+            ValueError: When the file is closed
+        """
+        return self._s3_object.write(b)
+
+    def closed(self) -> bool:
+        """Returns whether the stream is closed or not"""
+        return self._s3_object.closed
+
+    def close(self) -> None:
+        """Closes the stream and uploads the bytes to S3"""
+        self._s3_object.close()
+
+
+class S3fsInputFile(InputFile):
+    """An input file implementation for the S3fsFileIO
+
+    Args:
+        location(str): An S3 URI
+
+    Attributes:
+        location(str): An S3 URI
+    """
+
+    def __init__(self, location: str, s3):
+        self._s3 = s3
+        super().__init__(location=location)
+
+    def __len__(self) -> int:
+        """Returns the total length of the file, in bytes"""
+        object_info = self._s3.info(self.location)
+        if object_info.get("Size"):  # s3fs versions seem inconsistent on the case used for size

Review Comment:
   How about some walrus?
   ```suggestion
           if size := object_info.get("Size"):  # s3fs versions seem inconsistent on the case used for size
   ```



##########
python/pyiceberg/io/s3fs.py:
##########
@@ -0,0 +1,235 @@
+# 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.
+"""FileIO implementation for reading and writing table files that uses s3fs"""
+
+from typing import Optional, Union
+
+from s3fs import S3FileSystem
+
+from pyiceberg.io.base import (
+    FileIO,
+    InputFile,
+    InputStream,
+    OutputFile,
+    OutputStream,
+)
+
+
+class S3fsInputStream(InputStream):
+    """A seekable wrapper for reading an S3 Object that abides by the InputStream protocol
+
+    Args:
+        s3_object(s3fs.core.S3File): An s3 object
+    """
+
+    def __init__(self, s3_object):
+        self._s3_object = s3_object
+
+    def read(self, size: int = -1) -> bytes:
+        """Read the byte content of the s3 object
+
+        Args:
+            size (int, optional): The number of bytes to read. Defaults to -1 which reads the entire file.
+
+        Returns:
+            bytes: The byte content of the file
+        """
+        return self._s3_object.read(length=size)
+
+    def seek(self, offset: int, whence: int = 0) -> int:
+        return self._s3_object.seek(loc=offset, whence=whence)
+
+    def tell(self) -> int:
+        return self._s3_object.tell()
+
+    def closed(self) -> bool:

Review Comment:
   We don't have closed anymore (this is actually a property, but did not play well with the protocol).



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org