簡體   English   中英

Python HDFS Snakebite:方法僅適用於打印

[英]Python HDFS Snakebite : Methods work only with print

我正在使用來自的蛇咬客戶端

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

這是一個錯誤嗎? 難道我做錯了什么?

這似乎是設計使然,因為文檔指出方法所返回的大多數對象都是生成器。 因此,該功能通常不會直到值已經消耗與做任何事情的next()for做隱式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM