文章目录
1、使用FSDataInputStream获取HDFS的/user/hadoop/目录下的task.txt的文件内容,并输出,其中uri为hdfs://localhost:9000/user/hadoop/task.txt。
package step2;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
public class FileSystemCat {
public static void main(String[] args) throws IOException {
//请在Begin-End之间添加你的代码,完成任务要求。
/********* Begin *********/
URI uri =URI.create("hdfs://localhost:9000/user/hadoop/task.txt");
Configuration config = new Configuration();
FileSystem fs =FileSystem.get(uri,config);
InputStream in = null;
try{
in = fs.open(new Path(uri));
IOUtils.copyBytes(in,System.out, 2048, false);
}catch(Exception e){
IOUtils.closeStream(in);
}
/********* End *********/
}
}
2、HDFS-JAVA接口之上传文件
package step3;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.io.File;
import org.apache.hadoop.conf.Configuration;
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.apache.hadoop.util.Progressable;
public class FileSystemUpload {
public static void main(String[] args) throws IOException {
/********* Begin *********/
File localPath = new File("/develop/input/hello.txt");
String hdfsPath = "hdfs://localhost:9000/user/tmp/hello.txt";
InputStream in = new BufferedInputStream(new FileInputStream(localPath));// 获取输入流对象
Configuration config = new Configuration();
FileSystem fs = FileSystem.get(URI.create(hdfsPath), config);
long fileSize = localPath.length() > 65536 ? localPath.length() / 65536 : 1; // 待上传文件大小
FSDataOutputStream out = fs.create(new Path(hdfsPath), new Progressable() {
// 方法在每次上传了64KB字节大小的文件之后会自动调用一次
long fileCount = 0;
public void progress() {
System.out.println("总进度" + (fileCount / fileSize) * 100 + "%");
fileCount++;
}
});
IOUtils.copyBytes(in, out, 2048, true);// 最后一个参数的意思是使用完之后是否关闭流
/********* End *********/
}
}
3、HDFS-JAVA接口之删除文件
要求:
删除HDFS的/user/hadoop/目录(空目录);
删除HDFS的/tmp/test/目录(非空目录);
列出HDFS根目录下所有的文件和文件夹;
列出HDFS下/tmp/的所有文件和文件夹。
package step4;
import java.io.IOException;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.Path;
public class FileSystemDelete {
public static void main(String[] args) throws IOException {
/********* Begin *********/
String root = "hdfs://localhost:9000/";//根目录
String path = "hdfs://localhost:9000/tmp"; //要列出的目录
//待删除的两个目录
String del1 = "hdfs://localhost:9000/user/hadoop";
String del2 = "hdfs://localhost:9000/tmp/test";
Configuration config = new Configuration();
FileSystem fs = FileSystem.get(URI.create(root),config);
fs.delete(new Path(del1),true);
fs.delete(new Path(del2),true);
Path[] paths = {new Path(root),new Path(path)};
FileStatus[] status = fs.listStatus(paths);
Path[] listPaths = FileUtil.stat2Paths(status);
for(Path path1 : listPaths){
System.out.println(path1);
}
/********* End *********/
}
}