HDFS API复制文件到远程HDFS中,目标文件输出格式差异,导致文件存放位置的差异...

代码中目标路径dst格式的区别比较:

Path dst = new Path("qj1");

Path dst = new Path("hdfs://master:9000/qj2");

代码如下:

package com.lango.mapreduce.example.chainmapper;

import java.io.IOException;

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

public class CopyToHDFS {
    public static void main(String[] args) throws IOException {
        
        Configuration conf = new Configuration();
        conf.set("fs.default.name", "hdfs://master:9000");
        
        FileSystem fs = FileSystem.get(conf);
        
        fs.copyFromLocalFile(new Path("/home/wqj/opt/140702152709log.txt"), new Path("qj1"));
        fs.copyFromLocalFile(new Path("/home/wqj/opt/140702152709log.txt"), new Path("hdfs://master:9000/qj2"));
    }
}

运行结果:

Path dst = new Path("qj1");

154329_gPQI_870039.jpg

Path dst = new Path("hdfs://master:9000/qj2");

154505_SoW0_870039.jpg

可见,dst输出文件格式写法不一样,其结果存放位置也不一样。

总结:

猜想:

Path dst = new Path("qj1");

HDFS中,用户默认是在“/user/hadoop/”目录下。

可以通过运行“hadoop fs -ls”验证猜想。

运行“hadoop fs -ls”程序结果为:

161154_eLDb_870039.jpg

命令列出的是"/user/hadoop/"目录下的内容。所以,我们现在做的一系列操作都是在"/user/hadoop"目录下。

所以,

Path dst = new Path("qj1");

如果未指定绝对路径,而是如上这种相对路径形式,上传的文件自然就到了"/user/hadoop"目录底下。

如使用

Path dst = new Path("/qj1");

形式,则其结果和

Path dst = new Path("hdfs://master:9000/qj1");

的结果是一致的。都是在HDFS分布式文件系统的根目录下。

感悟:

事实上,HDFS分布式文件系统很多特性和本地文件的很多特性是一样的。

转载于:https://my.oschina.net/HIJAY/blog/300390

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值