[英]Is there any existing API to read and write to HDFS from JAVA
是否存在可从HDFS读取/写入的任何现有API,以及实现该API的最佳做法。
Below is the code snippet 下面是代码片段
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 org.apache.hadoop.util.Progressable;
String Dest = "/user/pkumar/test.xml";
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(Dest),conf);
Path path = new Path(Dest);
if(!fs.exists(path)){
OutputStream out = fs.create(path, new Progressable(){
public void progress(){
System.out.print(".");
}
});
System.out.println();
IOUtils.copyBytes(connect, out, 4096, true);
}
use the below dependencies in your pom.xml 在pom.xml中使用以下依赖项
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.3.0-cdh5.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.3.0-cdh5.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.2</version>
</dependency>
</dependencies>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.