1. 手把手带你玩转大数据系列--hdfs API开发以及概念讲解
在昨天的时候,我搭建了一套hdfs的集群,因为太晚了,就只是单纯的将搭建的配置文件跟大家进行了分享,并没有进行其他的讲解,今天呢,就梳理一下hdfs的相关理论知识,最后的时候,我会将今天用来测试hdfs API的代码附上,觉得写的还好的,希望支持一下,觉得什么地方有问题的,也欢迎私信或者在下方评论区大家一起讨论
因为时间原因,今天的理论就用了之前给人培训的时候形成的图片,内容涵盖读写、备份、管道、持久化、安全、高可用等,后期会给形成文档再分享给大家,抱歉,需要这几张图片的,私信我获取
接下来就是重头戏,api代码,原来我只是想单纯的用junit测试一下集群链接有没有问题,但是想到可能会有新人也看这篇文章,就将常用的一些方法给罗列出来了,希望有兴趣的,可以实际动手操作一下
环境:完全分布式集群、idea、maven
注意
1、需要配置本地环境
2、本地用户要和集群的一致,不方便设置的,可以在环境变量添加HADOOP_USER_NAME 值设定为集群的用户名,我的是root
3、idea中创建resource文件夹,并赋予root权限,将core-site.xml和hdfs-site.xml两个集群配置文件导入
在前一博客中我们搭建了hdfs的开发环境,接下来我们要在idea中进行相应的代码开发1、引入依赖包```java<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common --><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-common</artifactId><version>2.6.5</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs --><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-hdfs</artifactId><version>2.6.5</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>compile</scope></dependency>```因为需要进行测试,所以我们使用junit进行单元测试,测试每一个功能模块是否好用接下来我们开始代码的编写,以下代码包括hdfs的增删改查、上传、下载以及block信息查询等功能```javapackage com.msb.hdfs;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.*;import org.apache.hadoop.io.IOUtils;import org.junit.After;import org.junit.Before;import org.junit.Test;import java.io.*;import java.net.URI;public class TestHdfs {public Configuration conf = null;public FileSystem fs = null;@Beforepublic void conn() throws IOException, InterruptedException {conf = new Configuration(true);//取环境变量,创建链接//fs = FileSystem.get(conf);//直接连接集群fs = FileSystem.get(URI.create("hdfs://node01:9000/"), conf, "root");}//创建,删除,查询@Testpublic void mkdir() throws IOException {Path dir = new Path("/Test");if (fs.exists(dir)) {fs.delete(dir, true);}//创建目录fs.mkdirs(dir);}//上传文件@Testpublic void upload() throws Exception {//创建输入BufferedInputStream input = new BufferedInputStream(new FileInputStream(new File("./data/hello.txt")));//创建输出Path outfile = new Path("/Test/out.txt");FSDataOutputStream output = fs.create(outfile);IOUtils.copyBytes(input,output,conf,true);}//下载文件@Testpublic void getFileHdfs() throws IOException {//定义文件Hdfs下载的路径Path output = new Path("/Test/out.txt");//定义下载到win中文件路径Path input = new Path("D:/student.txt");//下载fs.copyToLocalFile(false, output, input, true);//关闭fs.close();System.out.println("plus下载成功");}@Testpublic void blocks() throws Exception {Path path = new Path("/Test/out.txt");FileStatus status = fs.getFileStatus(path);BlockLocation[] blks = fs.getFileBlockLocations(status, 0, status.getLen());for (BlockLocation blk:blks) {System.out.println(blk);}}@Afterpublic void close() throws IOException {fs.close();}}
```执行,然后再我们的hdfs集群中通过> web页面或者命令hdfs dfs -ls / 进行相应的查看
好了,这就是今天的内容,下期会同步更新,至于更新什么,处于爱情(被逼无奈),奈何女朋友说不明白zookeeper是什么,在解释了半天也不行的情况下,大家应该明白我下次会说什么了吧
但是,随着互联网的发展,zookeeper已经不仅仅是在大数据中应用,互联网编程中也应用比较多,所以,下次的时候,会结合互联网的相关知识进行一些知识点的整理
觉得写的还不错的,欢迎点一下在看(作为一个从来不知道在看什么样的人,哎难受,女朋友说一个在看一个吻,跪求大家),或者评论一手666,谢谢啦
