首页 > 代码库 > hadoop常用api编写

hadoop常用api编写

package hsfs常用api;

import java.io.ByteArrayInputStream;
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.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

//FileSystem类 用于管理hdfs下的文件

public class api {
public static final String HDFS_PATH = "hdfs://192.168.23.128:9000";
public static final String DIR_PATH = "/234";
public static final String FILE_PATH = "/234/f345";

public static void main(String[] args) throws Exception {
final FileSystem filesystem = FileSystem.get(new URI(HDFS_PATH) , new Configuration()); //构建FileSystem类对象

//创建文件夹
// makedirectory(filesystem);
//上传文件
// uploaddata(filesystem);
//下载文件
// downloaddata(filesystem);
//删除文件(夹)
delete(filesystem);
}

private static void delete(final FileSystem filesystem) throws IOException {
filesystem.delete(new Path(FILE_PATH), true); //删除FILE_PATH下文件(夹)
}

private static void downloaddata(final FileSystem filesystem)
throws IOException {
final FSDataInputStream in = filesystem.open(new Path(FILE_PATH)); //在hdfs上创建输入流文件FILE_PATH
IOUtils.copyBytes(in, System.out, 1024, true);//输出到控制台
}

private static void uploaddata(final FileSystem filesystem)
throws FileNotFoundException, IOException {
final FileInputStream in = new FileInputStream("/usr/local/f345"); //在本地新建文件f345
final FSDataOutputStream out = filesystem.create(new Path(FILE_PATH));//在hdfs上创建输出路径FILE_PATH
IOUtils.copyBytes(in, out, 1024, true); //(输入,输出,换冲,true)
}

private static void makedirectory(final FileSystem filesystem)
throws IOException {
filesystem.mkdirs(new Path(DIR_PATH)); //在hdfs上新建文件在路经DIR_PATH下
}
}