package com.bjsxt.mr;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class test {
Configuration conf;
FileSystem fs;
@Before
public void conn() throws IOException{
conf = new Configuration(true);
fs = FileSystem.get(conf);
}
@After
public void close() throws IOException{
fs.close();
}
@Test
public void mkdir() throws IOException{
Path dirs = new Path("/data");
if(fs.exists(dirs )){
fs.delete(dirs,true);
}
fs.mkdirs(dirs);
}
@Test
public void upload() throws IOException{
Path output = new Path("/data/sxt.txt");
FSDataOutputStream out = fs.create(output );
InputStream input = new BufferedInputStream(new FileInputStream(new File("c:\\nginx.txt")));
IOUtils.copyBytes(input,out,conf,true);
}
@Test
public void blks() throws IOException{
Path f = new Path("/user/root/test.txt");
FileStatus file = fs.getFileStatus(f);
BlockLocation[] blks = fs.getFileBlockLocations(file, 0, file.getLen());
for (BlockLocation b : blks){
System.out.println(b);
}
FSDataInputStream in = fs.open(f);
System.out.println((char)in.readByte());
System.out.println((char)in.readByte());
System.out.println((char)in.readByte());
System.out.println((char)in.readByte());
//让指针转移到这个位置
in.seek(1048576);
System.out.println((char)in.readByte());
System.out.println((char)in.readByte());
System.out.println((char)in.readByte());
System.out.println((char)in.readByte());
System.out.println((char)in.readByte());
}
}