大数据分析 HDFS程序


大数据分析 程序内容整理 HDFS

复习的时候应注意各个程序之间的公共部分以及各自的独特部分,这样可以更加清楚

比如在所有的程序里,都有一个程序结构:

try{
    有用的内容
}catch (Exception e) {
    e.printStackTrace();
}

注:以下程序导入包的部分均省略。

HDFS

在HDFS上创建txt文件

/*
 * 在HDFS上创建txt文件
 */
package com.hdfs;
import ...
public class HdfsTest {
    public static void main(String[] args) throws Exception {
            //----------------------------------------------
            //获得虚拟机(即通过shell控制的那个环境)的属性
            Properties properties = System.getProperties(); 
            properties.setProperty("HADOOP_USER_NAME", "root"); 
            //----------------------------------------------
            //设置配置,包括使用的FileSystem对应的ip,以及DistributedFileSystem这个类
            Configuration conf = new Configuration(); 
            conf.set("fs.defaultFS", "hdfs://172.18.4.15:9000");
            conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
            //----------------------------------------------
            try {
            //----------------------------------------------
            //主函数内自主编写的内容,调用了创建文件函数、验证文件存在函数和打开文件函数
                FileSystem hdfs = FileSystem.get(conf);
                createFile(hdfs);
                existsFile(hdfs);
                //System.out.printf("\n");
                openFile(hdfs);
            //----------------------------------------------
            } catch (Exception e) {
                // try{}catch(Exception e){e.printStackTrace}是一个固定的结构,知道有用的东西在try{}里就行
                e.printStackTrace();
            } }
    //"createFile"函数内容,功能为创建一个文件
    public static void createFile(FileSystem hdfs) throws Exception {
        byte[] buff = "hello, xx!".getBytes(); //设置创建的文件内的内容
        Path dfs = new Path("/hadoop2/xiaoyuzhu/xiaoyuzhuword.txt"); //创建的文件所在的HDFS路径
        FSDataOutputStream outputStream = hdfs.create(dfs); //在上一行的路径里创建一个文件
        outputStream.write(buff, 0, buff.length); //往创建的文件里写入文件的内容
        outputStream.close(); }  //关闭输出流
    //“existsFile”函数内容,功能为验证一个文件是否存在
    public static void existsFile(FileSystem hdfs) throws Exception {
        Path findf = new Path("/hadoop2/xiaoyuzhu/xiaoyuzhuword.txt");//要验证的文件的HDFS路径
        boolean isExists = hdfs.exists(findf); //返回是否存在的布尔值(true或false)
        System.out.println(isExists);//输出这个布尔值
    }
    //"openFile"函数内容,功能为打开一个文件
    public static void openFile(FileSystem hdfs) throws Exception{
        Path file = new Path("/hadoop2/xiaoyuzhu/xiaoyuzhuword.txt");//要打开的文件的HDFS路径
        //-----------------------------------------------------------------
        //打开该文件
        FSDataInputStream in = hdfs.open(file);
        BufferedReader d = new BufferedReader(new InputStreamReader(in));
        //-----------------------------------------------------------------
        //输出该文件内容
        String content;
        while((content=d.readLine())!=null) { //先读取内容赋给content变量,当content不为null时说明以及读取到了内容,这时候输出内容
            System.out.println(content);
        }
        //-----------------------------------------------------------------
        d.close();
        hdfs.close();
    }
}

此程序的关键内容为:

creatFile函数:

从本地复制文件到HDFS上

/*
 * 从本地复制文件到HDFS上
 */
package com.hdfs;
import ...
public class Hdfs9_1Test {
    public static void main(String[] args) throws Exception {
            //-------------------------------------------------------------------------
            //获得属性、进行配置(固定内容)
            Properties properties = System.getProperties();
            properties.setProperty("HADOOP_USER_NAME", "root");
            Configuration conf = new Configuration();
            conf.set("fs.defaultFS", "hdfs://172.18.4.15:9000");
            conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
            //-------------------------------------------------------------------------
            try {
                //----------------------------------------------
                //主函数内自主编写的内容,调用了文件复制函数
                FileSystem hdfs = FileSystem.get(conf);
                Path srcPath = new Path("D:/Part456/file/shuihuzhuan.txt"); //本地上传文件路径
                //Path srcPath = new Path("/opt/test91.txt");
                Path dstPath = new Path("/hadoop2/xiaoyuzhu/input3"); //hdfs目标路径
                //调用文件系统的文件复制函数,前面参数是指是否删除原文件,true为删除,默认为false
                hdfs.copyFromLocalFile(false, srcPath, dstPath); 
                existsFile(hdfs); //验证文件是否复制到目标路径
                openFile(hdfs); //打开复制后的文件
                //---------------------------------------------------------------------
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } }
    //“existsFile”函数内容,功能为验证一个文件是否存在
    public static void existsFile(FileSystem hdfs) throws Exception {
        Path findf = new Path("/hadoop2/xiaoyuzhu/xiaoyuzhuword.txt");//要验证的文件的HDFS路径
        boolean isExists = hdfs.exists(findf); //返回是否存在的布尔值(true或false)
        System.out.println(isExists);//输出这个布尔值
    }
    //"openFile"函数内容,功能为打开一个文件
    public static void openFile(FileSystem hdfs) throws Exception{
        Path file = new Path("/hadoop2/xiaoyuzhu/xiaoyuzhuword.txt");//要打开的文件的HDFS路径
        //-----------------------------------------------------------------
        //打开该文件
        FSDataInputStream in = hdfs.open(file);
        BufferedReader d = new BufferedReader(new InputStreamReader(in));
        //-----------------------------------------------------------------
        //输出该文件内容
        String content;
        while((content=d.readLine())!=null) { //先读取内容赋给content变量,当content不为null时说明以及读取到了内容,这时候输出内容
            System.out.println(content);
        }
        //-----------------------------------------------------------------
        d.close();
        hdfs.close();
    }
}

