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(); } }
时间: 2024-10-20 01:34:06