Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added netloc support for hdfs URIs #168

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions smart_open/smart_open_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@ class ParseUri(object):
* s3://my_key:my_secret@my_bucket/my_key
* s3://my_key:my_secret@my_server:my_port@my_bucket/my_key
* hdfs:///path/file
* hdfs://path/file
* hdfs://host/path/file
* hdfs://host:port/path/file
* webhdfs://host:port/path/file
* ./local/path/file
* ~/local/path/file
Expand All @@ -355,6 +356,9 @@ class ParseUri(object):
* file:///home/user/file
* file:///home/user/file.bz2

NOTE: hdfs://path/file does no longer work as it is against the URI
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add more information to comment (when this happens, what HDFS version affected, etc)?

Copy link
Author

@vvaten vvaten Feb 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a generic change that affects all HDFS versions. They do support hdfs://host/path/file URI format. Using hdfs://path/file in smart_open violates this and also violates the URI specification (RFC3986) where the hostname part is always after the '://'. The correct way to refer to local content is hdfs:///path/file instead of hdfs://path/file.

specification. 'path' here is nowadays interpreted as the host of the URI

"""
def __init__(self, uri, default_scheme="file"):
"""
Expand All @@ -370,8 +374,7 @@ def __init__(self, uri, default_scheme="file"):
self.scheme = parsed_uri.scheme if parsed_uri.scheme else default_scheme

if self.scheme == "hdfs":
self.uri_path = parsed_uri.netloc + parsed_uri.path
self.uri_path = "/" + self.uri_path.lstrip("/")
self.uri_path = uri

if not self.uri_path:
raise RuntimeError("invalid HDFS URI: %s" % uri)
Expand Down
28 changes: 20 additions & 8 deletions smart_open/tests/test_smart_open.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,17 @@ def test_hdfs(self, mock_subprocess):
smart_open_object = smart_open.HdfsOpenRead(smart_open.ParseUri("hdfs:///tmp/test.txt"))
smart_open_object.__iter__()
# called with the correct params?
mock_subprocess.Popen.assert_called_with(["hdfs", "dfs", "-text", "/tmp/test.txt"], stdout=mock_subprocess.PIPE)
mock_subprocess.Popen.assert_called_with(["hdfs", "dfs", "-text", "hdfs:///tmp/test.txt"], stdout=mock_subprocess.PIPE)

# second possibility of schema
smart_open_object = smart_open.HdfsOpenRead(smart_open.ParseUri("hdfs://tmp/test.txt"))
# network location host in HDFS schema
smart_open_object = smart_open.HdfsOpenRead(smart_open.ParseUri("hdfs://host/tmp/test.txt"))
smart_open_object.__iter__()
mock_subprocess.Popen.assert_called_with(["hdfs", "dfs", "-text", "/tmp/test.txt"], stdout=mock_subprocess.PIPE)
mock_subprocess.Popen.assert_called_with(["hdfs", "dfs", "-text", "hdfs://host/tmp/test.txt"], stdout=mock_subprocess.PIPE)

# network location host and port in HDFS schema
smart_open_object = smart_open.HdfsOpenRead(smart_open.ParseUri("hdfs://host:port/tmp/test.txt"))
smart_open_object.__iter__()
mock_subprocess.Popen.assert_called_with(["hdfs", "dfs", "-text", "hdfs://host:port/tmp/test.txt"], stdout=mock_subprocess.PIPE)

@mock.patch('smart_open.smart_open_lib.subprocess')
def test_hdfs_encoding(self, mock_subprocess):
Expand Down Expand Up @@ -530,14 +535,21 @@ def test_hdfs(self, mock_subprocess):
smart_open_object.write("test")
# called with the correct params?
mock_subprocess.Popen.assert_called_with(
["hdfs", "dfs", "-put", "-f", "-", "/tmp/test.txt"], stdin=mock_subprocess.PIPE
["hdfs", "dfs", "-put", "-f", "-", "hdfs:///tmp/test.txt"], stdin=mock_subprocess.PIPE
)

# network location host in HDFS schema
smart_open_object = smart_open.HdfsOpenWrite(smart_open.ParseUri("hdfs://host/tmp/test.txt"))
smart_open_object.write("test")
mock_subprocess.Popen.assert_called_with(
["hdfs", "dfs", "-put", "-f", "-", "hdfs://host/tmp/test.txt"], stdin=mock_subprocess.PIPE
)

# second possibility of schema
smart_open_object = smart_open.HdfsOpenWrite(smart_open.ParseUri("hdfs://tmp/test.txt"))
# network location host and port in HDFS schema
smart_open_object = smart_open.HdfsOpenWrite(smart_open.ParseUri("hdfs://host:port/tmp/test.txt"))
smart_open_object.write("test")
mock_subprocess.Popen.assert_called_with(
["hdfs", "dfs", "-put", "-f", "-", "/tmp/test.txt"], stdin=mock_subprocess.PIPE
["hdfs", "dfs", "-put", "-f", "-", "hdfs://host:port/tmp/test.txt"], stdin=mock_subprocess.PIPE
)

@unittest.skip('Not sure how to implement unsecured mode with boto3')
Expand Down