在windows上訪問hadoop系統 解決 Login failed: Cannot run program "whoami": CreateProcess error=2

在windows上訪問hadoop系統
按照hadoop的例子HadoopDFSFileReadWrite.java,在eclipse裏建立項目,拷貝lib導入projext,創建conf拷貝到本地作爲src一部分
修改hadoop-site.xml中的fs.default.name屬性爲namenode的ip形式

    static void usage() {
        System.out.println("Usage : Client <inputfile> <output file>");
        System.exit(1);
    }

    static void printAndExit(String str) {
        System.err.println(str);
        System.exit(1);
    }

    public static void main(String[] argv) throws IOException {
        for(String arg : argv) {
            System.out.println("arg="+arg);
        }
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(conf);

        if (argv.length != 2) usage();

        // Hadoop DFS deals with Path
        Path inFile = new Path(argv[0]);
        Path outFile = new Path(argv[1]);

        // Check if input/output are valid
        if (!fs.exists(inFile)) {
            printAndExit("Input file not found");
        }
        if (!fs.isFile(inFile)) {
            printAndExit("Input should be a file");
        }
        if (fs.exists(outFile)) {
            printAndExit("Output already exists");
        }

        // Read from and write to new file
        FSDataInputStream in = fs.open(inFile);
        FSDataOutputStream out = fs.create(outFile);
        byte buffer[] = new byte[256];
        try {
            int bytesRead = 0;
            while ((bytesRead = in.read(buffer)) > 0) {
                out.write(buffer, 0, bytesRead);
            }
        } catch (IOException e) {
            System.out.println("Error while copying file");
        } finally {
            in.close();
            out.close();
        }
    }

運行報錯:
Exception in thread "main" java.io.IOException: Login failed: Cannot run program "whoami": CreateProcess error=2, ?????????
 at org.apache.hadoop.dfs.DFSClient.createNamenode(DFSClient.java:124)
 at org.apache.hadoop.dfs.DFSClient.<init>(DFSClient.java:143)
 at org.apache.hadoop.dfs.DistributedFileSystem.initialize(DistributedFileSystem.java:65)
 at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:166)
 at org.apache.hadoop.fs.FileSystem.getNamed(FileSystem.java:122)
 at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:94)
 at com.chua.hadoop.client.Client.main(Client.java:25)

網上查資料,發現windows上沒有whoami命令(即使有估計也是訪問不了,因爲hdfs的文件權限形式;後來試了一下隨便填了一個用戶去做,報錯:org.apache.hadoop.fs.permission.AccessControlException: Permission denied),
在hadoop-site.xml里加入
 <property>
  <name>hadoop.job.ugi</name>
  <value>zxf,zxf</value>
 </property>
 
 

還是報錯
java.net.ConnectException: Connection refused

此時我又把代碼放到linux下運行,還是同樣的錯誤,期間讓費了很多時間,看了很多blog,都沒有相關資料,我看了hadoop中關於Permissions and Security的資料,也沒有看出問題.
於是把代碼放到linux下,並把把fs.default.name改回到namenode的運行參數,即:localhost:prot的形式,這次運行成功,我納悶
分別telnet localhost prot與telnet ip port,發現localhost的可以連上,而ip的連不上,問題集中在這裏了,我沒有socket編程經驗,所以問了旁邊的人,說socket listener的建立分形式的,像我這樣的情況,listener只能創建本地socket.
這次修改hdfs環境的hadoop-site.xml將fs.default.name改成ip形式,重啓hdfs
然後把clinet的hadoop-site.xml中的fs.default.name改爲ip,運行成功,這個問題困擾了一天時間.
windows環境的clinet運行也同樣成功了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章