• 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());
            
        }
        
    
    }

  • 相关阅读:
    【php数组函数序列】之array_values()
    Mysql数据库编码问题3(修改数据库,表,字段编码为utf8)
    framework fckedit / KingEditor
    Linux + JDK/TOMCAT/Dia(Planner)/eclipse
    proxyServer squid / varnish / apache traffic server / ATS
    framework SiteMesh
    Linux + BusyBox
    对链表的插入操作
    链表原理
    链表的删除操作
  • 原文地址:https://www.cnblogs.com/LXL616/p/10897658.html
Copyright © 2020-2023  润新知