HDFS API 学习:几个常用的API
2024-09-28 01:54:53
1.Hadoop-1.2.1 API 文档:http://hadoop.apache.org/docs/r1.2.1/api/
2.几个API:
create(Path f)
:Opens an FSDataOutputStream at the indicated Path.
copyFromLocalFile(Path src, Path dst)
:The src file is on the local disk.
create(Path f)
:Opens an FSDataOutputStream at the indicated Path.
boolean
exists(Path f)
:Check if exists.
get(URI uri, Configuration conf):Returns the FileSystem for this URI's scheme and authority.
listStatus(Path f): List the statuses of the files/directories in the given path if the path is a directory.
mkdirs(Path f)
: Call mkdirs(Path, FsPermission)
with default permission.
rename(Path src, Path dst)
:Renames Path src to Path dst.
3.代码实现:
import java.io.IOException;
import java.net.URI; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path; public class testHDFS { static String hdfs = "hdfs://localhost:9000";
static Configuration conf = new Configuration(); public static void createFolder() throws IOException { FileSystem fs = FileSystem.get(URI.create(hdfs), conf);
Path path = new Path("/test");
fs.mkdirs(path);
fs.close();
} public static void createFile() throws IOException { FileSystem fs = FileSystem.get(URI.create(hdfs), conf);
Path path = new Path("/test/test3.txt");
FSDataOutputStream out = fs.create(path);
out.write("hello hadoop.".getBytes());
} public static void renameFile() throws IOException { FileSystem fs = FileSystem.get(URI.create(hdfs), conf);
Path path = new Path("/test/test1.txt");
Path newPath = new Path("/test/test2.txt");
System.out.println(fs.rename(path, newPath));
} public static void uploadFileToHDFS() throws IOException { FileSystem fs = FileSystem.get(URI.create(hdfs), conf);
Path src = new Path("/home/ares/test.txt");
Path dst = new Path("/test");
fs.copyFromLocalFile(src, dst);
} public static void listFile() throws IOException { FileSystem fs = FileSystem.get(URI.create(hdfs), conf);
Path path = new Path("/test");
FileStatus[] files = fs.listStatus(path);
for (FileStatus file : files) {
System.out.println(file.getPath().toString());
}
} public static void deleteFileOnHDFS() throws IOException {
FileSystem fs = FileSystem.get(URI.create(hdfs), conf);
Path path = new Path("/test/test2.txt");
boolean isExists = fs.exists(path);
if (isExists) {
fs.delete(path, isExists);
System.out.println("file is deleted.");
} else {
System.out.println("file is exist.");
}
} public static void main(String[] args) throws IOException {
// createFolder();
// createFile();
// renameFile();
// uploadFileToHDFS() ;
// listFile();
deleteFileOnHDFS();
}
}
最新文章
- 6个重要的.NET概念:栈,堆,值类型,引用类型,装箱,拆箱
- Java 网络编程之 Socket
- python 学习2
- Acadia Lab 228 + Lab 222
- android 模拟器上网问题
- The Singleton pattern
- 【Unity3D基础教程】给初学者看的Unity教程(六):理解Unity的新GUI系统(UGUI)
- Flexigrid在IE下不显示数据的处理
- android源码-安卓源码-Android源码下载-安卓游戏源码
- An erroroccurred while filtering resources
- 用c#在Access数据库中创建新表
- hdu 1301 Jungle Roads
- JAVA与JSON的序列化、反序列化
- Linux上mysql的安装与配置
- MySQL 加锁处理分析-转载
- Groovy 设计模式 -- 责任链模式
- day15_雷神_前端03
- leetcode1016
- 《廖雪峰 . Git 教程》学习总结
- 「PKUSC2018」最大前缀和