在Java程序中进行hdfs操作_极悦注册
专注Java教育14年 全国咨询/投诉热线:444-1124-454
极悦LOGO图
始于2009,口口相传的Java黄埔军校
首页 学习攻略 Java学习 在Java程序中进行hdfs操作

在Java程序中进行hdfs操作

更新时间:2022-11-14 10:35:14 来源:极悦 浏览771次

将文件从本地复制到 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);         
    }
}

 

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>