zoukankan      html  css  js  c++  java
  • Hadoop 学习笔记(二) HDFS API

    4.删除HDFS上的文件
    package proj;
    
    import java.io.IOException;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.Path;
    
    public class DeleteFile {
    
        public static void main(String[] args) throws IOException {
            Configuration conf = new Configuration();
            //要点:没有这句会传到本地文件系统,而不是hdfs
            conf.set("fs.default.name","hdfs://localhost:9000");
            FileSystem hdfs = FileSystem.get(conf);
            Path delef = new Path("in/test3.txt");
            boolean isDeleted = hdfs.delete(delef, false);
            //递归删除
            //boolean isDelete = hdfs.delete(delef, true);
            System.out.println("delete? "+ isDeleted);
        }
    
    }
    检查文件是否存在
    package
    proj; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class CheckFile { public static void main(String[] args) throws IOException { Configuration conf = new Configuration(); //要点:没有这句会传到本地文件系统,而不是hdfs conf.set("fs.default.name","hdfs://localhost:9000"); FileSystem hdfs = FileSystem.get(conf); Path findf = new Path("in/hello.c"); boolean exist = hdfs.exists(findf); System.out.println("exist ? " + exist); } }
    查找某个文件在HDFS集群的位置
    package
    proj; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.BlockLocation; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class FileLocal { public static void main(String[] args) throws IOException{ Configuration conf = new Configuration(); //要点:没有这句会传到本地文件系统,而不是hdfs conf.set("fs.default.name","hdfs://localhost:9000"); FileSystem hdfs = FileSystem.get(conf); Path fpath = new Path("in/hello.c"); FileStatus filestatus = hdfs.getFileStatus(fpath); BlockLocation[] blkLocations = hdfs.getFileBlockLocations(filestatus, 0, filestatus.getLen()); int blockLen = blkLocations.length; for (int i = 0; i < blockLen; i++) { String[] hosts = blkLocations[i].getHosts(); System.out.println("block "+i+" location "+ hosts[i]); } } }
    获取HDFS集群上所有节点名称
    package
    proj; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.hdfs.DistributedFileSystem; import org.apache.hadoop.hdfs.protocol.DatanodeInfo; public class GetList { public static void main(String[] args) throws IOException { Configuration conf = new Configuration(); //要点:没有这句会传到本地文件系统,而不是hdfs conf.set("fs.default.name","hdfs://localhost:9000"); FileSystem fs = FileSystem.get(conf); DistributedFileSystem hdfs = (DistributedFileSystem)fs; DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats(); String[] names = new String[dataNodeStats.length]; for (int i = 0; i < dataNodeStats.length; i++) { names[i] = dataNodeStats[i].getHostName(); System.out.println("node " + i + " name " + names[i]); } } }
  • 相关阅读:
    2014年辛星完全解读Javascript第六节 对象
    2014年辛星完全解读Javascript第五节 break和continue与错误处理
    2014年辛星完全解读Javascript第四节 流程控制语句
    2014年辛星完全解读Javascript第三节
    移动端滑动卡顿问题
    移动端iOS阻止橡皮筋效果
    inline-block 元素之间的空白问题
    初识webview
    原型链、prototype、_proto_那些事
    VMware workstation转到vsphere解决办法
  • 原文地址:https://www.cnblogs.com/i80386/p/3439132.html
Copyright © 2011-2022 走看看