HDFS的api操作

1.2 使用文件系统方式访问数据

1.2.1 获取 FileSystem 的几种方式

* 第一种方式
@Test
public void getFileSystem1() throws IOException {
    Configuration configuration = new Configuration();
    //指定我们使用的文件系统类型:
    configuration.set("fs.defaultFS", "hdfs://node01:8020/");

    //获取指定的文件系统
    FileSystem fileSystem = FileSystem.get(configuration);
    System.out.println(fileSystem.toString());

}
* 第二种方式
```java
@Test
public void getFileSystem2() throws  Exception{
    FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), new       Configuration());
    System.out.println("fileSystem:"+fileSystem);
}
```

* 第三种方式
```java
@Test
public void getFileSystem3() throws  Exception{
    Configuration configuration = new Configuration();
    configuration.set("fs.defaultFS", "hdfs://node01:8020");
    FileSystem fileSystem = FileSystem.newInstance(configuration);
    System.out.println(fileSystem.toString());
}
```

* 第四种方式
```java
//@Test
public void getFileSystem4() throws  Exception{
    FileSystem fileSystem = FileSystem.newInstance(new URI("hdfs://node01:8020") ,new Configuration());
    System.out.println(fileSystem.toString());
}
```

1.2.2 遍历 HDFS 中所有文件

@Test
	public void listMyFiles() throws Exception {
	    //获取fileSystem类
	    FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), new Configuration());
	    //获取RemoteIterator 得到所有的文件或者文件夹,第一个参数指定遍历的路径,第二个参数表示是否要递归遍历
	    RemoteIterator<LocatedFileStatus> locatedFileStatusRemoteIterator = fileSystem.listFiles(new Path("/"), true);
	    while (locatedFileStatusRemoteIterator.hasNext()) {
	        LocatedFileStatus next = locatedFileStatusRemoteIterator.next();
	        System.out.println(next.getPath().toString());
	    }
	    fileSystem.close();
	}
		
### 1.2.3 HDFS 上创建文件夹
@Test
	public void mkdirs() throws  Exception{
	    FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), new Configuration());
	    boolean mkdirs = fileSystem.mkdirs(new Path("/hello/mydir/test"));
	    fileSystem.close();
	}

1.2.4 下载文件

@Test
	public void getFileToLocal()throws  Exception{
	    FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), new Configuration());
	    FSDataInputStream inputStream = fileSystem.open(new Path("/timer.txt"));
	    FileOutputStream  outputStream = new FileOutputStream(new File("e:\\timer.txt"));
	    IOUtils.copy(inputStream,outputStream );
	    IOUtils.closeQuietly(inputStream);
	    IOUtils.closeQuietly(outputStream);
	    fileSystem.close();
	}
### 1.2.5 HDFS 文件上传
@Test
	public void 
	Data() throws  Exception{
	    FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), new Configuration());
	    fileSystem.copyFromLocalFile(new Path("file:///c:\\install.log"),new Path("/hello/mydir/test"));
	    fileSystem.close();
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值