一、pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>yofc</groupId>
<artifactId>root</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<!-- 指定jdk -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

二、测试

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.log4j.BasicConfigurator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import java.io.IOException;
import java.net.URI; public class HDFSClient { private Configuration conf;
private FileSystem fs; @Before
public void init() throws Exception {
// 设置 HADOOP_HOME 环境变量
System.setProperty("hadoop.home.dir", "D:/DevelopTools/hadoop-2.9.2/");
// 日志初始化
BasicConfigurator.configure(); conf = new Configuration();
//conf.set("fs.defaultFS", "hdfs://192.168.8.136:9000");
//fs = FileSystem.get(conf ); // 获取 hdfs 客户端对象,指定用户名,避免无权限
fs = FileSystem.get(new URI("hdfs://192.168.8.136:9000"), conf, "root");
} @After
public void close() throws IOException {
fs.close();
} // 在 hdfs 上创建文件夹
@Test
public void mkdirs() throws IOException {
fs.mkdirs(new Path("/10086/"));
}
}

文件上传

@Test
public void testCopyFromLocalFile() throws Exception{
fs.copyFromLocalFile(new Path("D:/MyFile/Downloads/Writage-1.12.msi"), new Path("/Writage-1.12.msi"));
}

手动 IO 流方式

@Test
public void putFileToHDFS() throws Exception{
// 获取输入流
FileInputStream fis = new FileInputStream(new File("D://MyFile/Downloads/Writage-1.12.msi"));
// 获取输出流
FSDataOutputStream fos = fs.create(new Path("/Writage-1.12.msi"));
// 流的对拷
IOUtils.copyBytes(fis, fos, conf);
// 关闭资源
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
}

文件下载

@Test
public void testCopyToLocalFile() throws Exception{
// fs.copyToLocalFile(new Path("/AAA.txt"), new Path("d:/BBB.txt"));
/**
* delSrc:是否删除原数据
* src:hdfs 路径
* dst:本地 路径
* useRawLocalFileSystem:crc 文件完整性校验
*/
fs.copyToLocalFile(false, new Path("/Writage-1.12.msi"), new Path("D://Writage.msi"), true);
}

手动 IO 流方式

@Test
public void getFileFromHDFS() throws Exception{
// 获取输入流
FSDataInputStream fis = fs.open(new Path("/Writage-1.12.msi"));
// 获取输出流
FileOutputStream fos = new FileOutputStream(new File("D://Writage.msi"));
// 流的对拷
IOUtils.copyBytes(fis, fos, conf);
// 关闭资源
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
}

分块方式,这里要下载的文件被 hdfs 切割成了 3 块

// 下载第一块
@Test
public void readFileSeek1() throws Exception{
// 获取输入流
FSDataInputStream fis = fs.open(new Path("/hadoop-2.9.2-win10-64.tar.gz"));
// 获取输出流
FileOutputStream fos = new FileOutputStream(new File("D://hadoop-2.9.2-win10-64.tar.gz"));
// 流的对拷(只拷贝128m)
byte[] buf = new byte[1024];
for (int i = 0; i < 1024 * 128; i++) {
fis.read(buf);
fos.write(buf);
}
// 关闭资源
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
}
// 下载第二块
@Test
public void readFileSeek2() throws Exception{
// 获取输入流
FSDataInputStream fis = fs.open(new Path("/hadoop-2.9.2-win10-64.tar.gz"));
// 设置指定读取的起点
fis.seek(1024*1024*128);
// 获取输出流
FileOutputStream fos = new FileOutputStream(new File("D://hadoop-2.9.2-win10-64.tar.gz2"));
// 流的对拷(只拷贝128m)
byte[] buf = new byte[1024];
for (int i = 0; i < 1024 * 128; i++) {
fis.read(buf);
fos.write(buf);
}
// 关闭资源
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
}
// 下载第三块
@Test
public void readFileSeek3() throws Exception{
// 获取输入流
FSDataInputStream fis = fs.open(new Path("/hadoop-2.9.2-win10-64.tar.gz"));
// 设置指定读取的起点
fis.seek(1024*1024*128*2);
// 获取输出流
FileOutputStream fos = new FileOutputStream(new File("D://hadoop-2.9.2-win10-64.tar.gz3"));
// 流的对拷
IOUtils.copyBytes(fis, fos, conf);
// 关闭资源
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
}

分块下载完毕后合并文件

# Windows 环境下

# 将 hadoop-2.9.2-win10-64.tar.gz2 追加到  hadoop-2.9.2-win10-64.tar.gz
type hadoop-2.9.2-win10-64.tar.gz2 >> hadoop-2.9.2-win10-64.tar.gz
# 将 hadoop-2.9.2-win10-64.tar.gz3 追加到 hadoop-2.9.2-win10-64.tar.gz
type hadoop-2.9.2-win10-64.tar.gz3 >> hadoop-2.9.2-win10-64.tar.gz # 最后 hadoop-2.9.2-win10-64.tar.gz 就是一个完整的文件了

文件删除

@Test
public void testDelete() throws Exception{
/**
* var1:hdfs 路径
* var2:是否递归删除,若为文件夹则必须为 true
*/
fs.delete(new Path("/Writage-1.12.msi"), true);
}

重命名

@Test
public void testRename() throws Exception{
// 把根目录的 10086 改为 mkmk
fs.rename(new Path("/10086/"), new Path("/mkmk/"));
}

查看文件详情

