(初级)HDFS的Java访问

2017-07-24  本文已影响0人  呆老板

package hdfs;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.net.URI;

import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.FSDataInputStream;

import org.apache.hadoop.fs.FSDataOutputStream;

import org.apache.hadoop.fs.FileStatus;

import org.apache.hadoop.fs.FileSystem;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.IOUtils;

public class App2 {

static final String PATH="hdfs://192.168.2.27:9000/hello";

static final String DIR = "/d1";

static final String FILE = "/d1/hello";

public static void main(String[] args) throws IOException, URISyntaxException {

//用户代码操作HDFS时,是直接调用FileSystem的子类完成的

FileSystem fileSystem = getFileSystem();

//创建文件夹 hadoop fs -mkdir

//mkdir(fileSystem);

//删除文件夹

//remove(fileSystem);

//上传文件

//putData(fileSystem);

//下载文件

//getData(fileSystem);

//浏览文件夹

list(fileSystem);

}

private static void list(FileSystem fileSystem) throws IOException {

FileStatus[] listStatus = fileSystem.listStatus(new Path("/"));

for(FileStatus fileStatus  :listStatus){

String isDir = fileStatus.isDir()?"文件夹":"文件";

String permission = fileStatus.getPermission().toString();

short replication = fileStatus.getReplication();

long len = fileStatus.getLen();

String path = fileStatus.getPath().toString();

System.out.println(isDir+"\t"+permission+"\t"+replication+"\t"+len+"\t"+path);

}

}

private static void getData(FileSystem fileSystem) throws IOException {

FSDataInputStream in = fileSystem.open(new Path(FILE));

IOUtils.copyBytes(in, System.out, 1024);

}

private static void putData(FileSystem fileSystem) throws IOException, FileNotFoundException {

FSDataOutputStream out = fileSystem.create(new Path(FILE));

FileInputStream in = new FileInputStream("E:/readme.txt");

IOUtils.copyBytes(in, out, 1024,true);

}

private static void remove(FileSystem fileSystem) throws IOException {

fileSystem.delete(new Path(DIR), true);

}

private static void mkdir(FileSystem fileSystem) throws IOException {

fileSystem.mkdirs(new Path(DIR));

}

private static FileSystem  getFileSystem() throws IOException, URISyntaxException {

return FileSystem.get(new URI(PATH), new Configuration());

}

}

上一篇下一篇

猜你喜欢

热点阅读