HDFS API 学习:几个常用的API
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();
}
}
HDFS API 学习:几个常用的API的更多相关文章
- ASP.NET MVC Web API 学习笔记---第一个Web API程序
http://www.cnblogs.com/qingyuan/archive/2012/10/12/2720824.html GetListAll /api/Contact GetListBySex ...
- 【转载】ASP.NET MVC Web API 学习笔记---第一个Web API程序
1. Web API简单说明 近来很多大型的平台都公开了Web API.比如百度地图 Web API,做过地图相关的人都熟悉.公开服务这种方式可以使它易于与各种各样的设备和客户端平台集成功能,以及通过 ...
- ASP.NET MVC Web API 学习笔记---第一个Web API程序【转】
http://www.cnblogs.com/qingyuan/archive/2012/10/12/2720824.html 1. Web API简单说明 近来很多大型的平台都公开了Web API. ...
- ASP.NET MVC Web API 学习笔记---第一个Web API程序---近来很多大型的平台都公开了Web API
1. Web API简单说明 近来很多大型的平台都公开了Web API.比如百度地图 Web API,做过地图相关的人都熟悉.公开服务这种方式可以使它易于与各种各样的设备和客户端平台集成功能,以及通过 ...
- Java基础学习笔记(五) - 常用的API
API介绍 概念:API 即应用编程程序接口.Java API是JDK中提供给我们使用的类说明文档,这些类将底层的代码实现封装.无需关心这些类是如何实现,只需要学习如何使用. 使用:通过API找到需要 ...
- JDBC主要API学习总结
JDBC主要API学习 一.JDBC主要API简介 JDBC API 是一系列的接口,它使得应用程序能够进行数据库联接,执行SQL语句,并且得到返回结果. 二.Driver 接口 Java.sql.D ...
- Servlet 常用API学习(三)
Servlet常用API学习 (三) 一.HTTPServletRequest简介 Servlet API 中定义的 ServletRequest 接口类用于封装请求消息. HttpServletRe ...
- Servlet 常用API学习(二)
Servlet常用API学习 一.HTTP简介 WEB浏览器与WEB服务器之间的一问一答的交互过程必须遵循一定的规则,这个规则就是HTTP协议. HTTP是 hypertext transfer pr ...
- Servlet 常用API学习(一)
Servlet常用API学习 一.Servlet体系结构(图片来自百度图片) 二.ServletConfig接口 Servlet在有些情况下可能需要访问Servlet容器或借助Servlet容器访问外 ...
随机推荐
- 解决IIS的Server Application Error
问题描述一下: Server Application ErrorThe server has encountered an error while loading an application dur ...
- Ubuntu下Eclipse中运行Hadoop程序的参数问题
需要统一的参数: 当配置好eclipse中hadoop的程序后,几个参数需要统一一下: hadoop安装目录下/etc/core_site.xml中 fs.default.name的端口号一定要与ha ...
- gcc用法小记
By francis_hao Feb 13,2017 概要 这里只列出了最常用的选项 选项解释 -c|-S|-E 启动gcc编译器时,它会顺序执行预处理.编译.汇编和连接(四个阶段的详细介绍 ...
- shell脚本应用
解析乱的日志文件到临时文件中,然后用awk 1004 cd /usr/local 1005 ll 1006 cd pttmsg/ 1007 ll 1008 cd msgbin-2/ ...
- 线段树模板 CDOJ1057
UESTCOJ不知道为什么进不去了哇 跟着叉姐的算法讲堂写的板子 叉姐的思路真的好清晰啊,一定是练习的多并且理解的够深了 希望自己也可以每天进步一点点吧 代码: #include <map> ...
- JS中this的指向问题(读书笔记纯手打~)
一.this JavaScrip的this总是指向一个对象,而具体指向哪个对象是在运行时基于函数的执行环境动态绑定的,而非函数被声明时的环境. 二.this的指向 根据运用情景可分为四类: 1.作为对 ...
- vue.单选和多选,纯css自定义单选框样式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- DOM操作的一个小坑
最近在苦读<JavaScript高级程序教程>,真不愧是前端圣经,学到了很多东西. nodeList.NameNodeMap.HTMLCollection这三个集合是动态的!每当文档发生变 ...
- java 身份证15位转18位
/** * 根据身份证号获取性别 * * @param pid * 身份证号 * @return 性别 F为女M为男 */ public static String getSexByPid(Strin ...
- org.apache.http.conn.HttpHostConnectException: Connection to xxx refused.
if you are using emulator to run your app for local server. mention the local ip as 10.0.2.2 and hav ...