hdfs io流
package hdfsutil;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.commons.compress.utils.IOUtils;
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.junit.Before;
import org.junit.Test;
public class hdfsIO {
FileSystem fs = null;
@Before
public void init() throws IOException, InterruptedException, URISyntaxException {
fs = FileSystem.get(new URI("hdfs://hadoop01:9000"), new Configuration(), "root");
}
@Test
public void testDownload() throws IllegalArgumentException, IOException {
FSDataInputStream in = fs.open(new Path("/ttt.txt"));
FileOutputStream out = new FileOutputStream("C:/ttt.txt");
org.apache.hadoop.io.IOUtils.copyBytes(in, out, new Configuration());
org.apache.hadoop.io.IOUtils.closeStream(in);
org.apache.hadoop.io.IOUtils.closeStream(out);
}
@Test
public void testSeek() throws IllegalArgumentException, IOException {
FSDataInputStream in = fs.open(new Path("/test.txt"));
in.seek(6);
FileOutputStream out = new FileOutputStream("c:/test.seg.txt");
org.apache.hadoop.io.IOUtils.copyBytes(in, out, 4096);
org.apache.hadoop.io.IOUtils.closeStream(in);
org.apache.hadoop.io.IOUtils.closeStream(out);
}
@Test
public void tsetUpload() throws IllegalArgumentException, IOException {
FileInputStream in = new FileInputStream("c:/test.seg.txt");
FSDataOutputStream out = fs.create(new Path("/test.seg.txt"));
org.apache.hadoop.io.IOUtils.copyBytes(in, out, 4096);
org.apache.hadoop.io.IOUtils.closeStream(in);
org.apache.hadoop.io.IOUtils.closeStream(out);
}
}