解决问题java.io.IOException: (null) entry in command string: null chmod 0644 E:\yhtt1.txt

在对HDFS的api进行操作时,在IDEA上创建了maven工程并导入jar包,所加依赖如下:

<dependency>
  <groupId>org.apache.hadoop</groupId>
  <artifactId>hadoop-common</artifactId>
  <version>2.7.7</version>
</dependency>
<dependency>
  <groupId>org.apache.hadoop</groupId>
  <artifactId>hadoop-hdfs</artifactId>
  <version>2.7.7</version>
</dependency>
<dependency>
  <groupId>org.apache.hadoop</groupId>
  <artifactId>hadoop-client</artifactId>
  <version>2.7.7</version>
</dependency>

在IDEA中写了个上传文件的测试类:代码如下

    @Test
    public void putData() throws  Exception{
        Configuration configuration=new Configuration();
        configuration.set("fs.defaultFS","hdfs://hadoop:8020");
        FileSystem fileSystem=FileSystem.get(configuration);
        //上传文件到/hello/mydir/test
        fileSystem.copyFromLocalFile(new Path("file:///e:\\yhtt.txt"),new Path("/hello/mydir/test"));
        fileSystem.close();
    }

游览器访问hadoop:50070看到文件已经成功上传到服务器上:(注意:此处访问的hadoop是我在host文件进行了映射,你只需要写你的ip地址即可),我用的是hadoop-2.7.7版本,每个版本的端口号不一致,例如3版本的端口号就是9870,这个自己需要明确

 再次写了个下载文件的测试类:

@Test
    public void getFileToLocal()throws  IOException{
        Configuration configuration=new Configuration();
        configuration.set("fs.defaultFS","hdfs://hadoop:8020");
        //创建fileSystem
        FileSystem fileSystem=FileSystem.get(configuration);

        fileSystem.copyToLocalFile(new Path("/hello/mydir/test/yhtt.txt"), new Path("file:///e:\\yhtt1.txt"));
        fileSystem.close();
    }

但在运行程序后,IDEA出现报错信息:java.io.IOException: (null) entry in command string: null chmod 0644 E:\yhtt1.txt

进一步排查问题:

1.首先检查了在window中是否配置了hadoop的环境变量,以及是否加入path中。

在Path中:

 

 2.确保自己的hadoop文件目录放到一个没有中文没有空格的路径下,这一点很重要,不可有中文路径,我用的是hadoop2.7.7(windows版)

在确保环境变量没有问题后,发现自己忘记把hadoop.dll文件放到c:\windows\system32目录中了,hadoop.dll可以在github上下载:GitHub - 4ttty/winutils: Windows binaries for Hadoop versions (built from the git commit ID used for the ASF relase)在放到system32后,再次运行测试类,发现可以从服务器上下载文件到指定盘符了

下载时对文件进行了重命名,否则IDEA会报错,另外需要注意的时,检验自己是否下载成功的依据就是:看指定下载盘符是否有.crc后缀的文件,即此文件就是判断你文件下载成功的一个检验标准。

在 java 中操作 HDFS,主要涉及以下 Class:

Configuration:该类的对象封转了客户端或者服务器的配置;

FileSystem:该类的对象是一个文件系统对象,可以用该对象的一些方法来对文件进行操作,通过 FileSystem 的静态方法 get 获得该对象。

FileSystem fs = FileSystem.get(conf)

get 方法从 conf 中的一个参数 fs.defaultFS 的配置值判断具体是什么类型的文件系统。如果我们的代码中没有指定 fs.defaultFS,并且工程 classpath下也没有给定相应的配置,conf中的默认值就来自于hadoop的jar包中的core-default.xml , 默 认 值 为 : file:/// , 则 获 取 的 将 不 是 一 个DistributedFileSystem 的实例,而是一个本地文件系统的客户端对象

获取FileSystem的方式:

Configuration configuration=new Configuration();
configuration.set("fs.defaultFS","hdfs://hadoop:8020");
FileSystem fileSystem=FileSystem.get(configuration);
System.out.println(fileSystem.toString());

 最终我们获得打印出该对象的地址值

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值