******HDFS应用开发篇******
HDFS的java操作
1 搭建开发环境
1.1引入依赖
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.4</version>
</dependency>
注:如需手动引入jar包,请引入hadoop安装目录的下的share文件下的common、hdfs 等jar包
1.2 博主使用的是IDEA编辑器
1)打开编辑器 点击新建java项目
包名: com.lyz.bigdata.hdfs
类名: HdfsClientDemo
2)然后引入jar包 jar包引入方式: 直接Ctrl+Alt+Shift+S 打开
3)点击Modules
4) 点击Dependencies 依赖
5)点击“+”号 选择你要上传的jar包 将上面share目录下的common和hdfs jar导进去就可以了
1.3 本地环境配置
1)在此电脑-属性-高级系统设置-环境变量中添加 Hadoop 的配置
2)path中添加
1.4 编写代码
package com.lyz.bigdata.hdfs;/**
* @Author:759057893@qq.com Lyz
* @Date: ${Date} 15:49
* @Description:
**/
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
import java.util.Iterator;
import java.util.Map;
/**
*@Author:759057893@qq.com Lyz
*@Date: 2019/1/14 15:49
*@Description:
**/
public class HdfsClientDemo {
FileSystem fs =null ;
Configuration conf = null;
@Before
public void init() throws Exception {
conf = new Configuration();
//fs.defaultFS 是在虚拟机上寻找主机地址和端口号, hdfs://mini1:9000 博主的主机名是mini1 端口号一般对应的都是9000
conf.set("fs.defaultFS", "hdfs://mini1:9000");
// 拿到一个文件系统操作的客户端实例对象
//fs = FileSystem.get(conf);
//可以直接传入URI和用户身份
fs = FileSystem.get(new URI("hdfs://mini1:9000"),conf,"hadoop");
}
/**
* @function: 上传
* @author: 759057893@qq.com lyz
* @date: 2019/1/18 11:09
*/
@Test
public void testUpload() throws Exception {
// 第一个路径指的是原路径 指window中你想要上传的文件 第二个路径指的是虚拟机上linux的根路径
fs.copyFromLocalFile(new Path("c:/hsrv.txt"),new Path("/hsrv123.txt"));
fs.close();
}
/**
* @function: 下载
* @author: 759057893@qq.com lyz
* @date: 2019/1/18 11:09
*/
@Test
public void testDownload() throws Exception {
fs.copyToLocalFile(new Path("/hsrv123.txt"),new Path("f:/hadoop-APIcode/"));
fs.close();
}
/**
* @function: 打印参数
* @function: 显示数据
* @author: 759057893@qq.com lyz
* @date: 2019/1/18 11:33
*/
@Test
public void testConf() throws Exception{
Iterator<Map.Entry<String,String>> it =conf.iterator();
while(it.hasNext()){
Map.Entry<String,String> ent = it.next();
System.out.println(ent.getKey()+ ";"+ent.getValue());
}
}
/**
* @function:
* @function: 创建文件
* @author: 759057893@qq.com lyz
* @date: 2019/1/18 11:33
*/
@Test
public void mkdir() throws Exception{
boolean mkdir = fs.mkdirs(new Path("/testMkdir/a/bbb"));
System.out.println(mkdir);
}
/**
* @function:
* @function: 删除文件
* @author: 759057893@qq.com lyz
* @date: 2019/1/18 11:33
*/
@Test
public void testDelete() throws Exception{
boolean flag = fs.delete(new Path("/tmp"),true);
System.out.println(flag);
}
/**
* 递归列出指定目录下所有子文件夹中的文件
* @throws Exception
*/
@Test
public void testLs() throws Exception {
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
while(listFiles.hasNext()){
LocatedFileStatus fileStatus = listFiles.next();
System.out.println("blocksize: " +fileStatus.getBlockSize());
System.out.println("owner: " +fileStatus.getOwner());
System.out.println("Replication: " +fileStatus.getReplication());
System.out.println("Permission: " +fileStatus.getPermission());
System.out.println("Name: " +fileStatus.getPath().getName());
System.out.println("------------------");
BlockLocation[] blockLocations = fileStatus.getBlockLocations();
for(BlockLocation b:blockLocations){
System.out.println("块起始偏移量: " +b.getOffset());
System.out.println("块长度:" + b.getLength());
//块所在的datanode节点
String[] datanodes = b.getHosts();
for(String dn:datanodes){
System.out.println("datanode:" + dn);
}
}
}
}
/**
* @function: 查询存储的是文件还是文件夹
* @author: 759057893@qq.com lyz
* @date: 2019/1/21 14:40
*/
@Test
public void testLs2() throws Exception {
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for(FileStatus file :listStatus){
System.out.println("name: " + file.getPath().getName());
System.out.println((file.isFile()?"file":"directory"));
}
}
public static void main(String[] args) throws Exception {
FileSystem fs =null ;
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://mini1:9000");
// 拿到一个文件系统操作的客户端实例对象
fs = FileSystem.get(conf);
}
}
1.5 运行
点击运行 就可以看到文件已经上传成功