package cn.Awz.HadoopClientTest;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
public class HCTest {
public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {
// fileUpload();
// fileLoad();
// fileDelete();
rename();
}
//创建文件
public static void createFile() throws IOException, URISyntaxException, InterruptedException {
Configuration conf = new Configuration();
//此处是重点 删除会报错原因不详 参考博客https://blog.csdn.net/www646288178/article/details/115661846?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162987905516780262516325%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=162987905516780262516325&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v29_ecpm-3-115661846.pc_search_similar&utm_term=%3A+No+FileSystem+for+scheme+%22hdfs%22&spm=1018.2226.3001.4187
conf.set("fs.hdfs.impl",org.apache.hadoop.hdfs.DistributedFileSystem.class.getName());
//创建hdfs的客户端
FileSystem fs = FileSystem.get(new URI("你的hdfs地址"),conf,"hadoop");
Path path = new Path("/Awz/test");
//在hdfs上新建路径
fs.mkdirs(new Path("/Awz/test"));
System.out.println("文件创建成功");
//关闭资源
fs.close();
}
//文件上传
public static void fileUpload() throws IOException, URISyntaxException, InterruptedException {
//获取hdfs对象
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("你的hdfs地址"),conf,"hadoop");
// 上传文件api
fs.copyFromLocalFile(new Path("D:\\HadoopDevelopment\\testData\\data1.txt"),new Path("/Awz/test"));
// 关闭资源
fs.close();
}
// 文件下载
public static void fileLoad() throws URISyntaxException, IOException, InterruptedException {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("你的hdfs地址"), conf, "hadoop");
// fs.copyToLocalFile(new Path("/Awz/test/data.txt"),new Path("D:\\HadoopDevelopment\\testData"));
//删除 src文件
fs.copyToLocalFile(false,new Path("/Awz/test/data.txt"),new Path("D:\\HadoopDevelopment\\testData\\data1.txt"),true);
fs.close();
}
// 文件删除
public static void fileDelete() throws IOException, URISyntaxException, InterruptedException {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("你的hdfs地址"), conf, "hadoop");
fs.delete(new Path("/Awz/test/data1.txt"),false);
fs.close();
}
//文件该名
public static void rename() throws IOException, URISyntaxException, InterruptedException {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("你的hdfs地址"), conf, "hadoop");
fs.rename(new Path("/Awz/test/data.txt"),new Path("/Awz/test/d.txt"));
fs.close();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/133856.html