11.11
1|6显示hdfs的文件
//显示文件内容 public static void cat(String file)throws URISyntaxException, IOException, InterruptedException { FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:8020"), new Configuration(), "root"); Path filePath = new Path(file); if(fs.exists(filePath)) { FSDataInputStream in = fs.open(filePath); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String content = null; while((content = br.readLine()) != null) { System.out.println(content); } br.close(); fs.close(); }else { System.out.println("file "+filePath+ "doesn't exist."); } fs.close(); }
1|6移动hdfs的文件
//移动hdfs中的文件
public static void moveFile(String srcPath, String dirPath) throws URISyntaxException, IOException, InterruptedException {
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:8020"), new Configuration(), "root");
if(fs.exists(new Path(dirPath))) {
System.out.println("文件被占用。");
return;
}
if(fs.rename(new Path(srcPath), new Path(dirPath))) {
System.out.println("文件移动成功。");
} else {
System.out.println("文件移动失败。");
}
}
1|7上传与下载
//将本地文件上传到hdfs
public static void copyFromLocalFile(String localPath,String srcPath) throws URISyntaxException, IOException, InterruptedException
{
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:8020"), new Configuration(), "root");
fs.copyFromLocalFile(new Path(localPath),new Path(srcPath));
System.out.println("上传成功!!!");
fs.close();
}
//将hdfs的文件下载到本地
public static void downFromHdfs(String src,String dst)throws URISyntaxException, IOException, InterruptedException
{
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:8020"), new Configuration(), "root");
Path dstPath=new Path(dst);
//第一个参数为:是否删除原文件,源文件,目的文件,是否使用本地文件系统
fs.copyToLocalFile(false,new Path(src),dstPath,true);
fs.close();
System.out.println("文件下载成功已存放到"+dst);
}
1|8追加内容到文件结尾
//追加到结尾
public static void addContentToTail(String filePath, String content, boolean head) throws IOException, InterruptedException, URISyntaxException {
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:8020"), new Configuration(), "root");
Path path= new Path(filePath);
FSDataOutputStream ops = fs.append(path);
ops.write(content.getBytes());
if (!head) {
System.out.println("内容以追加到结尾。");
}
ops.close();
fs.close();
}
1|9追加本地文件内容到hdfs文件结尾
// 追加本地文件内容到目的文件结尾
public static void addFileToTail(String localFilePath, String remoteFilePath) throws IOException, InterruptedException, URISyntaxException{
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:8020"), new Configuration(), "root");
Path remotePath = new Path(remoteFilePath);
// 创建一个本地文件(需要追加的文件)读入流
FileInputStream inps = new FileInputStream(localFilePath);
// 创建一个文件输出留,输出的内容追加到文件末尾
FSDataOutputStream ops = fs.append(remotePath);
byte[] buffer = new byte[1024];
int read = -1;
while((read = inps.read(buffer)) > 0) {
ops.write(buffer, 0, read);
}
ops.close();
inps.close();
fs.close();
}
1|10追加内容到hdfs文件开头
// 追加内容到文件开头
public static void addContentToHead(String filePath, String content) throws IOException, InterruptedException, URISyntaxException {
// 创建一个临时本地文件
String localFilePath = new File("").getCanonicalPath()+"\\web\\text\\xlf.txt" ;
// 将要追加的文件移动到本地
moveToLocalFile(filePath, localFilePath);
// 创建一个新的HDFS文件(空的)
createFile(filePath);
appendContentToTail(content,filePath, true);
addFileToTail(localFilePath, filePath);
System.out.println("内容以追加到开头。");
}