hdfs的一些操作

来源:互联网 发布:linux 断电 丢失 文件 编辑:程序博客网 时间:2024/06/05 03:45
import java.io.FileNotFoundException;import java.io.IOException;import java.net.URI;import java.net.URISyntaxException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.BlockLocation;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.LocatedFileStatus;import org.apache.hadoop.fs.Path;import org.apache.hadoop.fs.RemoteIterator;import org.junit.Before;import org.junit.Test;public class HdfsUtils {    public FileSystem fs = null;    @Before    public void init() throws IOException, InterruptedException, URISyntaxException {        fs = FileSystem.get(new URI("hdfs://os-1:9000"),new Configuration(),"root");    }    // 从hdfs下载文件到本地    @Test    public void  downLocal() throws IllegalArgumentException, IOException {        fs.copyToLocalFile(false,new Path("/Test1.java"), new Path("C:/Users/os/Desktop/share"),true);        fs.close();    }    // 文件夹操作    @Test    public void testDir() throws IllegalArgumentException, IOException {        fs.mkdirs(new Path("/aaa"));        boolean exists = fs.exists(new Path("/aaa"));        if(exists) {            System.out.println("创建了一个文件件: /aaa");        }        fs.close();    }    // 文件信息的查看    @Test    public void testFileStatus() throws FileNotFoundException, IllegalArgumentException, IOException {        RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);        while(listFiles.hasNext()) {            LocatedFileStatus fileStatus = listFiles.next();            System.out.println(fileStatus.getPath().getName()); //文件名        }    }    //    @Test()    public void  testOthers() throws IllegalArgumentException, IOException {        BlockLocation[] fileBlockLocations =  fs.getFileBlockLocations(new Path("/anaconda-ks.cfg"), 18, 100);        for(BlockLocation location:fileBlockLocations) {            System.out.println(location.getOffset());            System.out.println(location.getNames()[0]);        }        //修改副本数量        fs.setReplication(new Path("/anaconda-ks.cfg"),(short) 2);    }    public  void main(String[] args) throws IOException {        //Configuration conf = new Configuration();        //conf.set("fs.defaultFS", "hdfs://192.168.146.129:9000");        //fs = FileSystem.get(conf);        //FileSystem fs = FileSystem.get(new URI("hdfs://os-1:9000"),conf,"root");        fs.copyFromLocalFile(new Path("C:\\Users\\os\\eclipse-workspace\\hadoop-1\\src\\os\\unis\\cn\\Test1.java"), new Path("hdfs://os-1:9000/"));        fs.close();    }}
原创粉丝点击