hdfs在生产应用中主要是客户端的开发,其核心步骤是从hdfs提供的api中构造一个HDFS的访问客户端对象,然后通过该客户端对象操作HDFS上的文件(如增删改查)。案例如下:
package lyl.hadoop;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class HadoopFileSystem {
public static void main(String[] args) throws IOException {
read();
}
/**
* 从HDFS上读取文件输出到控制台
* @throws IOException
*/
public static void read() throws IOException {
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(configuration);
Path path = new Path("/user/flume/hiveLogs/FlumeData.1517279361739");
FSDataInputStream inputStream = fileSystem.open(path);
IOUtils.copyBytes(inputStream, System.out, 4094);
IOUtils.closeStream(inputStream);
}
/**
* 将本地文件写入到HDFS
* @throws IOException
*/
public static void write() throws IOException {
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(configuration);
Path path = new Path("/user/write.txt");
FSDataOutputStream fsDataOutputStream = fileSystem.create(path);
FileInputStream fileInputStream = new FileInputStream(new File("D:\\README.txt"));
IOUtils.copyBytes(fileInputStream, fsDataOutputStream, 4094);
IOUtils.closeStream(fileInputStream);
}
/**
* 删除HDFS文件
* @throws IOException
*/
public static void delete() throws IOException {
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(configuration);
Path path = new Path("/user/README.txt");
fileSystem.delete(path, true);
}
/**
* 创建目录
* @throws IOException
*/
public static void mkdir() throws IOException {
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(configuration);
Path path = new Path("/lyl");
fileSystem.mkdirs(path);
}
/**
* 获取文件或者目录具体信息 如blocksize replication owner permission
* @throws IOException
*/
public static void getFileStatus() throws IOException {
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(configuration);
Path path = new Path("/user");
FileStatus fileStatus = fileSystem.getFileStatus(path);
System.out.println(fileStatus);
}
/**
* 获取该目录下所有文件的具体信息
* @throws IOException
*/
public static void ListFileSatuts() throws IOException {
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(configuration);
Path path = new Path("/user");
FileStatus[] fileStatus = fileSystem.listStatus(path);
for (FileStatus fileStatus1 : fileStatus) {
System.out.println(fileStatus1);
}
}
}
BDStar原创文章。发布者:Liuyanling,转载请注明出处:http://bigdata-star.com/archives/1426