8000 Add HdfsFlagTarget by fenimore · Pull Request #2559 · spotify/luigi · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add HdfsFlagTarget #2559

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

Merged
merged 6 commits into from
Oct 30, 2018
Merged
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
35 changes: 35 additions & 0 deletions luigi/contrib/hdfs/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,38 @@ def _is_writable(self, path):
return True
except hdfs_clients.HDFSCliError:
return False


class HdfsFlagTarget(HdfsTarget):
"""
Defines a target directory with a flag-file (defaults to `_SUCCESS`) used
to signify job success.

This checks for two things:

* the path exists (just like the HdfsTarget)
* the _SUCCESS file exists within the directory.

Because Hadoop outputs into a directory and not a single file,
the path is assumed to be a directory.
"""
def __init__(self, path, format=None, client=None, flag='_SUCCESS'):
"""
Initializes a HdfsFlagTarget.

:param path: the directory where the files are stored.
:type path: str
:param client:
:type client:
:param flag:
:type flag: str
"""
if path[-1] != "/":
raise ValueError("HdfsFlagTarget requires the path to be to a "
"directory. It must end with a slash ( / ).")
super(HdfsFlagTarget, self).__init__(path, format, client)
self.flag = flag

def exists(self):
hadoopSemaphore = self.path + self.flag
return self.fs.exists(hadoopSemaphore)
18 changes: 18 additions & 0 deletions test/contrib/hdfs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,24 @@ def test_pickle(self):
t = hdfs.HdfsTarget("/tmp/dir")
pickle.dumps(t)

def test_flag_target(self):
target = hdfs.HdfsFlagTarget("/some/dir/", format=format)
if target.exists():
target.remove(skip_trash=True)
self.assertFalse(target.exists())

t1 = hdfs.HdfsTarget(target.path + "part-00000", format=format)
with t1.open('w'):
pass
t2 = hdfs.HdfsTarget(target.path + "_SUCCESS", format=format)
with t2.open('w'):
pass
self.assertTrue(target.exists())

def test_flag_target_fails_if_not_directory(self):
with self.assertRaises(ValueError):
hdfs.HdfsFlagTarget("/home/file.txt")


@attr('minicluster')
class HdfsTargetTest(MiniClusterTestCase, HdfsTargetTestMixin):
Expand Down
0