首页 技术 正文
技术 2022年11月21日
0 收藏 315 点赞 2,850 浏览 3323 个字
package cn.itcast.hdfs;import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Before;
import org.junit.Test;import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;public class HdfsClient {
FileSystem fs = null; @Before
public void init() throws Exception {
// 构造一个配置参数对象,设置一个参数:我们要访问的hdfs的URI
// 从而FileSystem.get()方法就知道应该是去构造一个访问hdfs文件系统的客户端,以及hdfs的访问地址
// new Configuration();的时候,它就会去加载jar包中的hdfs-default.xml
// 然后再加载classpath下的hdfs-site.xml
// conf.set("fs.defaultFS", "hdfs://mini1:9000");
/**
* 参数优先级: 1、客户端代码中设置的值 2、classpath下的用户自定义配置文件 3、然后是服务器的默认配置
*/
/*conf.set("dfs.replication", "2");
conf.set("dfs.block.size", "64m");*/ // 获取一个hdfs的访问客户端,根据参数,这个实例应该是DistributedFileSystem的实例
// fs = FileSystem.get(conf); // 如果这样去获取,那conf里面就可以不要配"fs.defaultFS"参数,而且,这个客户端的身份标识已经是root用户
Configuration conf = new Configuration();
fs = FileSystem.get(new URI("hdfs://mini1:9000"), conf, "root");
} /**
* 往hdfs上传文件
*/ @Test
public void testAddFileToHdfs() throws Exception {
//要上传的文件所在的本地路径
//要上传到hdfs的目标路径*/
Path src = new Path("e:/hello1.txt");
Path dst = new Path("/");
fs.copyFromLocalFile(src, dst);
fs.close(); }
/**
* 从hdfs中复制文件到本地文件系统
*
* @throws IOException
* @throws IllegalArgumentException
*/
@Test
public void testDownloadFileToLocal() throws IllegalArgumentException, IOException { fs.copyToLocalFile(false, new Path("/hello1.txt"), new Path("e:/"), true);
fs.close(); } /**
* 通过流的形式从hdfs下载数据
* @throws Exception
*/
@SuppressWarnings("resource")
@Test
public void testDownloadFileToLocal2() throws Exception { FSDataInputStream in = fs.open(new Path("/hello1.txt")); FileOutputStream out = new FileOutputStream(new File("e:/1.txt")); IOUtils.copy(in, out); fs.close();
}
/**
* 目录操作
*
* @throws IllegalArgumentException
* @throws IOException
*/
@Test
public void testMkdirAndDeleteAndRename() throws IllegalArgumentException, IOException { // 创建目录
// fs.mkdirs(new Path("/nihao/henhao/yeah"));// // 删除文件夹 ,如果是非空文件夹,参数2必须给值true
// fs.delete(new Path("/nihao/henhao"), true);
//
// // 重命名文件或文件夹
fs.rename(new Path("/nihao"), new Path("/ni")); }
/**
* 查看目录信息,只显示文件
*
* @throws IOException
* @throws IllegalArgumentException
* @throws FileNotFoundException
*/
@Test
public void testListFiles() throws FileNotFoundException, IllegalArgumentException, IOException { // 思考:为什么返回迭代器,而不是List之类的容器
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.getBlockSize());
System.out.println(fileStatus.getPermission());
System.out.println(fileStatus.getLen());
BlockLocation[] blockLocations = fileStatus.getBlockLocations();
for (BlockLocation bl : blockLocations) {
System.out.println("block-length:" + bl.getLength() + "--" + "block-offset:" + bl.getOffset());
String[] hosts = bl.getHosts();
for (String host : hosts) {
System.out.println(host);
} } System.out.println("--------------为allen打印的分割线--------------"); } } /**
* 查看文件及文件夹信息
*
* @throws IOException
* @throws IllegalArgumentException
* @throws FileNotFoundException
*/
@Test
public void testListAll() throws FileNotFoundException, IllegalArgumentException, IOException { FileStatus[] listStatus = fs.listStatus(new Path("/")); String flag = "";
for (FileStatus fstatus : listStatus) { if (fstatus.isFile()) {
flag = "f-- ";
} else {
flag = "d-- ";
}
System.out.println(flag + fstatus.getPath().getName());
System.out.println(fstatus.getPermission()); } }
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,892