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 ...
随机推荐
- Dynamics AX 2012 R2 创建一个带有负载均衡的服务器集群
安装额外AOS的主要目的,是将它添加到集群,或用于创建批处理服务器. 1.创建集群服务器 这里,Reinhard使用上节Install An Additional AOS 中创建的AOS,来创建集群. ...
- Linux中执行shell脚本的4种方法
bash shell 脚本的方法有多种,现在作个小结.假设我们编写好的shell脚本的文件名为hello.sh,文件位置在/data/shell目录中并已有执行权限. 方法一:切换到shell脚本所在 ...
- Unity3D Function Not Supported - Unity动画事件提示不支持的解决方法
把脚本放在所有组件的最上面 然后刷新一下就好了 如果还是显示不支持就是参数问题.比如不支持bool参数,但支持string参数. 所以最好用无参数的
- keytool的使用
1. 创建数字证书 keytool -genkey -v -alias scent -dname "CN=John,OU=MNG,O=Corp,L=Hangzhou,ST=Zhejiang, ...
- JavaScript高级应用(一)
1.尺寸 //各种尺寸 s += "\r\n网页可见区域宽(document.body.clientWidth):"+ document.body.clientWidth; s + ...
- 解决 .so文件64与32不兼容问题
http://blog.csdn.net/vhawk/article/details/49964475 android64位机子兼容32位.so库文件 2016-11-25 19:39 5416人阅读 ...
- Codeforces Round #339 (Div.2)
A. Link/Cut Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- wampserver You don't have permission to access / on this server. 解决 方法(转,正好碰到这样的事情了就转下来)
最近在安装最近版wampserver 2.2 d时发现安装好后启动服务器,访问localhost显示You don't have permission to access / on this serv ...
- nodejs系列(一)安装和介绍
一.安装nodejs http://www.nodejs.org/download/.进入release/选择想要安装的文件,win下安装选择mis和exe的比较方便,安装完毕重新打开cmd命令行,p ...
- Integer
import static java.lang.System.*; public class IntegerTestOne{ public static void main(String []args ...