Hadoop的hdfs api操做

public static void listFile(String path) throws IOException{
		//讀取配置文件
		Configuration conf = new Configuration();
		//獲取文件系統
		FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop1:9000"),conf);
		//獲取文件或目錄狀態
		FileStatus[] fileStatus = fs.listStatus(new Path(path));
		//打印文件的路徑
		for (FileStatus file : fileStatus) {
			System.out.println(file.getPath());
		}
	 
		//關閉文件系統
		fs.close();
	 }

1、包依賴java

<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-client</artifactId>
    <version>2.7.6</version>
</dependency>

2、API的操做apache

1.建立目錄api

public static void mkdir(String path) throws IOException {
		Configuration conf = new Configuration();
		conf.set("fs.defaultFS", "hdfs://127.0.0.1:9000");
		FileSystem fs = FileSystem.get(conf);
		Path srcPath = new Path(path);
		boolean isok = fs.mkdirs(srcPath);
		if (isok) {
			System.out.println("create dir ok!");
		} else {
			System.out.println("create dir failure");
		}
		fs.close();
	}

2.刪除目錄oop

/**
	 * 刪除目錄
	 * @param path
	 */
	public static void rmdir(String path)throws Exception {
		Configuration configuration = new Configuration();
		FileSystem fs = FileSystem.get(URI.create("hdfs://127.0.0.1:9000"), configuration);
	    boolean flag = fs.deleteOnExit(new Path("/test"));
	    if(flag) {
			 System.out.println("delete ok!");
		}else {
			 System.out.println("delete failure");
		}
		
		//關閉文件系統
		fs.close();

	}

3.建立文件code

public static void createFile(String dst , byte[] contents) throws IOException{
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(URI.create("hdfs://localhost:9000"),conf);
		Path dstPath = new Path(dst);  
		FSDataOutputStream outputStream = fs.create(dstPath);
		outputStream.write(contents);
		outputStream.close();
		fs.close();
		System.out.println("文件建立成功!");
		
	 }

4.讀取文件內容hadoop

public static void readFile(String uri) throws IOException {
		//讀取配置文件
		Configuration conf = new Configuration();
		//獲取文件系統
		FileSystem fs = FileSystem.get(URI.create("hdfs://localhost:9000"),conf);
		
		InputStream in = null;
		try {
			in = fs.open(new Path(uri));
			//複製到標準輸出流
			IOUtils.copyBytes(in, System.out, 4096,false);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			IOUtils.closeStream(in);
		}
	}

5.查看文件目錄get

public static void listFile(String path) throws IOException{
		//讀取配置文件
		Configuration conf = new Configuration();
		//獲取文件系統
		FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop1:9000"),conf);
		//獲取文件或目錄狀態
		FileStatus[] fileStatus = fs.listStatus(new Path(path));
		//打印文件的路徑
		for (FileStatus file : fileStatus) {
			System.out.println(file.getPath());
		}
	 
		//關閉文件系統
		fs.close();
	 }

其它操做查看應的FileSystem的apiit

相關文章
相關標籤/搜索