@Test
public void testListFiles() throws Exception{
// 递归获取根目录下的所有文件
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
while(listFiles.hasNext()){
LocatedFileStatus fileStatus = listFiles.next();
// 文件名称
System.out.println(fileStatus.getPath().getName());
// 文件权限
System.out.println(fileStatus.getPermission());
// 文件长度
System.out.println(fileStatus.getLen());
// 文件块信息
BlockLocation[] blockLocations = fileStatus.getBlockLocations();
for (BlockLocation blockLocation : blockLocations) {
// 文件块所在的主机名
String[] hosts = blockLocation.getHosts();
for (String host : hosts) {
System.out.println(host);
}
}
System.out.println("-------------------");
}
}

判断是文件还是文件夹

@Test
public void testListStatus() throws Exception{
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for (FileStatus fileStatus : listStatus) {
if (fileStatus.isFile()) {
System.out.println("文件:"+fileStatus.getPath().getName());
}else{
System.out.println("文件夹:"+fileStatus.getPath().getName());
}
}
}

Windows 运行 Hadoop 问题:https://wiki.apache.org/hadoop/WindowsProblems

HDFS-JavaAPI的更多相关文章

  1. 利用JavaAPI访问HDFS的文件

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  2. HDFS文件系统的JAVA-API操作(一)

    使用java.net.URL访问HDFS文件系统 HDFS的API使用说明: 1.如果要访问HDFS,HDFS客户端必须有一份HDFS的配置文件 也就是hdfs-site.xml,从而读取Nameno ...

  3. 大数据入门第五天——离线计算之hadoop(下)hadoop-shell与HDFS的JavaAPI入门

    一.Hadoop Shell命令 既然有官方文档,那当然先找到官方文档的参考:http://hadoop.apache.org/docs/current/hadoop-project-dist/had ...

  4. HDFS之二:HDFS文件系统JavaAPI接口

    HDFS是存取数据的分布式文件系统,HDFS文件操作常有两种方式,一种是命令行方式,即Hadoop提供了一套与Linux文件命令类似的命令行工具.HDFS操作之一:hdfs命令行操作 另一种是Java ...

  5. HDFS之JAVAAPI

    JAVAAPI 上传小文件 代码

  6. 使用javaAPI操作hdfs

    欢迎到https://github.com/huabingood/everyDayLanguagePractise查看源码. 一.构建环境 在hadoop的安装包中的share目录中有hadoop所有 ...

  7. hadoop的hdfs中的javaAPI操作

    package cn.itcast.bigdata.hdfs; import java.net.URI; import java.util.Iterator; import java.util.Map ...

  8. 三、hdfs的JavaAPI操作

    下文展示Java的API如何操作hdfs,在这之前你需要先安装配置好hdfs https://www.cnblogs.com/lay2017/p/9919905.html 依赖 你需要引入依赖如下 & ...

  9. HDFS的JavaAPI

    配置windows平台的Hadoop环境 在 windows 上做 HDFS 客户端应用开发,需要设置 Hadoop 环境,而且要求是windows 平台编译的 Hadoop,不然会报以下的错误: F ...

  10. hdfs深入:10、hdfs的javaAPI操作

    /** * 递归遍历hdfs中所有的文件路径 */ @Test public void getAllHdfsFilePath() throws URISyntaxException, IOExcept ...

随机推荐

  1. PHP——敏感词过滤

    前言 如果可以用第三方的话,那么你是幸运的,因为现在这种敏感词过滤,敏感图片,敏感语音过滤的第三方服务还是挺多的 敏感词过滤 核心代码 利用PHP内置的三个函数 array_combine() | a ...

  2. 洛谷3707 [SDOI2017] 相关分析 【线段树】

    分析: 化简一下就行了,注意一下平方和公式的运用以及精度的误差. 代码: #include<bits/stdc++.h> using namespace std; ; int n,m; i ...

  3. hdu 5877 Weak Pair (Treap)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5877 题面; Weak Pair Time Limit: 4000/2000 MS (Java/Other ...

  4. 修改KVM的模拟网卡类型

    修改KVM的模拟网卡类型 来源 https://www.cnblogs.com/EasonJim/p/9751051.html 在KVM下可以生成两种型号的网卡,RTL8139和E1000,其实应该是 ...

  5. ecplise properties文件 中文转码

    1.安装插件 2.重开ecplise 3.在项目的乱码文件如jeesite.properties右键 openwith propertiesEditor 就可以看到中文了 输入 proedit 安装完 ...

  6. BZOJ3456 城市规划 【多项式求ln】

    题目链接 BZOJ3456 题解 真是一道经典好题,至此已经写了分治\(NTT\),多项式求逆,多项式求\(ln\)三种写法 我们发现我们要求的是大小为\(n\)无向联通图的数量 而\(n\)个点的无 ...

  7. nginx.conf(centos7 1.14)主配置文件修改

    #nginx1.14 centos7# For more information on configuration, see:# * Official English Documentation: h ...

  8. LNOI2014LCA(树链剖分+离线操作+前缀和)

    题意:给一棵有根树,有多组询问,询问为l r z,求下标为l到r之间的点和z的lca的深度和. 如果我们一个一个求.emmmmm... 考虑答案怎么产生,仔细想一想,如果我们把l到r的所有点到根都加上 ...

  9. Zabbix使用netstat监控会话

    原文链接 TCP的连接状态对于我们web服务器来说是至关重要的,尤其是并发量ESTAB:或者是syn_recv值,假如这个值比较大的话我们可以认为是不是受到了攻击,或是是time_wait值比较高的话 ...

  10. 20165223 实验四 Android开发基础

    实验四 Android开发基础 目录 一.实验报告封面 二.具体实验内容 (一)Android Stuidio的安装测试 (二)Activity测试 (三)UI测试 (四)布局测试 (五)教材代码测试 ...