此程序的关键内容为:

在HDFS上删除文件

/*
 * 在HDFS上删除文件
 */
package com.hdfs;

import ...
public class Hdfs9_2Test {
    public static void main(String[] args) throws Exception {
        //-------------------------------------------------------------------------
        //获得属性、进行配置(固定内容)
        Properties properties = System.getProperties();
        properties.setProperty("HADOOP_USER_NAME", "root");
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://172.18.4.15:9000");
        conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
        //-------------------------------------------------------------------------
        try {
            //----------------------------------------------
            //主函数内自主编写的内容,调用了文件删除函数
                FileSystem hdfs = FileSystem.get(conf);
                Path path = new Path("/hadoop2/xiaoyuzhu/output2"); //待删除文件路径             
                boolean test = hdfs.deleteOnExit(path); //删除文件,并返回删除的结果(true或false)
                if(test){
                    System.out.println("delete success!");
                }else{
                    System.out.println("delete failure");
                }
                hdfs.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } }
}

此程序的关键内容为:

统计HDFS文件夹的状态信息

/*
 * 统计HDFS文件夹的状态信息
 */
package com.hdfs;

import ...
public class Hdfs9_3Test {
    public static void main(String[] args) throws Exception {
            //-------------------------------------------------------------------------
            //获得属性、进行配置(固定内容)
            Properties properties = System.getProperties();
            properties.setProperty("HADOOP_USER_NAME", "root");
            Configuration conf = new Configuration();
            conf.set("fs.defaultFS", "hdfs://172.18.4.15:9000");
            conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
            //-------------------------------------------------------------------------
            try {
                //----------------------------------------------
                //主函数内自主编写的内容,调用了列举状态函数:listStatus
                FileSystem hdfs = FileSystem.get(conf);
                Path path = new Path("/xiaoyuzhu"); //待统计的路径         
                FileStatus[] filelist = hdfs.listStatus(path); //获取统计信息
                //----------------------------------------------
                //输出统计信息
                for (int i = 0; i < filelist.length; i++) { 
                    System.out.println("_________" + path + "目录下所有文件______________");
                    FileStatus fileStatus = filelist[i];
                    System.out.println("Name:"+fileStatus.getPath().getName());
                    System.out.println("Size:"+fileStatus.getLen());
                    System.out.println("Path:"+fileStatus.getPath());
                }
                //----------------------------------------------
                hdfs.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } }
}

此程序的关键内容为:

获取HDFS文件的块位置信息

/*
 * 获取HDFS文件的块位置信息
 */
package com.hdfs;

import ...
public class Hdfs9_4Test {
    public static void main(String[] args) throws Exception {
            //-------------------------------------------------------------------------
            //获得属性、进行配置(固定内容)
            Properties properties = System.getProperties();
            properties.setProperty("HADOOP_USER_NAME", "root");
            Configuration conf = new Configuration();
            conf.set("fs.defaultFS", "hdfs://172.18.4.15:9000");
            conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
            //-------------------------------------------------------------------------
            try {
                //----------------------------------------------
                //主函数内自主编写的内容,调用了获取文件块位置信息函数:getFileBlockLocations
                FileSystem hdfs = FileSystem.get(conf);
                Path path = new Path("/xiaoyuzhu/word.txt"); //待统计的路径         
                System.out.println(hdfs.getFileBlockLocations(path, 0, 0));    //输出块位置信息  
                hdfs.close();
                //----------------------------------------------
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } }
}

此程序的关键内容为:

从HDFS下载文件到本地

/*
 * 从HDFS下载文件到本地 
 */
package com.hdfs;

import ...
public class Download_file {
    public static void main(String[] args) throws Exception {
            //-------------------------------------------------------------------------
            //获得属性、进行配置(固定内容)
            Properties properties = System.getProperties();
            properties.setProperty("HADOOP_USER_NAME", "root");
            Configuration conf = new Configuration();
            conf.set("fs.defaultFS", "hdfs://172.18.4.15:9000");
            conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
            //-------------------------------------------------------------------------
            try {
                //----------------------------------------------
                //主函数内自主编写的内容,调用了文件复制到本地函数
                FileSystem hdfs = FileSystem.get(conf);
                Path src = new Path("/hadoop2/xiaoyuzhu/output3/part-r-00000"); //HDFS源文件路径 
                Path dst = new Path("C:/Users/Lenovo/Desktop/大数据报告/file"); //下载至本地路径
                hdfs.copyToLocalFile(src,dst); 
                hdfs.close();
                //----------------------------------------------
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } }

}

此程序的关键内容为:

HDFS程序的公共部分

设置属性、进行配置

existsFile函数

openFile函数


文章作者: Mat Jenin
文章链接: http://matjenin.xyz
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Mat Jenin !
  目录