zoukankan      html  css  js  c++  java
  • HDFS API基本操作

    对HDFS API基本操作都是通过 org.apache.hadoop.fs.FileSystem类进行的,以下是一些常见的操作:

    package HdfsAPI;
    
    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URI;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.BlockLocation;
    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.Test;
    
    import junit.framework.TestCase;
    
    
    public class HDFSAPI extends TestCase{  
        public static String hdfsUrl = "hdfs://master:8020";
        //创建文件夹
        @Test
        public void testHDFSmkdir() throws IOException{
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(URI.create(hdfsUrl),conf);
            Path path = new Path("/liguodong/hdfs");
            fs.mkdirs(path);
        }
    
        //创建文件
        @Test
        public void testHDFSmkFile() throws IOException{
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(URI.create(hdfsUrl),conf);
            Path path = new Path("/liguodong/hdfs/liguodong.txt");
            FSDataOutputStream fdos = fs.create(path); 
            fdos.write("hello hadoop".getBytes());
        }
        //重命名
        @Test
        public void testHDFSRenameFile() throws IOException{
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(URI.create(hdfsUrl),conf);
            Path path = new Path("/liguodong/hdfs/liguodong.txt");
            Path Renamepath = new Path("/liguodong/hdfs/love.txt");
            System.out.println(fs.rename(path, Renamepath));
        }
        //上传一个本地文件
        @Test
        public void testHDFSPutLocalFile1() throws IOException{
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(URI.create(hdfsUrl),conf);
            //因为在windows上面调试,所以选择的是windows的文件夹,
            //假设在Linxu,须要Linux文件夹。
            Path srcpath = new Path("g:/liguodong.txt");
            Path destpath = new Path("/liguodong/hdfs");
            fs.copyFromLocalFile(srcpath, destpath);
        }
    
        @Test
        public void testHDFSPutLocalFile2() throws IOException{
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(URI.create(hdfsUrl),conf);
            //因为在windows上面调试,所以选择的是windows的文件夹,
            //假设在Linxu,须要Linux文件夹。
            String srcpath ="g:/oncemore.txt";
            Path destpath = new Path("/liguodong/hdfs/kkk.txt");
            InputStream is = new BufferedInputStream(
                    new FileInputStream(new File(srcpath)));
            FSDataOutputStream fdos = fs.create(destpath);
            IOUtils.copyBytes(is, fdos, 4094);
        }
    
        //查看某个文件夹以下的全部文件
        @Test
        public void testHDFSListFiles() throws IOException{
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(URI.create(hdfsUrl),conf);
            Path path = new Path("/liguodong/hdfs");
            FileStatus[] files = fs.listStatus(path);
    
            for (FileStatus file : files) {
                System.out.println(file.getPath().toString());
            }
        }
    
        //查看某个文件的数据块信息
        @Test
        public void testHDFSGetBlockInfo() throws IOException{
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(URI.create(hdfsUrl),conf);
            Path path = new Path("/liguodong/hdfs/kkk.txt");
            FileStatus filestatus = fs.getFileStatus(path);
            BlockLocation[] blkLoc = fs.getFileBlockLocations
                    (filestatus, 0, filestatus.getLen());
            for (BlockLocation loc : blkLoc) {
                for (int i = 0; i < loc.getHosts().length; i++) {
                    //获取数据块在哪些主机上
                    System.out.println(loc.getHosts()[i]);//获取文件块的主机名
                    //因为这个文件仅仅有一个块。所以输出结果为:slave2、slave1、slave5
                }
            }
        }   
    }
  • 相关阅读:
    ubuntu下安装jupyter notebook问题。ERROR: Package ‘ipython‘ requires a different Python: 3.5.2 not in 」=3.6
    VmWare虚拟机设置ubuntu和windows之间的共享文件夹
    标准测试宏选项
    C库函数、系统函数等调用错误的处理方法
    比atoi()函数更健壮的一类包装函数
    系统函数和C库函数调用的几种错误处理方法
    经典生产者-消费者问题解析
    linux下的并发编程详解
    一文学会GDB操作命令
    Stm32CubeMx lwip+freeRTOS TCP 服务
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5382414.html
Copyright © 2011-2022 走看看