Hadoop API
命令
上传文件
hadoop fs -put 文件 hdfs文件夹
hadoop fs -put test.txt /files #将test.txt文件上传到hdfs文件夹files下
删除文件
hadoop fs -rm -r 文件/文件夹
hadoop fs -rm -rf /quanfeihu/test.txt
查看文件
hadoop fs -ls [-R] 文件夹
hadoop fs -ls -R /quanfeihu #递归显示quanfiehu文件夹下的文件
创建文件夹
hadoop fs -mkdir [-p] 文件夹
hadoop fs -mkdir -p /quanfeihu/test/01 #创建文件夹/quanfeihu/test/01
下载文件
hadoop fs -get 远程文件 本地文件
hadoop fs -get /quanfeihu/test.txt c://
复制文件
hadoop fs -cp 远程文件 远程文件夹
hadoop fs -cp /quanfeihu/test.txt /quanfeihu/test/
移动/重命名
hadoop fs -mv 远程文件 远程文件(夹)
hadoop fs -mv /quanfeihu/test.txt /quanfeihu/test/
tail查看文件
hadoop fs -tail [-f] 远程文件
hadoop fs -tail -f /quanfeihu/test.txt
在文件后追加
hadoop fs -appendToFile 本地文件 远程文件
(将本地文件追加到远程文件下)hadoop fs -appendToFile c://test.txt /quanfiehu/test.txt
修改文件组/用户
hadoop fs -chown owner:group 文件
hadoop fs -chown root:root /quanfeihu/test.txt
java API
maven依赖
<dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>2.8.3</version> </dependency>
初始化
//创建客户端对象
Configuration conf = new Configuration();
conf.set("dfs.blocksize", "128m"); //设置block大小
FileSystem fs = FileSystem.get(new URI("hdfs://linux11:9000/"), conf, "root");
创建文件夹
fs.mkdirs(new Path("/quanfeihu/"));
上传文件
//将test.txt 上传到hdfs远程/quanfeihu文件夹下 fs.copyFromLocalFile(new Path("C:\\test.txt"), new Path("/quanfeihu"));
下载文件
/*将远程文件/quanfeihu/test.txt下载到本地C盘*/ //使用hadoop本地环境 fs.copyToLocalFile(new Path("/quanfeihu/test.txt"), new Path("C:\\")); //不适用hadoop本地环境 fs.copyToLocalFile(false, new Path("/quanfeihu/test.txt"), new Path("C:\\"),true);
移动或者重命名
fs.rename(new Path("/quanfeihu/test.txt"), new Path("/quanfeihu/test1.txt"));
删除文件/文件夹
fs.delete(new Path("/quanfiehu"), true);
查看目录信息
FileSystem fs = FileSystem.get(new URI("hdfs://linux:9000/"), new Configuration(), "root"); RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true); while (listFiles.hasNext()) { LocatedFileStatus nextFile = listFiles.next(); System.out.println("get file path:" + nextFile.getPath().toString()); System.out.println("get file Permission:" + nextFile.getPermission()); System.out.println("get file Owner:" + nextFile.getOwner()); System.out.println("get file Group:" + nextFile.getGroup()); System.out.println("get file AccessTime:" + nextFile.getAccessTime()); System.out.println("get file BlockSize:" + nextFile.getBlockSize()); System.out.println("get file Len:" + nextFile.getLen()); System.out.println("get file Replication:" + nextFile.getReplication()); // System.out.println("get file Replication:" + nextFile.getBlockLocations()); BlockLocation[] blockLocations = nextFile.getBlockLocations(); for (BlockLocation b : blockLocations) { System.out.println("blk 的长度:" + b.getLength()); System.out.println("blk 的偏移量:" + b.getOffset()); for (String host : b.getHosts()) { System.out.println("blk 的主机名:" + host); } } } fs.close();
判断文件是否存在
boolean exist = fs.exists(new Path("/quanfeihu/hdfs_my_start.sh"));
写入文件
FileSystem fs = FileSystem.get(conf); FSDataOutputStream out = fs.create(new Path("/2.txt")); out.write("xuanxuan\n".getBytes()); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(out)); bufferedWriter.write("测试一下"); bufferedWriter.newLine(); bufferedWriter.close(); out.close(); fs.close();
读取文件流
FileSystem fs = FileSystem.get(conf); FSDataInputStream in = fs.open(new Path("/quanfeihu/hadoop-2.8.3.tar.gz")); FileOutputStream out = new FileOutputStream(new File("g://hadoop.tar.gz")); in.seek(0); //从文件什么地方开始读(字节) byte[] b = new byte[1024]; int read = -1; int count = 0; while ((read = in.read(b)) != -1) { /*System.out.println(new String(b, 0, read));*/ out.write(b, 0, read); } in.close(); fs.close(); out.close();