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构建工程

指定工程路径

框架结构:

导入依赖节点

<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.8.</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.8.</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.8.</version>
</dependency>

查询文件信息

// 查询文件信息
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(); } }

HDFS API编程的更多相关文章

  1. 【HDFS API编程】从本地拷贝文件,从本地拷贝大文件,拷贝HDFS文件到本地

    接着之前继续API操作的学习 CopyFromLocalFile: 顾名思义,从本地文件拷贝 /** * 使用Java API操作HDFS文件系统 * 关键点: * 1)create Configur ...

  2. 【HDFS API编程】查看HDFS文件内容、创建文件并写入内容、更改文件名

    首先,重点重复重复再重复: /** * 使用Java API操作HDFS文件系统 * 关键点: * 1)创建 Configuration * 2)获取 FileSystem * 3)...剩下的就是 ...

  3. 【HDFS API编程】jUnit封装-改写创建文件夹

    首先:什么是jUnit  回顾: https://www.cnblogs.com/Liuyt-61/p/10374732.html 上一节我们知道: /** * 使用Java API操作HDFS文件系 ...

  4. 【HDFS API编程】第一个应用程序的开发-创建文件夹

    /** * 使用Java API操作HDFS文件系统 * 关键点: * 1)创建 Configuration * 2)获取 FileSystem * 3)...剩下的就是 HDFS API的操作了*/ ...

  5. 【HDFS API编程】开发环境搭建

    使用HDFS API的方式来操作HDFS文件系统 IDEA Java 使用Maven来管理项目 先打开IDEA,New Project 创建GAV然后next 默认使用的有idea内置的Maven,可 ...

  6. 【HDFS API编程】查看文件块信息

    现在我们把文件都存在HDFS文件系统之上,现在有一个jdk.zip文件存储在上面,我们想知道这个文件在哪些节点之上?切成了几个块?每个块的大小是怎么样?先上测试类代码: /** * 查看文件块信息 * ...

  7. 【HDFS API编程】查看目标文件夹下的所有文件、递归查看目标文件夹下的所有文件

    使用hadoop命令:hadoop fs -ls /hdfsapi/test  我们能够查看HDFS文件系统/hdfsapi/test目录下的所有文件信息 那么使用代码怎么写呢?直接先上代码:(这之后 ...

  8. 【HDFS API编程】副本系数深度剖析

    上一节我们使用Java API操作HDFS文件系统创建了文件a.txt并写入了hello hadoop(回顾:https://www.cnblogs.com/Liuyt-61/p/10739018.h ...

  9. 干货--安装eclipse-hadoop-plugin插件及HDFS API编程两个遇到的重要错误的解决

    在Windows的eclipse上写hdfs的API程序,都会遇到两个错误,在网上查了很多资料,都没有解决的办法,经过了很多时间的研究,终于把这个问题解决了 错误是 1.java.io.IOExcep ...

随机推荐

  1. ch01 PHP开篇

    ch01 PHP开篇 1.1启蒙知识 思考:WAMP是什么?:集成开发环境 [Windows+Apache服务器+MySQL数据库+PHP编程] 1.1.1 站点 将网站所有相关素材都放到一个文件夹中 ...

  2. caffe-ssd的GPU在make runtest的时候报错:BatchReindexLayerTest/2.TestGradient,where TypeParam=caffe::GPUdevice(<float>)(<double>)

    make runtest报错:BatchReindexLayerTest/2.TestGradient,where TypeParam=caffe::GPUdevice<float> Ba ...

  3. 常用分布随机数生成及JS类函数开发和运用

    (2017-02-15 银河统计) 随机数生成是运用蒙特卡洛或统计随机模拟仿真方法的前提.本文在银河统计Web Service接口基础上,编制JS类函数生成常用分布随机数,为在网页中实现模拟仿真项目提 ...

  4. 生成id

    package com.develop.web.util; import java.security.MessageDigest; import java.text.SimpleDateFormat; ...

  5. 【 记忆网络 2 】 End-to-End Memory Network

    继上一篇:Memory Network 1. 摘要 引入了一个神经网络,在一个可能很大的外部记忆上建立了一个recurrent attention模型. 该体系结构是记忆网络的一种形式,但与该工作中的 ...

  6. Linux 简单文本处理

    1.创建文件加“.”带表隐藏文件 2.password文件内“user:x:501:501::/home/lishiming:/bin/bash”含义:   用户名:密码控位键:UID:GID:用户解 ...

  7. poj1985和poj1849(树的直径)

    题目传送门:poj1985 树是连通无环图,树上任意两点之间的路径是唯一的.定义树上任 意两点u, v的距离为u到v路径上边权的和.树的直径MN为树上最长路 径,即点M和N是树上距离最远的两个点. 题 ...

  8. Java基础学习-注释的概述和分类

    /*     注释:用于解释说明程序的文字          分类:             单行://             多行:/**/       作用:解释说明程序,提高程序的阅读性 */ ...

  9. 多台linux主机间免密码登录

    即在一台主机上登录另一台主机. 有2台linux主机A.B.A输入命令ssh B的ip地址以连接B,发现需要输入B的登录密码,怎样不需要输入密码呢? 步骤1: 在主机A中,输入ssh-keygen - ...

  10. P3829 [SHOI2012]信用卡凸包

    思路 注意到结果就是每个信用卡边上的四个圆心的凸包周长+一个圆的周长 然后就好做了 注意平行时把距离小的排在前面,栈中至少要有1个元素(top>1),凸包中如果存在叉积为0的点也要pop,否则可 ...