首页 技术 正文
技术 2022年11月15日
0 收藏 926 点赞 3,474 浏览 2912 个字

  

  不多说,直接上代码。

代码

package zhouls.bigdata.myWholeHadoop.HDFS.hdfs4;

import java.io.IOException;

import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.protocol.DatanodeInfo;

import java.net.URI;

public class HDFStest1
{

/**
* @param args
* @throws IOException
* @throws URISyntaxException
*/
public static void main(String[] args) throws IOException, URISyntaxException
{
// TODO Auto-generated method stub
//mkdir();
//copyToHDFS();
//getFile();
//ListAllFile();
//getFileLocal();
//rmdir();
getHDFSNodes();
}
//获取HDFS文件系统
public static FileSystem getFileSystem() throws IOException,URISyntaxException
{
Configuration conf = new Configuration();//读取配置文件,比如core-site.xml
//FileSystem fs =FileSystem.get(conf);

URI uri = new URI(“hdfs://HadoopMaster:9000”);

FileSystem fs = FileSystem.get(uri,conf);
return fs;
}

public static void mkdir() throws IOException,URISyntaxException
{
//第一步,获取文件系统
FileSystem fs =getFileSystem();
//第二步,创建文件目录
fs.mkdirs(new Path(“/zhouls/data”));
//第三步,释放资源
fs.close();
}

public static void copyToHDFS() throws IOException,URISyntaxException
{
//第一步
FileSystem fs=getFileSystem();
//第二步
Path srcpath=new Path(“D://Data/weibo.txt”);
Path dstpath=new Path(“/zhouls/data”);
//第三步
fs.copyFromLocalFile(srcpath, dstpath);
//第四步
fs.close();
}

public static void getFile() throws IOException, URISyntaxException
{
//第一步
FileSystem fs=getFileSystem();
//第二步
Path srcpath=new Path(“/zhouls/data/weibo.txt”);
Path dstpath=new Path(“D://Data/test”);
//第三步
fs.copyToLocalFile(srcpath, dstpath);
//第四步
fs.close();

}

public static void ListAllFile() throws IOException, URISyntaxException
{
//第一步
FileSystem fs=getFileSystem();
//第二步
FileStatus[] status =fs.listStatus(new Path(“/zhouls”));
//第三步
Path[] listedPaths = FileUtil.stat2Paths(status);
//第四步
for(Path p:listedPaths)
{
System.out.println(p);

}
//第五步
fs.close();
}

public static void getFileLocal() throws IOException, URISyntaxException
{
//第一步
FileSystem fs=getFileSystem();
//第二步
Path path=new Path(“/zhouls/data/weibo.txt”);
//第三步
FileStatus fileStatus=fs.getFileLinkStatus(path);
//第四步
BlockLocation[] blkLocations = fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
//第五步
for(int i=0;i< blkLocations.length;i++)
{
String[] hosts = blkLocations[i].getHosts();
System.out.println(“block_”+i+”_location:”+hosts[0]);
}
//第六步
fs.close();
}

public static void rmdir() throws IOException, URISyntaxException
{
//第一步
FileSystem fs=getFileSystem();
//第二步
fs.delete(new Path(“/zhouls/data”),true);
//第三步
fs.close();
}

public static void getHDFSNodes() throws IOException, URISyntaxException
{
//第一步
FileSystem fs=getFileSystem();
//第二步
DistributedFileSystem hdfs = (DistributedFileSystem)fs;
//第三步
DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats();
//第四步
for(int i=0;i< dataNodeStats.length;i++)
{
System.out.println(“DataNode_”+i+”_Name:”+dataNodeStats[i].getHostName());
}
//第五步
fs.close();
}

}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,022
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,359
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,142
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,773
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,851