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]); } } }
  • 相关阅读:
    BZOJ 1176: [Balkan2007]Mokia
    BZOJ 4034: [HAOI2015]T2
    BZOJ 4031: [HEOI2015]小Z的房间
    BZOJ 4128: Matrix
    JSP学习-08-JavaBean
    JSP学习-标准标签库
    电影剧本写作基础
    JSP学习-09-自定义标签
    JSP学习-07Cookie 与Session
    JSP学习-06过滤器
  • 原文地址:https://www.cnblogs.com/i80386/p/3439132.html
Copyright © 2011-2022 走看看