HDFS 之Java API归纳
1. HDFS访问方式
- HDFS和文件IO、mysql、yarn、zookeeper一样,都同时提供了命令行客户端,java api访问方式
- mysql、文件IO还有图形化客户端,navicat,图形化操作系统进行文件创建,删除等操作。
2. HDFS的java api汇总
2.1 封装获取FileSystem对象的代码
private static FileSystem getFileSystem() throws URISyntaxException, IOException, InterruptedException {
URI uri = new URI("hdfs://192.168.133.4:8020");
// 上传 注意这个configuration类中可以设置各种配置信息,当然配置信息也可以放在项目的配置文件中,如果都不处理,则可以在软件安装包的配置文件中设置。配置信息生效按照就近原则,优先使用代码中,然后是项目中,然后才是软件安装目录中的配置信息
Configuration entries = new Configuration();
FileSystem root = FileSystem.newInstance(uri, entries, "root");
return root;
}
2.2 创建文件夹和文件
private static void fileCreate(){
try {
FileSystem fileSystem = FsUtil.getFileSystem();
// 创建
Path path = new Path("/datas/xixi/haha");
// 创建文件--注意这时候文件中还没有数据,需要以文件流方式写入数据
boolean newFile = fileSystem.createNewFile(new Path("/datas/yamiyami"));
boolean mkdirs = fileSystem.mkdirs(path);
fileSystem.close();
} catch (Exception e) {
e.printStackTrace();
}
}
2.3 展示hdfs中信息
private static void fileInfos() {
try {
FileSystem fileSystem = FsUtil.getFileSystem();
Path path = new Path("/");
// 递归便利文件,拿到文件对象
RemoteIterator<LocatedFileStatus> iterator = fileSystem.listFiles(path, true);
// 迭代器,遍历
while (iterator.hasNext()) {
LocatedFileStatus next = iterator.next();
boolean directory = next.isDirectory();
long blockSize = next.getBlockSize();
String group = next.getGroup();
long len = next.getLen();
long modificationTime = next.getModificationTime();
String owner = next.getOwner();
System.out.println("文件信息:" + next.toString());
// 拿到每个文件的块对象
BlockLocation[] blockLocations = next.getBlockLocations();
for (BlockLocation blockLocation : blockLocations) {
// 遍历打印信息
String[] hosts = blockLocation.getHosts();
long length = blockLocation.getLength();
String[] names = blockLocation.getNames();
long offset = blockLocation.getOffset();
String s = blockLocation.toString();
String[] storageIds = blockLocation.getStorageIds();
StorageType[] storageTypes = blockLocation.getStorageTypes();
System.out.println("块信息:" + s);
}
}
fileSystem.close();
} catch (Exception e) {
e.printStackTrace();
}
}
2.4删除文件
private static void fileDelete() {
try {
FileSystem fileSystem = FsUtil.getFileSystem();
Path path = new Path("/datas/xixi");
// 判断是否存在
if(fileSystem.exists(path)) {
// 判断是否文件夹
FileStatus fileStatus = fileSystem.getFileStatus(path);
if(fileStatus.isDirectory()){
fileSystem.delete(path, true);
}
}
fileSystem.close();
} catch (Exception e) {
e.printStackTrace();
}
}
2.5 文件移动或者重命名
private static void fileMoveOrRename() {
try {
FileSystem fileSystem = FsUtil.getFileSystem();
fileSystem.rename(new Path("/datas/xixi.jar"), new Path("/datas2/woshishuaige.jar"));
fileSystem.close();
} catch (Exception e) {
e.printStackTrace();
}
}
2.6 文件状态和信息查看
private static void fileStatus() {
try {
FileSystem fileSystem = FsUtil.getFileSystem();
Path path = new Path("/");
org.apache.hadoop.fs.FileStatus[] fileStatuses = fileSystem.listStatus(path);
for (org.apache.hadoop.fs.FileStatus fileStatus : fileStatuses) {
boolean directory = fileStatus.isDirectory();
long blockSize = fileStatus.getBlockSize();
String group = fileStatus.getGroup();
long len = fileStatus.getLen();
long modificationTime = fileStatus.getModificationTime();
String owner = fileStatus.getOwner();
String s = fileStatus.toString();
long accessTime = fileStatus.getAccessTime();
Path path1 = fileStatus.getPath();
short replication = fileStatus.getReplication();
FsPermission permission = fileStatus.getPermission();
boolean file = fileStatus.isFile();
System.out.println("fileStatus:" + s);
}
} catch (Exception e) {
e.printStackTrace();
}
}
2.7文件上传
private static void fileUpload() {
try {
FileSystem fileSystem = FsUtil.getFileSystem();
fileSystem.copyFromLocalFile(new Path("F:\\软件包\\CentOS-7-x86_64-DVD-2003.iso"), new Path("/datas2"));
fileSystem.close();
} catch (Exception e) {
e.printStackTrace();
}
}
2.8文件写入–数据流形式
private static void fileWrite() {
try {
FileSystem fileSystem = FsUtil.getFileSystem();
Path path = new Path("/datas/qingshu2.json");
// 不存在就创建
if(!fileSystem.exists(path)) {
boolean newFile = fileSystem.createNewFile(path);
}
FSDataOutputStream append = fileSystem.append(path);
append.write("你是风儿我是沙,活在一起是泥巴".getBytes());
append.flush();
append.close();
fileSystem.close();
} catch (Exception e) {
e.printStackTrace();
}
}
==不管是java 文件操作、mysql、hdfs、yarn、zookeeper,代码套路都类似,建立连接,进行数据操作,关闭连接。代码套路很相似,包括API的命名,功能设计都很相似,创建,删除,查看,更新,读写IO流,关闭连接等等 ==