一、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. BZOJ3730震波——动态点分治+线段树(点分树套线段树)

    题目描述 在一片土地上有N个城市,通过N-1条无向边互相连接,形成一棵树的结构,相邻两个城市的距离为1,其中第i个城市的价值为value[i].不幸的是,这片土地常常发生地震,并且随着时代的发展,城市 ...

  2. P1164 小A点菜

    原题链接 https://www.luogu.org/problemnew/show/P1164 此题是一道简单的动规问题 才学两天不是很熟练,我苦思冥想看着题解终于想出来了. 主要的思路如下: 我们 ...

  3. 设置服务器的MySQL允许远程访问/外网访问

    我需要在C++中连接服务器上的MySQL数据库.但是直接连接失败了,原来服务器上还要修改一下MySQL的配置. 一.服务器上的配置mysql数据库 进入mysql: mysql -uroot -p 输 ...

  4. Linux服务器定时健康检查,发生故障自动微信告警

    此脚本适用于于各种Linux环境,可以实现各种监控项目,可自定义阀值,实现不同监控效果已在原有脚本基础上做了简化,提取了主要功能目前实现的有:1.磁盘监控2.内存监控3.cpu负荷监控4.进程数监控5 ...

  5. PKUWC2019 凉凉记

    请配合 BGM 食用. 菜就是菜,说什么都是借口. Day 0 前一天先到纪中报道,高铁上打了一会单机膈膜,然后又打了一遍 \(FFT\) 板子,就到了中山. 到了后,发现气温骤然升高,马上 脱 换裤 ...

  6. Atcoder | AT2665 【Moderate Differences】

    又是一道思路特别清奇的题qwq...(瞪了一上午才发现O(1)的结论...差点还想用O(n)解决) 问题可以转化为是否能够由\(f_{1}=a\)通过\(\pm x \in[c,d]\)得到\(f_{ ...

  7. 构建FTP服务

    一.配置YUM仓库服务--------------YUM服务器------------------client------------------192.168.1.1 192.168.1.10[ro ...

  8. jmeter4.0测试dubbo接口遇到的问题:An error occurred: org.springframework.scheduling.quartz.CronTriggerBean has interface org.quartz.CronTrigger as super class

    半年前,用jmeter4.0测试dubbo接口的时候,遇到这样一个问题 An error occurred: org.springframework.scheduling.quartz.CronTri ...

  9. hdu 3294 Girls' research(manacher)

    Problem Description One day, sailormoon girls are so delighted that they intend to research about pa ...

  10. [BOI2007]Mokia 摩基亚(CDQ分治)

    upd:\((x1,y1)(x2,y2)\)表示以\((x1,y1)\)为左上端点 \((x2,y2)\)为右下端点的矩形 本来以为是一道二位树状数组的模板,但是看数据范围之后就放弃了,边界既然到了2 ...