2

のスネークバイトクライアントを使用しています

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

これはバグですか?私は何か間違ったことをしていますか?

4

1 に答える 1

3

ドキュメントには、メソッドによって返されるオブジェクトのほとんどがジェネレーターであると記載されているため、これは仕様によるものと思われます。したがって、値が暗黙的に実行されるnext()で消費されるまで、関数は通常何も実行しません。for

于 2016-05-12T16:29:04.343 に答える