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]); } } }
  • 相关阅读:
    程序员是脑力劳动还是体力劳动
    我理解的技术管理的核心工作----搭班子和带团队
    Ubuntu系统下在PyCharm里用virtualenv集成TensorFlow
    我理解的技术管理的核心工作----定战略
    数据分析师岗位的一点理解
    python中读写excel并存入mysql
    mac里用PyCharm中引用MySqlDB始末
    贝叶斯网络的通俗解读
    将sqlserver导出的csv数据导入到ubuntu和mac上的mysql
    Java之Spring Cloud概念介绍(非原创)
  • 原文地址:https://www.cnblogs.com/i80386/p/3439132.html
Copyright © 2011-2022 走看看