使用JavaAPI上传下载数据

 

使用JavaAPI上传下载数据

1.新建一个java文件,将示例代码写入其中。
2.运行Test。


3.在hdfs中查看是否上传成功:hdfs dfs -ls /folder1

4.下载操作类似

 

文件上传/下载的两种方法:

法1:利用java.io流操作

法2:利用hadoop.io.IOUtils流操作

复制代码
import org.junit.Test;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.Test;

public class TestUpload {
    @Test
    public void testUpload1() throws Exception{
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://bigdata11:9000");
        FileSystem client = FileSystem.get(conf);
        OutputStream out = client.create(new Path("/folder1/a.txt"));
        InputStream in = new FileInputStream("F:\\bigdata11\\JasonPeng.txt");
        byte[] buffer = new byte[1024];
        int len = 0;
        while((len = in.read(buffer))>0) {
            out.write(buffer,0,len);
        }
        
        out.flush();
        out.close();
        in.close();
    }
    @Test
    public void testUpload2() throws Exception{
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://bigdata11:9000");
        FileSystem client = FileSystem.get(conf);
        OutputStream out = client.create(new Path("/folder1/b.txt"));
        InputStream in = new FileInputStream("F:\\bigdata11\\JasonPeng.txt");
        IOUtils.copyBytes(in,out,1024);
    }

}
复制代码

 

 

 

 


 

复制代码
package demo;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.Test;
public class TestDownload {
    @Test
    public void testDownload() throws Exception{
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://bigdata11:9000");
        FileSystem client = FileSystem.get(conf);
        InputStream in = client.open(new Path("/folder1/a.txt"));
        OutputStream out = new FileOutputStream("F:\\bigdata11\\JasonPeng1.txt");
        byte[] buffer = new byte[1024];
        //长度
        int len = 0;
        while((len=in.read(buffer)) > 0){
            //表示读入了数据,再输出
            out.write(buffer,0,len);
        }
        
        out.flush();
        
        out.close();
        in.close();        
    }
    @Test
    public void testDownload1() throws Exception{
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://bigdata11:9000");
        FileSystem client = FileSystem.get(conf);
        InputStream in = client.open(new Path("/folder1/a.txt"));
        OutputStream out = new FileOutputStream("F:\\bigdata11\\JasonPeng2.txt");
        IOUtils.copyBytes(in,out,1024);
    }


}
复制代码

 

posted @   JasonPeng1  阅读(424)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示