这篇文章主要为大家展示了“HDFS中客户端操作有哪些”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“HDFS中客户端操作有哪些”这篇文章吧。
1. 将支持window系统的hadoop安装包,解压到本地,并配置环境变量( 无中文或特殊符号的路径)
--比如:我的Hadoop安装包位置是:D:\hadoop\hadoop-3.1.0
配置环境变量:
HADOOP_HOME D:\hadoop\hadoop-3.1.0
path %HADOOP_HOME%\bin
2. 如果上述操作后还有问题可以将bin目录下hadoop.dll和winutils.exe放到C:/windows/system32目录下,然后重启电脑。
操作HDFS:
1.创建文件系统对象 2.具体操作 :上传,删除,下载..... 3.关资源
private FileSystem fs;
//1.创建fs文件系统对象
@Before
public void before() throws Exception {
/*
get(final URI uri, final Configuration conf,final String user)
uri : HDFS的地址
conf : 需要使用的配置信息
user : 用来操作HDFS的用户
*/
// uri : HDFS的地址
URI uri = new URI("hdfs://hadoop102:9820");
//conf : 需要使用的配置信息
Configuration conf = new Configuration();
//user : 用来操作HDFS的用户
String user = "luck";
fs = FileSystem.get(uri, conf, user);
}
//3.关资源
@After
public void after(){
try {
if (fs != null) {
fs.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
//2.具体的操作
//上传
@Test
public void test() throws IOException {
/*
copyFromLocalFile(boolean delSrc, boolean overwrite, Path src, Path dst)
delSrc : 是否删除源文件(是剪切还是复制)
overwrite : 如果目标文件已存在是否覆盖目标文件
--注意:如果不覆盖但目标文件又存在则会报错
src : 源文件路径(本地)
dst : 目标文件路径(HDFS)
*/
fs.copyFromLocalFile(true, true,new Path("D:\\io\\hdfs\\aa.txt"),new Path("/hdfs"));
}
//下载
@Test
public void test2() throws IOException {
/*
copyToLocalFile(boolean delSrc, Path src, Path dst,
boolean useRawLocalFileSystem)
delSrc : 是否删除源文件(HDFS上的文件)
src : 源文件路径(HDFS)
dst : 目标文件路径(本地)
useRawLocalFileSystem : 是否使用useRawLocalFileSystem
如果使用:不会下载crc校验文件
如果不使用 : 会下载crc校验文件
*/
fs.copyToLocalFile(false,new Path("/hdfs/aa.txt"),new Path("D:\\io\\hdfs"),
false);
}
//删除
@Test
public void test3() throws IOException {
/*
delete(Path f, boolean recursive)
f : 删除的数据的路径
recursive : 是否递归(如果是目录(非空)必须是true否则会报错。如果是文件true和false都可以)
*/
fs.delete(new Path("/longge/sanguo.txt"),true);
}
//改名
@Test
public void test4() throws IOException {
/*
rename(Path src, Path dst)
src : 源文件路径
dst : 目标文件路径
*/
//fs.rename(new Path("/longge/xiyou.txt"),new Path("/longge/xiyouji.txt"));
//移动
fs.rename(new Path("/longge/xiyouji.txt"),new Path("/hdfs/xiyouji.txt"));
}
//文件详情查看
@Test
public void test5() throws IOException {
/*
listFiles(
final Path f, final boolean recursive)
f : 目标路径
recursive : 是否递归
*/
RemoteIterator<LocatedFileStatus> remoteIterator = fs.listFiles(new Path("/"), true);
while(remoteIterator.hasNext()){
//文件详情对象
LocatedFileStatus fileStatus = remoteIterator.next();
//文件名
System.out.println("=============" + fileStatus.getPath().getName() + "===================");
System.out.println("=====所属主:" + fileStatus.getOwner());
System.out.println("=====副本数量:" + fileStatus.getReplication());
//获取块的信息
BlockLocation[] blockLocations = fileStatus.getBlockLocations();
for (BlockLocation block : blockLocations) {
System.out.println(block);
}
}
//用流的方式实现HDFS上传和下载内容
//上传
@Test
public void test7() throws IOException {
//读-从本地读(文件输入流)
FileInputStream fis = new FileInputStream("D:\\io\\hdfs\\aa.txt");
//写-向HDFS写
FSDataOutputStream fos = fs.create(new Path("/hdfs/aa.txt"));
//一边读一边写
/*
文件对拷
copyBytes(InputStream in, OutputStream out,int buffSize, boolean close)
in : 输入流
out : 输出流
buffsize :缓存大小
close : 是否关流
*/
IOUtils.copyBytes(fis,fos,1024,false);
//关流
IOUtils.closeStream(fis);
IOUtils.closeStream(fos);
/**
*
*/
}
//下载
@Test
public void test8() throws IOException {
//读 - 从HDFS上读
FSDataInputStream fis = fs.open(new Path("/hdfs/xiyouji.txt"));
//写 - 向本地写(文件输出流)
FileOutputStream fos = new FileOutputStream("D:\\io\\hdfs\\xiyouji.txt");
//文件对拷
IOUtils.copyBytes(fis,fos,1024,true);
}
以上是“HDFS中客户端操作有哪些”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/luffycl/blog/4990056