zoukankan      html  css  js  c++  java
  • java上传本地文件至hdfs(简单写一下)

    1.创建一个maven项目,导入jar包

    <dependency>
                <groupId>org.apache.hadoop</groupId>
                <artifactId>hadoop-common</artifactId>
                <version>2.6.5</version>
            </dependency>
            <dependency>
                <groupId>org.apache.hadoop</groupId>
                <artifactId>hadoop-hdfs</artifactId>
                <version>2.6.5</version>
            </dependency>
            <dependency>
                <groupId>org.apache.hadoop</groupId>
                <artifactId>hadoop-client</artifactId>
                <version>2.6.5</version>
            </dependency>
            <dependency>
                <groupId>jdk.tools</groupId>
                <artifactId>jdk.tools</artifactId>
                <version>1.8</version>
                <scope>system</scope>
                <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
            </dependency>

    2.上传文件代码

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FSDataInputStream;
    import org.apache.hadoop.fs.FSDataOutputStream;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.io.IOUtils;
    public class HdfsText01 {
        public static void main(String[] args) throws Exception {
            //1 创建连接
            Configuration conf = new Configuration();
            //2 连接端口
            conf.set("fs.defaultFS", "hdfs://hadoop01:9000");
            //3 获取连接对象
            FileSystem fs = FileSystem.get(conf);
            //本地文件上传到 hdfs
            fs.copyFromLocalFile(new Path("E://haha.txt"), new Path("/output1"));
            fs.close();
            
        }
    }

    3.使用流上传文件

    //流上传文件
            FileInputStream in=new FileInputStream("E://haha.txt");//读取本地文件
            FSDataOutputStream out = fs.create(new Path("/output2"));//在hdfs上创建路径
            byte[] b = new byte[1024*1024];
            int read = 0;
            while((read = in.read(b)) > 0){
                out.write(b, 0, read);
            }

    4.将hdfs文件下载到本地

    //hdfs文件复制到本地(流)
    FSDataInputStream in = fs.open(new Path("/output"));
    FileOutputStream out = new FileOutputStream("E:/mm.txt");
    IOUtils.copyBytes(in, out, conf);

    注:在最后要关闭流哦

  • 相关阅读:
    1月19号 UIImageView
    1月18号 UILabel 加上导入.tff格式的字体
    1月18号 UIButton
    2016年 1月15号 cocoapods的导入
    1月12号 UIView
    12月30号 iOS程序准备
    12月29号 计算器(包含混合运算)
    2016.01.13 代理设计模式
    2016.01.04 视图控制器UIViewController
    2015.12.31 iOS程序准备(developer.apple.com)
  • 原文地址:https://www.cnblogs.com/-yinn/p/11892903.html
Copyright © 2011-2022 走看看