HdfsApi中copyToLocalFile中报空指针

                             HdfsApi中copyToLocalFile中报空指针

 

环境:Hadoop2.6.0-cdh5.16.2、jdk1.8

代码:

package com.xk.Hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.net.URI;

public class HdfsApi {

    
    /**
     * 从文件系统里面copy文件
     * @throws Exception
     */
    public static void copyToLocalFile() throws Exception{
        FileSystem fileSystem = HdfsApi.CreateFileSystem();
        fileSystem.copyToLocalFile(new Path("/out/data1.txt"),new Path("out/data1.txt"));
    }

    /**
     * 创建一个Configuration
     * @return
     */
    public static FileSystem CreateFileSystem() throws Exception{
        Configuration conf = new Configuration();
        conf.set("dfs.replication","1");
        URI uri = new URI("hdfs://hadoop:9000");
        FileSystem fileSystem = FileSystem.get(uri,conf,"hadoop");
        return fileSystem;
    }

    public static void main(String[] args) throws Exception {
        HdfsApi.copyToLocalFile();
    }
}

报错内容:

Exception in thread "main" java.lang.NullPointerException
	at java.lang.ProcessBuilder.start(ProcessBuilder.java:1012)
	at org.apache.hadoop.util.Shell.runCommand(Shell.java:548)
	at org.apache.hadoop.util.Shell.run(Shell.java:507)
	at org.apache.hadoop.util.Shell$ShellCommandExecutor.execute(Shell.java:789)
	at org.apache.hadoop.util.Shell.execCommand(Shell.java:882)
	at org.apache.hadoop.util.Shell.execCommand(Shell.java:865)
	at org.apache.hadoop.fs.RawLocalFileSystem.setPermission(RawLocalFileSystem.java:720)
	at org.apache.hadoop.fs.RawLocalFileSystem$LocalFSFileOutputStream.(RawLocalFileSystem.java:233)
	at org.apache.hadoop.fs.RawLocalFileSystem$LocalFSFileOutputStream.(RawLocalFileSystem.java:217)
	at org.apache.hadoop.fs.RawLocalFileSystem.createOutputStreamWithMode(RawLocalFileSystem.java:315)
	at org.apache.hadoop.fs.RawLocalFileSystem.create(RawLocalFileSystem.java:303)
	at org.apache.hadoop.fs.RawLocalFileSystem.create(RawLocalFileSystem.java:336)
	at org.apache.hadoop.fs.ChecksumFileSystem$ChecksumFSOutputSummer.(ChecksumFileSystem.java:391)
	at org.apache.hadoop.fs.ChecksumFileSystem.create(ChecksumFileSystem.java:454)
	at org.apache.hadoop.fs.ChecksumFileSystem.create(ChecksumFileSystem.java:433)
	at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:926)
	at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:907)
	at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:804)
	at org.apache.hadoop.fs.FileUtil.copy(FileUtil.java:368)
	at org.apache.hadoop.fs.FileUtil.copy(FileUtil.java:341)
	at org.apache.hadoop.fs.FileUtil.copy(FileUtil.java:292)
	at org.apache.hadoop.fs.FileSystem.copyToLocalFile(FileSystem.java:2123)
	at org.apache.hadoop.fs.FileSystem.copyToLocalFile(FileSystem.java:2092)
	at org.apache.hadoop.fs.FileSystem.copyToLocalFile(FileSystem.java:2068)
	at com.xk.Hdfs.HdfsApi.copyToLocalFile(HdfsApi.java:36)
	at com.xk.Hdfs.HdfsApi.main(HdfsApi.java:54)

解决办法:

 

1.从Linux机器上面传输一个Hadoop压缩包到本地,然后解压,并指定环境变量

HdfsApi中copyToLocalFile中报空指针_第1张图片

2.从这里下载winutils.exe和hadoop.dll

Hadoop git地址:https://github.com/steveloughran/winutils/tree/master/hadoop-2.6.0/bin

3.放这两个在%HADOOP_HOME%\bin下

你可能感兴趣的:(Hadoop)