HDFS Java API使用之读取上传文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDFS Java API使用之读取上传文件相关的知识,希望对你有一定的参考价值。
package com.ibeifeng.hadoop.senior.hdfs;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
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;
/**
*
* @author wangw
*
*/
public class HdfsApp {
/**
*
* @return
* @throws Exception
* get filesystem
*/
public static FileSystem getFileSystem() throws Exception {
// core-site.xml
Configuration conf = new Configuration();
//get filesystem
FileSystem fileSystem = FileSystem.get(conf);
//System.out.println(fileSystem);
return fileSystem;
}
/**
* read data
* @param fileName
* @throws Exception
*/
public static void read(String fileName) throws Exception {
//get filesystem
FileSystem fileSystem = getFileSystem();
//String fileName = "/user/wangw/mapreduce/wordcount/wc.input";
// read path
Path readPath = new Path(fileName);
//open file
FSDataInputStream inStream = fileSystem.open(readPath);
//
try{
//read
IOUtils.copyBytes(inStream, System.out, 4096, false);
}catch(Exception e){
e.printStackTrace();
}finally{
// close Stream
IOUtils.closeStream(inStream);
}
}
public static void main(String[] args) throws Exception {
//String fileName = "/user/wangw/mapreduce/wordcount/wc.input";
//read(fileName);
//get filesystem
FileSystem fileSystem = getFileSystem();
// write path
String putFileName = "/user/wangw/put-wc.input";
Path writePath = new Path(putFileName);
//Output Stream
FSDataOutputStream outStream = fileSystem.create(writePath);
// file input Stream
FileInputStream inStream = new FileInputStream(new File(
"/opt/modules/hadoop-2.5.0/wc.input"));
// stream read/write
try{
//read
IOUtils.copyBytes(inStream, outStream, 4096, false);
}catch(Exception e){
e.printStackTrace();
}finally{
// close Stream
IOUtils.closeStream(inStream);
IOUtils.closeStream(outStream);
}
}
}
以上是关于HDFS Java API使用之读取上传文件的主要内容,如果未能解决你的问题,请参考以下文章
Hadoop HDFS编程 API入门系列之路径过滤上传多个文件到HDFS
Hadoop——HDFS文件系统的Java API操作(上传下载查看删除创建文件)详细教学