博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDFS API编程
阅读量:7174 次
发布时间:2019-06-29

本文共 13237 字,大约阅读时间需要 44 分钟。

3.1常用类

       3.1.1Configuration

Hadoop配置文件的管理类,该类的对象封装了客户端或者服务器的配置(配置集群时,所有的xml文件根节点都是configuration)

创建一个Configuration对象时,其构造方法会默认加载hadoop中的两个配置文件,分别是hdfs-site.xml以及core-site.xml,这两个文件中会有访问hdfs所需的参数值,主要是fs.default.name,指定了hdfs的地址,有了这个地址客户端就可以通过这个地址访问hdfs了。即可理解为configuration就是hadoop中的配置信息。

 

     3.1.2 FileSystem

该类的对象是一个文件系统对象,对HDFS中的文件进行的一系列操作,如创建等

 

      3.1.3FileStatus

获取文件或者文件夹的元信息!比如:文件路径,文件大小,文件所有者,所在的块大小,文件修改时间,备份数量,权限等!

 

       3.1.4FSDataInputStream

输入流对象! 可以将HDFS中的文件或者文件夹读取到本地!

 

     3.1.5FSDataOutputStream

输出流对象! 可以将本地的文件或者文件夹上传到HDFS中!

 

     3.1.6构建工程

指定工程路径

框架结构

 

导入依赖节点

 

org.apache.hadoop
hadoop-common
2.8.0
org.apache.hadoop
hadoop-hdfs
2.8.0
org.apache.hadoop
hadoop-client
2.8.0

 

查询文件信息

 

// 查询文件信息    private static void catFile() throws IOException, FileNotFoundException {        // TODO Auto-generated method stub        // 指定集群当中主机的IP+端口        String uri = "hdfs://192.168.77.99:9000";        // 加载Hadoop的配置文件        Configuration con = new Configuration();        // 创建一个可以操作的HDFS对象        FileSystem fileSystem = FileSystem.get(URI.create(uri), con);        FileStatus[] listStatus = fileSystem.listStatus(new Path("/input/"));        for (FileStatus fileStatus : listStatus) {            /**             * 快捷键Syso             */            System.out.print("file文件信息------》" + fileStatus);        }        /**         * 获取单个文件         */        /**         * FileStatus fileStatus = fileSystem.getFileStatus(new Path(         * "input/file1.txt")); System.out.println(fileStatus);         */    }

 

查询文件内容

