HDFS中Java的API使用测试
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.protocol.DatanodeInfo; class TestHDFSFile { // 在hdfs上创建目录
public void CreateFilePath(String hdfspath) throws IOException {
Configuration conf = new Configuration();
FileSystem hdfs = FileSystem.get(conf);
// System.out.println("Upload to " + conf.get("fs.default.name"));
if (hdfs.exists(new Path(hdfspath))) {
System.out.println("文件目录已存在");
} else {
hdfs.mkdirs(new Path(hdfspath));
} // 列出该目录下的所有文件,以便查看
Path parentpath = new Path(hdfspath);
HdfsFileList(parentpath.getParent()); } // 查看某目录下的文件列表
public void HdfsFileList(Path hdfspath) throws IOException {
Configuration conf = new Configuration();
FileSystem hdfs = FileSystem.get(conf);
// System.out.println("Upload to " + conf.get("fs.default.name"));
FileStatus files[] = hdfs.listStatus(hdfspath);
if (files.length == 0) {
System.out.println("该目录下没有任何文件");
} else {
for (FileStatus file : files) {
System.out.println(file.getPath());
}
}
} // 上传本地文件到HDFS
public void UploadFileToHDFS(String localpath, String hdfspath)
throws Exception { Configuration conf = new Configuration();
// conf.addResource(new Path(localPath + "core-site.xml"));
FileSystem hdfs = FileSystem.get(conf);
Path src = new Path(localpath);
Path dst = new Path(hdfspath);
hdfs.copyFromLocalFile(src, dst); // 输出hdfs上目录中的文件列表
HdfsFileList(dst);
} // 创建HDFS文件,并对文件进行些内容
public void CreateFile(String hdfspath) throws Exception {
Configuration conf = new Configuration();
// byte[] buff = "hello world!".getBytes();
Scanner sc = new Scanner(System.in);
System.out.println("请输入一行字符串");
String src = sc.nextLine();// 在控制台获取一行字符串 FileSystem hdfs = FileSystem.get(conf);
Path dst = new Path(hdfspath);
FSDataOutputStream outputStream = null;
try {
outputStream = hdfs.create(dst);
// 将这一行字符串写入文件中
outputStream.writeBytes(src); // write(buff, 0, buff.length);
} catch (Exception e) {
e.printStackTrace(); } finally {
if (outputStream != null) {
outputStream.close();
}
} HdfsFileList(dst.getParent());
} // 重命名HDFS文件 public void RenameFileName(String oldname, String newname) throws Exception { Configuration conf = new Configuration(); FileSystem hdfs = FileSystem.get(conf);
// Path dst = new Path(hdfspath); Path frpath = new Path(oldname);
Path topath = new Path(newname); hdfs.rename(frpath, topath); HdfsFileList(topath.getParent());
} // 刪除HDFS文件或目录
public void DelHDFSFiles(String hdfspath) throws Exception { Configuration conf = new Configuration(); FileSystem hdfs = FileSystem.get(conf);
// Path dst = new Path(hdfspath); Path topath = new Path(hdfspath);
if(!hdfs.exists(topath)){
System.out.println("文件不存在");
return ;
} boolean ok = hdfs.delete(topath, true);
System.out.println(ok ? "删除成功" : "删除失败"); HdfsFileList(topath.getParent());
} // 查看HDFS文件或文件夹里所有文件的最后修改时间
public void GetFileModifyTime(String hdfspath) throws Exception { Configuration conf = new Configuration(); FileSystem hdfs = FileSystem.get(conf);
Path dst = new Path(hdfspath); FileStatus files[] = hdfs.listStatus(dst);
for (FileStatus file : files) {
/*
* System.out.println(file.getPath() + "\t" +
* file.getModificationTime());
*/ System.out.println(file.getPath() + "\t"
+ new Date(file.getModificationTime())); }
} // 查看HDFS文件是否存在
public boolean IsExists(String hdfspath) throws Exception { Configuration conf = new Configuration(); FileSystem hdfs = FileSystem.get(conf);
Path dst = new Path(hdfspath); boolean ok = hdfs.exists(dst);
System.out.println(ok ? "文件存在" : "文件不存在");
return ok;
} // 查看某个文件在HDFS集群的位置
public void FileBlockLocation(String hdfspath) throws Exception { Configuration conf = new Configuration(); FileSystem hdfs = FileSystem.get(conf);
Path dst = new Path(hdfspath); FileStatus fileStatus = hdfs.getFileStatus(dst);
BlockLocation[] blockLocations = hdfs.getFileBlockLocations(fileStatus,
0, fileStatus.getLen());
for (BlockLocation block : blockLocations) {
System.out.println(Arrays.toString(block.getHosts()) + "\t"
+ Arrays.toString(block.getNames()) + "\t"
+ block.getLength());
}
} // 获取HDFS集群上所有节点名称
public void GetHostName() throws Exception { Configuration conf = new Configuration(); DistributedFileSystem hdfs = (DistributedFileSystem) FileSystem
.get(conf);
DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats(); for (DatanodeInfo dataNode : dataNodeStats) {
System.out.println(dataNode.getHostName() + "\t"
+ dataNode.getName());
}
}
}
HDFS中Java的API使用测试的更多相关文章
- HDFS中JAVA API的使用
HDFS中JAVA API的使用 HDFS是一个分布式文件系统,既然是文件系统,就可以对其文件进行操作,比如说新建文件.删除文件.读取文件内容等操作.下面记录一下使用JAVA API对HDFS中的 ...
- 大数据(5) - HDFS中的常用API操作
一.安装java 二.IntelliJ IDEA(2018)安装和破解与初期配置 参考链接 1.进入官网下载IntelliJ IDEA https://www.jetbrains.com/idea/d ...
- [转]HDFS中JAVA API的使用
HDFS是一个分布式文件系统,既然是文件系统,就可以对其文件进行操作,比如说新建文件.删除文件.读取文件内容等操作.下面记录一下使用JAVA API对HDFS中的文件进行操作的过程. 对分HDFS中的 ...
- hadoop系列二:HDFS文件系统的命令及JAVA客户端API
转载请在页首明显处注明作者与出处 一:说明 此为大数据系列的一些博文,有空的话会陆续更新,包含大数据的一些内容,如hadoop,spark,storm,机器学习等. 当前使用的hadoop版本为2.6 ...
- Hadoop(五):HDFS的JAVA API基本操作
HDFS的JAVA API操作 HDFS在生产应用中主要是客户端的开发,其核心步骤是从HDFS提供的api中构造一个HDFS的访问客户端对象,然后通过该客户端对象操作(增删改查)HDFS上的文件. 主 ...
- 使用HDFS客户端java api读取hadoop集群上的信息
本文介绍使用hdfs java api的配置方法. 1.先解决依赖,pom <dependency> <groupId>org.apache.hadoop</groupI ...
- HDFS的Java API
HDFS Java API 可以用于任何Java程序与HDFS交互,该API使我们能够从其他Java程序中利用到存储在HDFS中的数据,也能够使用其他非Hadoop的计算框架处理该数据 为了以编程方式 ...
- Hadoop之HDFS(三)HDFS的JAVA API操作
HDFS的JAVA API操作 HDFS 在生产应用中主要是客户端的开发,其核心步骤是从 HDFS 提供的 api中构造一个 HDFS 的访问客户端对象,然后通过该客户端对象操作(增删改查)HDFS ...
- Java日期时间API系列13-----Jdk8中java.time包中的新的日期时间API类,时间类转换,Date转LocalDateTime,LocalDateTime转Date等
从前面的系列博客中可以看出Jdk8中java.time包中的新的日期时间API类设计的很好,但Date由于使用仍非常广泛,这就涉及到Date转LocalDateTime,LocalDateTime转D ...
随机推荐
- 【转】Struts1.x系列教程(4):标签库概述与安装
转载地址:http://www.blogjava.net/nokiaguy/archive/2009/01/archive/2009/01/archive/2009/01/archive/2009/0 ...
- FusionCharts或其它flash的div图层总是浮在最上层的问题
div的图层由div的style中的z-index来决定,z-index是层垂直屏幕的坐标,0最小,越大的话位置越靠上. 由于FusionCharts的图表都放在div中,如果页面还有其他的div,将 ...
- YTU 2345: 后序遍历二叉树
原文链接:https://www.dreamwings.cn/ytu2345/2611.html 2345: 后序遍历二叉树 时间限制: 1 Sec 内存限制: 128 MB 提交: 3 解决: ...
- linux初始化配置-----网络配置
一.设置linux网络 1)零时设置ip地址 由于centos7默认没有ifconfig命令所以为了使用方便我们先安装net-tool使我们能使用ifconfig命令查看ip地址 ·挂载系统光盘 [r ...
- Shader的自定义特性使用
使用自定义特性关键字,可以动态对Shader某一部分代码进行开关操作 shader(定义了KEYWORD1特性): 定义:#pragma shader_feature KEYWORD1 判断:#ifd ...
- JAVA基础知识之JDBC——离线RowSet
离线RowSet 如果直接使用ResultSet, 程序在得到ResultSet记录之后需要立即使用,否则一旦关闭Connection就不再可用,要解决这种情况要么将ResultSet的结果转换成Ja ...
- Xcode7中,如何新建category分类
易忘,所以留存: 1, 2, 3, 结果如下: 补充: http://tech.meituan.com/DiveIntoCategory.html
- HDU 5795 A Simple Nim(简单Nim)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- ubunt tmux X Error of failed request
X Error of failed request: BadName (named color or font does not exist) Major opcode of failed req ...
- Android NDK开发入门实例
AndroidNDK是能使Android应用开发者把从c/c++编译而来的本地代码嵌入到应用包中的一系列工具的组合. 注意: AndroidNDK只能用于Android1.5及以上版本中. I. An ...