hdfs的javaAPI操作
时间: 2023-09-04 13:16:07 浏览: 70
HDFS的Java API操作可以通过创建Hadoop Configuration对象来实现。可以使用以下代码来创建Configuration对象:
```java
Configuration config = new Configuration();
config.set("fs.defaultFS", "hdfs://<namenode-hostname>:<port>");
```
其中,fs.defaultFS是HDFS的URI,需要替换为实际的URI。然后,使用FileSystem对象来操作HDFS文件系统。以下是一些常用的HDFS Java API操作:
1. 创建目录:
```java
FileSystem hdfs = FileSystem.get(config);
Path directory = new Path("/path/to/directory");
boolean success = hdfs.mkdirs(directory);
```
2. 创建文件:
```java
FSDataOutputStream out = hdfs.create(new Path("/path/to/file"));
```
3. 写入数据到文件:
```java
byte[] data = "Hello World".getBytes();
out.write(data);
```
4. 读取文件内容:
```java
FSDataInputStream in = hdfs.open(new Path("/path/to/file"));
byte[] buffer = new byte[1024];
int bytesRead = in.read(buffer);
String content = new String(buffer, 0, bytesRead);
```
5. 删除文件或目录:
```java
boolean success = hdfs.delete(new Path("/path/to/file"), false);
```
以上是一些常用的HDFS Java API操作,可以根据实际需要进行扩展。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)