Eclipse-HadoopAPI开发测试#
1. 如何获取文件系统##
public static FileSystem getFileSystem() throws Exception { //读取配置文件, read core-*.xml; hdfs-*.xml Configuration configuration = new Configuration(); FileSystem fileSystem = FileSystem.get(configuration); System.out.println(fileSystem); return fileSystem; } public static void main(String[] args) throws Exception { //create configuration, read core-*.xml; hdfs-*.xml Configuration configuration = new Configuration(); FileSystem fileSystem = FileSystem.get(configuration); System.out.println(fileSystem); }
读取配置文件
public static void read(String filename)throws Exception { FileSystem fileSystem = getFileSystem(); //read Path Path readPath = new Path(filename); //input stream FSDataInputStream inStream = fileSystem.open(readPath); try{ IOUtils.copyBytes(inStream, System.out, 4096, false); }catch(Exception e){ e.printStackTrace(); } finally{ } }
写入文件
`public static void main(String[] args) throws Exception {
/* String filename = "/user/beifeng/core-site.xml";
read(filename); */
String filename = "/user/beifeng/wc.input";
FileSystem fileSystem = getFileSystem();
//read Path
Path readPath = new Path(filename); //write file
FSDataOutputStream outStream = fileSystem.create(readPath);
//get input stream
FileInputStream inStream = new FileInputStream( new File("/opt/datas/wc.input") );
try{
IOUtils.copyBytes(inStream, outStream, 4096, false);
}catch(Exception e){
e.printStackTrace();
}finally{
IOUtils.closeStream(inStream); IOUtils.closeStream(outStream);
}
}`
网友评论