代码如下, 后备参考:
package com.bigdata.hadoop.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; import org.junit.Test; public class HdfsTest { public static FileSystem getFileSystem() throws IOException{
Configuration configuration = new Configuration(); //Some configurations used to replace the configuration of resource files //configuration.set("fs.defaultFS", "hdfs://linux-66-64.liuwl.com:8020"); //... return FileSystem.get(configuration); } @Test public static void read() throws IOException{ String fileName = "/user/liuwl/tmp/core-site.xml"; FileSystem fileSystem = getFileSystem(); Path path = new Path(fileName); FSDataInputStream instream = fileSystem.open(path); try{ IOUtils.copyBytes(instream, System.out, 4896,false); }catch(Exception e){ e.getStackTrace(); }finally{ IOUtils.closeStream(instream); } } @Test public void write() throws IOException{ String fileName = "/user/liuwl/tmp/input/wc.input"; FileSystem fileSystem = getFileSystem(); Path writePath = new Path(fileName); FSDataOutputStream outStream = fileSystem.create(writePath); FileInputStream inStream = new FileInputStream(new File("/home/liuwl/opt/data/wc.input")); try{ IOUtils.copyBytes(inStream, outStream, 4896,false); }catch(Exception e){ e.getStackTrace(); }finally{ IOUtils.closeStream(inStream); IOUtils.closeStream(outStream); } } }