大数据(六) HDFS的API

package com.bw.HDFS;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

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;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TestHdfs {
	Configuration conf = null;
	FileSystem file = null;
	@Before
	public void conn() throws Exception {
		conf = new Configuration(true);//设置是否读取配置信息
		file=FileSystem.get(conf);
	}

@After
public void close() throws Exception {
	file.close();
}
//创建文件
@Test
public void mkdir() throws Exception {
	Path f = new Path("/aaa/output1");
	if (file.exists(f)) {
		file.delete(f);
	}
	file.mkdirs(f);
}
//判断文件是否存在
@Test
public void exists() throws Exception {
	Path f = new Path("/aaa/jia.txt");
	
	String s = file.exists(f)?"Yes":"No";
	
	System.out.println(s);
}
//重命名
@Test
public void rm() throws Exception {
	Path p1 = new Path("/user/root/passwd");
	Path p2 = new Path("/user/root/jia.txt");
	file.rename(p1, p2);
}
//上传1
@Test
public void putloadfile() throws Exception {
	//上传文件输出位置
	Path path = new Path("/user/root/jia.txt");
	//文件内容的输出
	FSDataOutputStream output = file.create(path);
	//输入位置,相当于文件内容的输入
	InputStream input = new BufferedInputStream(new FileInputStream(new File("d:\\ass.txt")));
	
	IOUtils.copyBytes(input, output, conf,true	);//把磁盘上传到云盘中
}
//下载1
@Test
public void donload() throws Exception {
	Path path = new Path("/user/root/jia.txt");
	
	FSDataInputStream input = file.open(path);
	
	FileOutputStream output = new FileOutputStream("D://abc.txt");
	
	IOUtils.copyBytes(input, output, conf, true);
}
//下载2
@Test
public void test() throws Exception {
	Path path = new Path("/user/root/jia.txt");
	FSDataInputStream input = file.open(path);
	FileOutputStream output = new FileOutputStream("D://abc.txt");
	IOUtils.copyBytes(input, output, conf, true);
}
//上传2
@Test
public void test1s() throws Exception {
	Path path = new Path("/user/root/jia.txt");
	FSDataOutputStream output = file.create(path);
	
	BufferedInputStream input = new BufferedInputStream(new FileInputStream(new File("D://ass.txt")));
	
	IOUtils.copyBytes(input, output, conf, true);
}
//上传3
@Test
public void Upload() throws Exception{
    // 本地文件
    Path src = new Path("D://baidu//BaiduYunGuanjia//at_monhavior");
    // 上传路径
    Path dst = new Path("/user/root/");
    // 上传文件
    file.copyFromLocalFile(src, dst);
    System.out.println("Upload to " + conf.get("fs.default.name"));
    
}
//下载3
@Test
public void test2() throws Exception, Exception {
	file.copyToLocalFile(new Path("/user/root/profile"), new Path("D://baidu//BaiduYunGuanjia"));
}

//上传4
	@Test
	public void test3() throws Exception, Exception {
		file.copyFromLocalFile(new Path("D://baidu//BaiduYunGuanjia"), new Path("/user/root/profile"));
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值