hadoop常用api编写
2020-12-13 06:26
标签:style java io 文件 ar new 管理 size package hsfs常用api; import java.io.ByteArrayInputStream; import org.apache.hadoop.conf.Configuration; //FileSystem类 用于管理hdfs下的文件 public class api { private static void delete(final FileSystem filesystem) throws IOException { private static void downloaddata(final FileSystem filesystem) private static void uploaddata(final FileSystem filesystem) private static void makedirectory(final FileSystem filesystem) hadoop常用api编写,搜素材,soscw.com hadoop常用api编写 标签:style java io 文件 ar new 管理 size 原文地址:http://www.cnblogs.com/hanyangmo/p/3905622.html
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
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 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);
}
filesystem.delete(new Path(FILE_PATH), true); //删除FILE_PATH下文件(夹)
}
throws IOException {
final FSDataInputStream in = filesystem.open(new Path(FILE_PATH)); //在hdfs上创建输入流文件FILE_PATH
IOUtils.copyBytes(in, System.out, 1024, true);//输出到控制台
}
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)
}
throws IOException {
filesystem.mkdirs(new Path(DIR_PATH)); //在hdfs上新建文件在路经DIR_PATH下
}
}
下一篇:Windows 7 备份和恢复