public static void getFile(String fileName) throws Exception {        // 指定集群中的主机IP+端口号        String uri = "hdfs://192.168.77.99:9000";        // 加载Hadoop的配置文件        Configuration con = new Configuration();        // 创建一个可以操作的HDFS对象        FileSystem fileSystem = FileSystem.get(URI.create(uri), con);        // 获取指定的文件        FSDataInputStream open = fileSystem.open(new Path(fileName));        // 将文件的内容装载到BufferedReader对象中去        BufferedReader reader = new BufferedReader(new InputStreamReader(open));        String line = "";        // 循环读取文件内容        while ((line = reader.readLine()) != null) {            System.out.println(line);        }        // 关闭资源        reader.close();        open.close();        fileSystem.close();    }

创建一个空的文件

public static void createNewFile(String fileName) throws Exception {        // 指定集群中的主机IP+端口号        String uri = "hdfs://192.168.77.99:9000";        // 加载Hadoop的配置文件        Configuration con = new Configuration();        // 创建一个可以操作的HDFS对象        FileSystem fileSystem = FileSystem.get(URI.create(uri), con);        if (fileSystem.exists(new Path(fileName))) {            System.out.println("文件已经存在");        } else {            boolean createNewFile = fileSystem                    .createNewFile(new Path(fileName));            if (createNewFile) {                System.out.println("成功");            } else {                System.out.println("失败");            }        }        fileSystem.close();    }

在新创建的文件夹写入内容

public static void createFile(String fileName) throws Exception {        // 指定集群中的主机IP+端口号        String uri = "hdfs://192.168.77.99:9000";        // 加载Hadoop的配置文件        Configuration con = new Configuration();        // 创建一个可以操作的HDFS对象        FileSystem fileSystem = FileSystem.get(URI.create(uri), con);        if (fileSystem.exists(new Path(fileName))) {            System.out.println("文件已经存在");        } else {            FSDataOutputStream create = fileSystem.create(new Path(fileName));            String str = "老黑今天又黑了";            create.write(str.getBytes());            create.flush();            create.close();        }        fileSystem.close();    }

 创建文件夹

public static void mkdirFile(String fileName) throws Exception {        // 指定集群中的主机IP+端口号        String uri = "hdfs://192.168.77.99:9000";        // 加载Hadoop的配置文件        Configuration con = new Configuration();        // 创建一个可以操作的HDFS对象        FileSystem fileSystem = FileSystem.get(URI.create(uri), con);        boolean mkdirs = fileSystem.mkdirs(new Path(fileName));        if (mkdirs) {            System.out.println("成功!");        } else {            System.out.println("失败!");        }        fileSystem.close();    }

重名文件的名字

public static void renameFile(String ordername, String newname)            throws Exception {        // 指定集群中的主机IP+端口号        String uri = "hdfs://192.168.77.99:9000";        // 加载Hadoop的配置文件        Configuration con = new Configuration();        // 创建一个可以操作的HDFS对象        FileSystem fileSystem = FileSystem.get(URI.create(uri), con);        boolean rename = fileSystem.rename(new Path(ordername), new Path(                newname));        if (rename) {            System.out.println("成功!");        } else {            System.out.println("失败!");        }        fileSystem.close();    }

删除文件夹

@SuppressWarnings("deprecation")    public static void deleteFile(String filename) throws Exception {        // 指定集群中的主机IP+端口号        String uri = "hdfs://192.168.77.99:9000";        // 加载Hadoop的配置文件        Configuration con = new Configuration();        // 创建一个可以操作的HDFS对象        FileSystem fileSystem = FileSystem.get(URI.create(uri), con);        boolean delete = fileSystem.delete(new Path(filename));        if (delete) {            System.out.println("成功!");        } else {            System.out.println("失败!");        }        fileSystem.close();    }

上传文件 Windows上传到HDFS上

public static void fromFile(String localName, String fromName)            throws Exception {        // 指定集群中的主机IP+端口号        String uri = "hdfs://192.168.77.99:9000";        // 加载Hadoop的配置文件        Configuration con = new Configuration();        // 创建一个可以操作的HDFS对象        FileSystem fileSystem = FileSystem.get(URI.create(uri), con);        fileSystem.copyFromLocalFile(new Path(localName), new Path(fromName));        fileSystem.close();    }

从hdfs下载到Windows

public static void downLoad(String defsFile,String localFile) throws Exception {        String uri = "hdfs://192.168.77.99:9000";        // 加载hadoop配置文件        Configuration con = new Configuration();        // 创建一个可以操作HDFS对象        FileSystem fileSystem = FileSystem.get(URI.create(uri), con);         fileSystem.copyToLocalFile(new Path(defsFile),new Path (localFile));        fileSystem.close();            }

注意:要记得调用方法

完整的代码结构--各种操作的方法

package com.hdfs;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStreamReader;import java.net.URI;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FSDataInputStream;import org.apache.hadoop.fs.FSDataOutputStream;import org.apache.hadoop.fs.FileStatus;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;public class HDFS {    public static void main(String[] args) throws Exception {        // catFile();        // getFile("/input/file1.txt");// 查询单个文件信息        // createNewFile("/input/file3.txt");        // createFile("/input/file4.txt");        // mkdirFile("/MKDIRS");//创建文件夹        // renameFile("/input/file5.txt","/input/file4.txt");//重命名文件的名字        //deleteFile("/MKDIRS");// 删除文件夹        //fromFile("C:\\Users\\70424\\Desktop\\1.txt","/input");//上传文件Windows到HDFS上        downLoad("/input/file1.txt","C:\\Users\\70424\\Desktop");//从hdfs下载到Windows    }    // 查询文件信息    private static void catFile() throws IOException, FileNotFoundException {        // TODO Auto-generated method stub        // 指定集群当中主机的IP+端口        String uri = "hdfs://192.168.77.99:9000";        // 加载Hadoop的配置文件        Configuration con = new Configuration();        // 创建一个可以操作的HDFS对象        FileSystem fileSystem = FileSystem.get(URI.create(uri), con);        FileStatus[] listStatus = fileSystem.listStatus(new Path("/input/"));        for (FileStatus fileStatus : listStatus) {            /**             * 快捷键Syso             */            System.out.print("file文件信息------》" + fileStatus);        }        /**         * 获取单个文件         */        /**         * FileStatus fileStatus = fileSystem.getFileStatus(new Path(         * "input/file1.txt")); System.out.println(fileStatus);         */    }    // 查询文件内容    public static void getFile(String fileName) throws Exception {        // 指定集群中的主机IP+端口号        String uri = "hdfs://192.168.77.99:9000";        // 加载Hadoop的配置文件        Configuration con = new Configuration();        // 创建一个可以操作的HDFS对象        FileSystem fileSystem = FileSystem.get(URI.create(uri), con);        // 获取指定的文件        FSDataInputStream open = fileSystem.open(new Path(fileName));        // 将文件的内容装载到BufferedReader对象中去        BufferedReader reader = new BufferedReader(new InputStreamReader(open));        String line = "";        // 循环读取文件内容        while ((line = reader.readLine()) != null) {            System.out.println(line);        }        // 关闭资源        reader.close();        open.close();        fileSystem.close();    }    // 创建一个空的文件    public static void createNewFile(String fileName) throws Exception {        // 指定集群中的主机IP+端口号        String uri = "hdfs://192.168.77.99:9000";        // 加载Hadoop的配置文件        Configuration con = new Configuration();        // 创建一个可以操作的HDFS对象        FileSystem fileSystem = FileSystem.get(URI.create(uri), con);        if (fileSystem.exists(new Path(fileName))) {            System.out.println("文件已经存在");        } else {            boolean createNewFile = fileSystem                    .createNewFile(new Path(fileName));            if (createNewFile) {                System.out.println("成功");            } else {                System.out.println("失败");            }        }        fileSystem.close();    }    // 在新创建的文件夹写入内容    public static void createFile(String fileName) throws Exception {        // 指定集群中的主机IP+端口号        String uri = "hdfs://192.168.77.99:9000";        // 加载Hadoop的配置文件        Configuration con = new Configuration();        // 创建一个可以操作的HDFS对象        FileSystem fileSystem = FileSystem.get(URI.create(uri), con);        if (fileSystem.exists(new Path(fileName))) {            System.out.println("文件已经存在");        } else {            FSDataOutputStream create = fileSystem.create(new Path(fileName));            String str = "老黑今天又黑了";            create.write(str.getBytes());            create.flush();            create.close();        }        fileSystem.close();    }    // 创建文件夹    public static void mkdirFile(String fileName) throws Exception {        // 指定集群中的主机IP+端口号        String uri = "hdfs://192.168.77.99:9000";        // 加载Hadoop的配置文件        Configuration con = new Configuration();        // 创建一个可以操作的HDFS对象        FileSystem fileSystem = FileSystem.get(URI.create(uri), con);        boolean mkdirs = fileSystem.mkdirs(new Path(fileName));        if (mkdirs) {            System.out.println("成功!");        } else {            System.out.println("失败!");        }        fileSystem.close();    }    // 重名文件的名字    public static void renameFile(String ordername, String newname)            throws Exception {        // 指定集群中的主机IP+端口号        String uri = "hdfs://192.168.77.99:9000";        // 加载Hadoop的配置文件        Configuration con = new Configuration();        // 创建一个可以操作的HDFS对象        FileSystem fileSystem = FileSystem.get(URI.create(uri), con);        boolean rename = fileSystem.rename(new Path(ordername), new Path(                newname));        if (rename) {            System.out.println("成功!");        } else {            System.out.println("失败!");        }        fileSystem.close();    }    // 删除文件夹    @SuppressWarnings("deprecation")    public static void deleteFile(String filename) throws Exception {        // 指定集群中的主机IP+端口号        String uri = "hdfs://192.168.77.99:9000";        // 加载Hadoop的配置文件        Configuration con = new Configuration();        // 创建一个可以操作的HDFS对象        FileSystem fileSystem = FileSystem.get(URI.create(uri), con);        boolean delete = fileSystem.delete(new Path(filename));        if (delete) {            System.out.println("成功!");        } else {            System.out.println("失败!");        }        fileSystem.close();    }    // 上传文件 Windows上传到HDFS上    public static void fromFile(String localName, String fromName)            throws Exception {        // 指定集群中的主机IP+端口号        String uri = "hdfs://192.168.77.99:9000";        // 加载Hadoop的配置文件        Configuration con = new Configuration();        // 创建一个可以操作的HDFS对象        FileSystem fileSystem = FileSystem.get(URI.create(uri), con);        fileSystem.copyFromLocalFile(new Path(localName), new Path(fromName));        fileSystem.close();    }    //从hdfs下载到Windows    public static void downLoad(String defsFile,String localFile) throws Exception {        String uri = "hdfs://192.168.77.99:9000";        // 加载hadoop配置文件        Configuration con = new Configuration();        // 创建一个可以操作HDFS对象        FileSystem fileSystem = FileSystem.get(URI.create(uri), con);         fileSystem.copyToLocalFile(new Path(defsFile),new Path (localFile));        fileSystem.close();            }}
View Code

 

转载于:https://www.cnblogs.com/3020815dzq/p/10123120.html

你可能感兴趣的文章
python14期2017.7.10
查看>>
servlet的运行机制,转发和重定向
查看>>
discuz首页设置默认地址不带forum.php后缀的方法
查看>>
Winform 串口通讯之读卡器
查看>>
解决手机网页字段过长导致页面混乱
查看>>
extjs使用笔记-21
查看>>
Android中的布局(layout)
查看>>
第 5 章 Nova - 024 - Nova 组件如何协同工作
查看>>
python制作爬虫并将抓取结果保存到excel中
查看>>
快速手工实现软件著作权源码60页制作
查看>>
Super Pi-计算10000位圆周率需要多少时间?
查看>>
第九周项目1-复数类中的运算符重载(续)
查看>>
PHP实现url参数组合字符串与数组相互转换
查看>>
Java Web整合开发(78) -- Struts 1
查看>>
JavaEE(1) - Weblogic 服务器管理的数据源
查看>>
CSharp设计模式读书笔记(14):职责链模式(学习难度:★★★☆☆,使用频率:★★☆☆☆)...
查看>>
【玩转开源】BananaPi R2 —— 第二篇 Openwrt 网口配置分析
查看>>
Git 常用命令
查看>>
前端学习之jquery
查看>>
配置caffe matlab 中遇到的坑
查看>>