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();
}
}

最新文章

  1. 6个重要的.NET概念:栈,堆,值类型,引用类型,装箱,拆箱
  2. Java 网络编程之 Socket
  3. python 学习2
  4. Acadia Lab 228 + Lab 222
  5. android 模拟器上网问题
  6. The Singleton pattern
  7. 【Unity3D基础教程】给初学者看的Unity教程(六):理解Unity的新GUI系统(UGUI)
  8. Flexigrid在IE下不显示数据的处理
  9. android源码-安卓源码-Android源码下载-安卓游戏源码
  10. An erroroccurred while filtering resources
  11. 用c#在Access数据库中创建新表
  12. hdu 1301 Jungle Roads
  13. JAVA与JSON的序列化、反序列化
  14. Linux上mysql的安装与配置
  15. MySQL 加锁处理分析-转载
  16. Groovy 设计模式 -- 责任链模式
  17. day15_雷神_前端03
  18. leetcode1016
  19. 《廖雪峰 . Git 教程》学习总结
  20. 「PKUSC2018」最大前缀和

热门文章

  1. 图片和byte[]的互相转换
  2. 【2017.12.22.A】
  3. 版本7以上IE以文件夹视图方式打开FTP的解决
  4. 通用adapter
  5. C ------ 标准函数介绍
  6. Error in deleting blocks.
  7. ansible 部署jdk
  8. C# 序列化理解 1(转)
  9. AngularJS+BootStrap的一些插件
  10. java三