版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_23013309/article/details/88791702
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import org.junit.Test;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 2018-7-15
* 对hdfs文件系统的增删改查
* 底层是数据的操作
*/
public class MyHDFSBaseHandle {
@Test
// 读数据
public void test() throws IOException {
Configuration conf = new Configuration();
//代码入口点,初始化hdfs文件系统
FileSystem fs = FileSystem.get(conf);
//通过fs读数据
Path p = new Path("/test.txt");
FSDataInputStream fis = fs.open(p);
int len = 0;
byte[] buff = new byte[1024];
while((len = fis.read(buff)) != -1) {
System.out.println(new String(buff,0,len));
}
}
@Test
//出现 winutil问题
// 写入文件
public void test2() throws Exception{
System.setProperty("HADOOP_USER_NAME", "centos");
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path p = new Path("/t3.txt");
FSDataOutputStream out = fs.create(p);
out.write("the people's republic of china".getBytes());
out.flush();
out.close();
}
@Test
// 文件的追加
public void test3() throws Exception{
System.setProperty("HADOOP_USER_NAME", "centos");
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path p = new Path("/t3.txt");
FSDataOutputStream append = fs.append(p);
append.write("i love this computer! it's has big power.".getBytes());
append.flush();
append.close();
}
@Test
// 删除文件
public void test4() throws Exception {
System.setProperty("HADOOP_USER_NAME", "centos");
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path p = new Path("/t3.txt");
fs.delete(p, true);
fs.close();
}
@Test
// 把hdfs内的文件复制到本地
public void test5() throws Exception {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path p = new Path("/aa/t2.txt");
FSDataInputStream in = fs.open(p);
FileOutputStream out = new FileOutputStream("tt.txt");
IOUtils.copyBytes(in, out, 1024);
in.close();
out.close();
fs.close();
}
@Test
/**
* 列出文件信息
*/
public void test6() throws Exception {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
FileStatus[] status = fs.listStatus(new Path("/"));
for(FileStatus satus1 : status) {
System.out.println(satus1.getPath());
}
fs.close();
}
}