首页 课程 师资 教程 报名

在Java程序中进行hdfs操作

  • 2022-11-14 10:35:14
  • 1127次 动力节点

将文件从本地复制到 HDFS

命令是

hadoop fs -copyFromLocal
package com.amal.hadoop;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
/**
 * @author amalgjose
 *
 */
public class CopyFromLocal { 
    public static void main(String[] args) throws IOException {         
        Configuration conf =new Configuration();
        conf.addResource(new Path("conf/core-site.xml"));
        conf.addResource(new Path("conf/mapred-site.xml"));
        conf.addResource(new Path("conf/hdfs=site.xml"));
        FileSystem fs = FileSystem.get(conf);
        Path sourcePath = new Path("source");
        Path destPath = new Path("/user/training");
        if(!(fs.exists(destPath)))
        {
            System.out.println("No Such destination exists :"+destPath);
            return;
        }
                 fs.copyFromLocalFile(sourcePath, destPath);         
    }
}

将文件从 HDFS 复制到本地

命令是

hadoop fs -copyToLocal
package com.amal.hadoop;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
/**
 * @author amalgjose
 *
 */
public class CopyToLocal {
public static void main(String[] args) throws IOException {         
        Configuration conf =new Configuration();
        conf.addResource(new Path("conf/core-site.xml"));
        conf.addResource(new Path("conf/mapred-site.xml"));
        conf.addResource(new Path("conf/hdfs=site.xml"));
        FileSystem fs = FileSystem.get(conf);
        Path sourcePath = new Path("/user/training");
        Path destPath = new Path("destination");
        if(!(fs.exists(sourcePath)))
        {
            System.out.println("No Such Source exists :"+sourcePath);
            return;
        }         
        fs.copyToLocalFile(sourcePath, destPath);         
    }
}

 

选你想看

你适合学Java吗?4大专业测评方法

代码逻辑 吸收能力 技术学习能力 综合素质

先测评确定适合在学习

在线申请免费测试名额
价值1998元实验班免费学
姓名
手机
提交