FileSystem是Hadoop提供的一个通用的文件系统API, Hadoop文件系统中通过Hadoop Path 对象(而非java.io.File 对象,因为它的语义与本地文件系统联系太紧密)来代表文件。可以将路径视为一个Hadoop 文件系统URI,如hdfs://localhost/user/ tom/ quangle. txt。下面就用最简单的方法来举例java 读取,删除,写入文件到hdfs。
先贴一下maven依赖
<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>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.7</version>
</dependency>
1.java hdfs 创建文件夹
FileSystem fs = null;
try {
Configuration config = new Configuration();
fs = FileSystem.get(URI.create("hdfs://hadoopMaster:9000"), config);
Path path = new Path("/tmp");
if (!fs.exists(path)) {
fs.mkdirs(path);
}
fs.close();
} catch (Throwable e) {
logger.error("出现异常", e);
}
2.java hdfs写入文件
FileSystem fs = null;
FSDataOutputStream fsDataOutputStream = null;
try {
fs = FileSystem.get(URI.create("hdfs://hadoopMaster:9000"), config);
Path path = new Path("/tmp");
if (!fs.exists(path)) {
fs.mkdirs(path);
}
fsDataOutputStream = fs.create(new Path("/tmp/itxw.txt"));
byte[] buff = "哈哈IT学问网".getBytes("UTF-8");
fsDataOutputStream.write(buff);
fsDataOutputStream.flush();
fsDataOutputStream.close();
fs.close();
} catch (Throwable e) {
logger.error("出现异常", e);
}
3.java hdfs删除文件
FileSystem fs = null;
FSDataOutputStream fsDataOutputStream = null;
try {
fs = FileSystem.get(URI.create("hdfs://hadoopMaster:9000"), config);
fs.delete(new Path("/tmp/itxw.txw"),false);//第二个参数表示是否递归删除
fs.close();
} catch (Throwable e) {
logger.error("出现异常", e);
}
4.java hdfs 读取文件
FileSystem fs = null;
FSDataInputStream fsDataInputStream = null;
try {
fs = FileSystem.get(URI.create("hdfs://hadoopMaster:9000"), config);
fsDataInputStream =fs.open(new Path("/tmp/itxw.txw"));
String content=IOUtils.toString(fsDataInputStream, "utf-8");
System.out.println(content);
fsDataInputStream.flush();
fsDataInputStream.close();
fs.close();
} catch (Throwable e) {
logger.error("出现异常", e);
}