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"));
}
}