一 、HDFS命令行客户端的常用操作命令
(1)start-dfs.sh :自动启动整个集群
stop-dfs.sh :自动停止整个集群
(2)上传文件到hdfs中: hadoop fs -put /本地文件 /HDFS路径
(3)下载文件到客户端本地磁盘: hadoop fs -get /hdfs中的路径 /本地磁盘目录
(4)在hdfs中创建文件夹: hadoop fs -mkdir -p /aaa/xxx
(5)移动hdfs中的文件(更名): hadoop fs -mv /hdfs的路径1 /hdfs的另一个路径2
(6)删除hdfs中的文件或文件夹: hadoop fs -rm -r /aaa
(7)查看hdfs中的文本文件内容: hadoop fs -cat /demo.txt hadoop fs -tail -f /demo.txt
二、开发代码
(1)将hdfs客户端开发所需的jar导入工程(jar包可在hadoop安装包中找到common/hdfs)
(2)要对hdfs中的文件进行操作,代码中首先需要获得一个hdfs的客户端对象
Configuration conf = new Configuration();
//指定本客户端上传到hdfs时需要保存的副本数为2
conf.set("dfs.replication", "2");
//指定本客户端上传文件到hdfs时切块的规格大小:128m
conf.set("dfs.blocksize", "128m");
FileSystem fs = FileSystem.get(new URI("hdfs://hdp-01:9000"),conf,"root");
(3)利用fs对象的方法进行文件操作
上传文件—— fs.copyFromLocalFile(new Path("本地路径"),new Path("hdfs的路径"));
下载文件——fs.copyToLocalFile(new Path("hdfs的路径"),new Path("本地路径"));