zoukankan      html  css  js  c++  java
  • 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());
            
        }
        
    
    }

  • 相关阅读:
    Android Studio 优秀插件: Parcelable Code Generator
    Android Studio 优秀插件:GsonFormat
    DrawerLayout(抽屉效果)
    Python拼接字符串的七种方式
    Python爬虫教程-使用chardet
    Python爬虫教程-实现百度翻译
    Tensorflow分布式部署和开发
    简单的Python GUI界面框架
    用keras构建自己的网络层 TensorFlow2.0教程
    Python GUI教程一:Hello World
  • 原文地址:https://www.cnblogs.com/LXL616/p/10897658.html
Copyright © 2011-2022 走看看