hdfs写入文件方法
时间: 2024-02-19 16:53:54 浏览: 71
HDFS写入文件的方法有多种,以下是其中两种常用的方法:
1. 使用hadoop fs -put命令将本地文件上传到HDFS中:
```
hadoop fs -put local_file_path hdfs_file_path
```
其中,local_file_path为本地文件路径,hdfs_file_path为HDFS文件路径。
2. 使用Java API将数据写入HDFS中:
```java
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import java.io.InputStream;
import java.net.URI;
public class HdfsWriter {
public static void main(String[] args) throws Exception {
String uri = "hdfs://localhost:9000/user/hadoop/test.txt";
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(uri), conf);
InputStream in = System.in;
Path path = new Path(uri);
FSDataOutputStream out = fs.create(path);
IOUtils.copyBytes(in, out, 4096, true);
}
}
```
其中,需要引入以下依赖:
```xml
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.7</version>
</dependency>
```
阅读全文
相关推荐

















