eclipse 对 hadoop1.2.1 hdfs 文件操作
package com.hdfs;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
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;
public class HDFSUtils {
private static final String HDFS_PATH = "hdfs://test1:9000/opt/hadoop-1.2.1/fff/hadooptest.txt";
private static final String HDFS_File_PATH = "hdfs://test1:9000";
private static final String ADD_DIR_PATH = "/firstdir";
private static final String UPLOAD_DIR_PATH = "/firstdir/firstfile";
public static void main(String[] args) throws Exception {
// //读取文件内容
// //将hdfs协议转换成http协议
// URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
// final URL url = new URL(HDFS_PATH);
// final InputStream in = url.openStream();
// /**
// * 读取文件内容
// * @param in 输入流
// * @param out 输出流
// * @param buffsize 缓冲区大小
// * @param close 是否关闭流
// */
// IOUtils.copyBytes(in, System.out, 1024, true);
//获取hdfs文件类
final FileSystem fileSystem = FileSystem.get(new URI(HDFS_File_PATH), new Configuration());
//创建文件夹
// mkdir(fileSystem);
//上传文件
// upload(fileSystem);
//下载文件
// download(fileSystem);
//删除文件
// deleteFile(fileSystem);
}
/**
* @param fileSystem
* @throws IOException
*/
private static void deleteFile(final FileSystem fileSystem)
throws IOException {
fileSystem.delete(new Path(UPLOAD_DIR_PATH), true);
}
/**
* @param fileSystem
* @throws IOException
*/
private static void download(final FileSystem fileSystem)
throws IOException {
FSDataInputStream in = fileSystem.open(new Path(UPLOAD_DIR_PATH));
IOUtils.copyBytes(in, System.out, 1024, true);
}
/**
* @param fileSystem
* @throws IOException
* @throws FileNotFoundException
*/
private static void upload(final FileSystem fileSystem) throws IOException,
FileNotFoundException {
FSDataOutputStream out = fileSystem.create(new Path(UPLOAD_DIR_PATH));
FileInputStream in = new FileInputStream("e:/log.txt");
IOUtils.copyBytes(in, out, 1024, true);
}
/**
* @param fileSystem
* @throws IOException
*/
private static void mkdir(final FileSystem fileSystem) throws IOException {
fileSystem.mkdirs(new Path(ADD_DIR_PATH));
}
}