hadoop-hdfs-API

package com.lxl.hadoop.hdfs;

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 TestHDFS {
    
    Configuration conf;
    FileSystem fs;
    
    @Before
    public void conn() throws Exception{
        
        conf = new Configuration(true);
        
        fs = FileSystem.get(conf);
        
        
    }
    
    
    @After
    public void close() throws Exception{
        fs.close();
    }
    

    
    /**  
    * Description: 创建目录
    * @author lxl  
    * @date 2019年5月21日  
    */  
    @Test
    public void mkdir() throws Exception{
        
        Path ifile = new Path("/ooxx");
        
        if(fs.exists(ifile)){
            fs.delete(ifile, true);
        }
        fs.mkdirs(ifile);
        
        
    }
    
    
    
    /**  
    * Description: 上传文件
    * @author lxl  
    * @date 2019年5月21日  
    */  
    @Test
    public void upload() throws Exception{
        
        Path f = new Path("/ooxx/hello.txt");
        FSDataOutputStream output = fs.create(f);
        
        InputStream input = new BufferedInputStream(new FileInputStream(new File("C:\\nginx")));
        
        
        IOUtils.copyBytes(input, output, conf, true);
        
    }
    

    /**  
    * Description: 获取block信息 读取文件
    * @author lxl  
    * @date 2019年5月21日  
    */ 
    @Test
    public void blks() throws Exception{
        
        Path i = new Path("/user/root/test.txt");
        FileStatus ifile = fs.getFileLinkStatus(i );
        BlockLocation[] blks = fs.getFileBlockLocations(ifile , 0, ifile.getLen());
        
        for (BlockLocation b : blks) {
            
            System.out.println(b);
            
        }
        
        FSDataInputStream in = fs.open(i);
        
        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());
        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());
        
        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());
        
    }
    

}

猜你喜欢

转载自www.cnblogs.com/LXL616/p/10897658.html