Python HDFS Snakebite:方法仅适用于打印
Python HDFS Snakebite : Methods work only with print
我正在使用
的 snakebite 客户端
https://github.com/spotify/snakebite
当我尝试在 hdfs 中创建目录或移动文件时,我注意到一个奇怪的行为。这是我的代码。它所做的只是将源目录的内容移动到目标目录。最后,显示目标目录的内容
def purge_pending(self,source_dir,dest_dir):
if(self.hdfs_serpent.test(path=self.root_dir+"/"+source_dir, exists=True, directory=True)):
print "Source exists ",self.root_dir+source_dir
for x in self.hdfs_serpent.ls([self.root_dir+source_dir]):
print x['path']
else:
print "Source does not exist ",self.root_dir+"/"+source_dir
return
if(self.hdfs_serpent.test(path=self.root_dir+"/"+dest_dir, exists=True, directory=True)):
print "Destination exists ",self.root_dir+dest_dir
else:
print "Destination does not exist ",self.root_dir+dest_dir
print "Will be created"
for y in self.hdfs_serpent.mkdir([self.root_dir+dest_dir],create_parent=True):
print y
for src in self.hdfs_serpent.ls([self.root_dir+source_dir]):
print src['path'].split("/")[-1]
for y in self.hdfs_serpent.rename([src['path']],self.root_dir+dest_dir+"/"+src['path'].split("/")[-1]):
print y
for x in self.hdfs_serpent.ls([self.root_dir+dest_dir]):
print x['path']
这是目标不存在时的示例输出
Source exists /root/source
/root/source/208560.json
/root/source/208571.json
/root/source/208574.json
/root/source/208581.json
/root/source/208707.json
Destination does not exist /root/dest
Will be created
{'path':'/research/dest/'}
208560.json
{'path':'/research/dest/208560.json'}
208571.json
{'path':'/research/dest/208571.json'}
208574.json
{'path':'/research/dest/208574.json'}
208581.json
{'path':'/research/dest/208581.json'}
208707.json
{'path':'/research/dest/208707.json'}
奇怪的是我必须将这些打印语句放入其中,否则什么都不起作用。所以
self.hdfs_serpent.mkdir([self.root_dir+dest_dir],create_parent=True)
不起作用,但是
for y in self.hdfs_serpent.mkdir([self.root_dir+dest_dir],create_parent=True):
print y
有!!!
也一样
self.hdfs_serpent.rename([src['path']],self.root_dir+dest_dir+"/"+src['path'].split("/")[-1])
上面的不起作用,但下面的起作用了
for y in self.hdfs_serpent.rename([src['path']],self.root_dir+dest_dir+"/"+src['path'].split("/")[-1]):
print y
这是一个错误吗?我做错了什么吗?
这看起来是设计使然,因为 documentation states that most of the objects returned by the methods are generators. Therefore, the function won't usually do anything until the values have been consumed with next() 而 for
是隐含的。
我正在使用
的 snakebite 客户端https://github.com/spotify/snakebite
当我尝试在 hdfs 中创建目录或移动文件时,我注意到一个奇怪的行为。这是我的代码。它所做的只是将源目录的内容移动到目标目录。最后,显示目标目录的内容
def purge_pending(self,source_dir,dest_dir):
if(self.hdfs_serpent.test(path=self.root_dir+"/"+source_dir, exists=True, directory=True)):
print "Source exists ",self.root_dir+source_dir
for x in self.hdfs_serpent.ls([self.root_dir+source_dir]):
print x['path']
else:
print "Source does not exist ",self.root_dir+"/"+source_dir
return
if(self.hdfs_serpent.test(path=self.root_dir+"/"+dest_dir, exists=True, directory=True)):
print "Destination exists ",self.root_dir+dest_dir
else:
print "Destination does not exist ",self.root_dir+dest_dir
print "Will be created"
for y in self.hdfs_serpent.mkdir([self.root_dir+dest_dir],create_parent=True):
print y
for src in self.hdfs_serpent.ls([self.root_dir+source_dir]):
print src['path'].split("/")[-1]
for y in self.hdfs_serpent.rename([src['path']],self.root_dir+dest_dir+"/"+src['path'].split("/")[-1]):
print y
for x in self.hdfs_serpent.ls([self.root_dir+dest_dir]):
print x['path']
这是目标不存在时的示例输出
Source exists /root/source
/root/source/208560.json
/root/source/208571.json
/root/source/208574.json
/root/source/208581.json
/root/source/208707.json
Destination does not exist /root/dest
Will be created
{'path':'/research/dest/'}
208560.json
{'path':'/research/dest/208560.json'}
208571.json
{'path':'/research/dest/208571.json'}
208574.json
{'path':'/research/dest/208574.json'}
208581.json
{'path':'/research/dest/208581.json'}
208707.json
{'path':'/research/dest/208707.json'}
奇怪的是我必须将这些打印语句放入其中,否则什么都不起作用。所以
self.hdfs_serpent.mkdir([self.root_dir+dest_dir],create_parent=True)
不起作用,但是
for y in self.hdfs_serpent.mkdir([self.root_dir+dest_dir],create_parent=True):
print y
有!!!
也一样self.hdfs_serpent.rename([src['path']],self.root_dir+dest_dir+"/"+src['path'].split("/")[-1])
上面的不起作用,但下面的起作用了
for y in self.hdfs_serpent.rename([src['path']],self.root_dir+dest_dir+"/"+src['path'].split("/")[-1]):
print y
这是一个错误吗?我做错了什么吗?
这看起来是设计使然,因为 documentation states that most of the objects returned by the methods are generators. Therefore, the function won't usually do anything until the values have been consumed with next() 而 for
是隐含的。