2014-11-05 49 views
0

我有一個腳本偉馳彈了使用Hadoop的CLI將文件上傳到HDFSPython的subprocess.call參數沒有被發現

下面是摘錄:

def hdfs_put(file_path, topic): 
    print(file_path) 
    print(topic) 
    call(["/usr/local/hadoop-2.5.1/bin/hadoop fs -put", file_path, "/" + topic] 
     , shell=True 
     , stderr=STDOUT) 

這裏是我得到(注意參數是不爲空):

avro/hdfs_1_2014-11-05.avro 
hdfs 
-put: Not enough arguments: expected 1 but got 0 
Usage: hadoop fs [generic options] -put [-f] [-p] <localsrc> ... <dst> 

回答

2

shell=True,該args應該包含一個字符串在shell中運行。你可以簡單地刪除shell=True和修復你的命令了一下:

check_call(["/usr/local/hadoop-2.5.1/bin/hadoop", "fs", "-put", file_path, "/" + topic] 
    , stderr=STDOUT) 

我改成了使用check_call,因爲這是最簡單的方式來檢查錯誤。

+0

現在我得到這個錯誤:'''FileNotFoundError:[錯誤2]沒有這樣的文件或目錄: '/usr/local/hadoop-2.5.1/bin/hadoop FS -put' ''' – Lee 2014-11-05 09:31:13

+0

它確實存在,但:(VENV)的bash-4.1#/usr/local/hadoop-2.5 .1/bin/hadoop fs -put -put:沒有足夠的參數:預計1但得到0 用法:hadoop fs [通用選項] -put [-f] [-p] ... Lee 2014-11-05 09:31:54

+0

@JohnDoe:我認爲你忽視了像我一樣分裂命令。請仔細閱讀我的答案,並與您的代碼進行比較。 – 2014-11-05 09:32:44

1

您正在誤用shell = True選項。

爲真時,該命令被傳遞到外殼,因爲它是。你不需要把它分解成一個列表。

def hdfs_put(file_path, topic): 
    print(file_path) 
    print(topic) 
    call("/usr/local/hadoop-2.5.1/bin/hadoop fs -put " + file_path + " /" + topic 
      , shell=True 
      , stderr=STDOUT) 

或者,如果你想擁有的參數作爲一個列表,那麼你將不得不放棄殼牌= TRUE:

def hdfs_put(file_path, topic): 
    print(file_path) 
    print(topic) 
    call(["/usr/local/hadoop-2.5.1/bin/hadoop", "fs", "-put", file_path, "/" + topic] 
     , stderr=STDOUT)