1.hdfs(distributedfilesystem)1.1分布式文件系统当数据集的大小超过了一台独立计算机的存储能力时,需要通过网络中的多台机器来存储数据集,并对网络中的多台机器进行管理.由计算机组成的文件系统称为分布式文件系统。1.2hdfs的特点分布式数据量越来越大。如果它不能存储在一个操作系统的管辖范围内,它将被分配到操作系统管理的更多磁盘中。但是管理维护不方便,因此迫切需要一个系统来管理多台机器上的文件。这是一个分布式文件管理系统。高可用拷贝机制的透明性其实就是通过网络访问文件的动作。从用户的角度来看,就像访问本地磁盘一样。1.3hdfs架构namenodenamenode文件系统的管理节点,维护整个文件系统的文件目录树,用于接收用户请求datanode数据节点存储块(hadoop1.x版本64mb,2.x版本128mb)hadoop)1.4hdfs设计流式数据访问一次写入多次读取商业硬件Hadoop不需要运行在昂贵的商业机器上(ibm小型机等),低延迟数据访问需要几十毫秒才能得到响应结果,应用无法使用hdfs存储数据。虽然hdfs不能解决低延迟访问,但是基于hdfs的hbase可以解决大量的延迟问题。小文件的每个文件都存储在namenode中,存储文件目录信息和块信息,约占150字节。HDFS不适合存储小文件和多用户写入。任何可以修改和存储在HDFS中的文件都只能有一个写入者(writer)数据,只能在文件末尾追加数据,不能在文件的任何位置修改。1.4块规划块的大小:数据块大数据集存储的基本单位块在hadoop1.x版本中为64mb,在hadoop2.x版本中为128mb。为什么?如上设计的硬盘会有一次查找时间(10ms)。查找时间占传输时间的1%。硬盘的读取速率一般为100mb/s1.5Secondarynamenode合并合并edits和fsimage的时机2.1图形化操作2.2Shell操作2.3API操作3.HDFS操作(图形界面)3.1HDFS启动流程进入安全模式,加载fsimage,loadedits,savecheckpoint(整合fsimage和edits文件,生成新的fsimage)exitsecurity全模式3.2通过浏览器访问http://namenode:500704。hdfs操作(shell操作)hdfsdfshadoopfs5。hdfs操作(API操作)5.1依赖POMorg.apache.hadoophadoop-common2.6.4依赖项>org.apache.hadoophadoop-client2.6.4org.apache.hadoophadoop-hdfs2.6.45.2hdfs读写文件importorg.apache.commons.compress.utils.IOUtils;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.fs.*;importorg.junit.Test;publicclassHdfsTest{/***写文件操作*/@TestpublicvoidtestWriteFile()throwsException{//创建配置对象Configurationconf=n新配置();conf.set("fs.defaultFS","hdfs://uplooking01:8020");//创建文件系统对象FileSystemfs=FileSystem.get(conf);Pathpath=newPath("/test002.txt");FSDataOutputStreamfsDataOutputStream=fs.create(path,true);fsDataOutputStream.write("你好".getBytes());fsDataOutputStream.flush();fsDataOutputStream.close();}/***读文件操作*/@TestpublicvoidtestReadFile()throwsException{//创建配置对象Configurationconf=newConfiguration();conf.set("fs.defaultFS","hdfs://uplooking01:8020");//创建文件系统对象FileSystemfs=FileSystem.get(conf);路径path=newPath("/test002.txt");FSDataInputStreamfsDataInputStream=fs.open(path);IOUtils.copy(fsDataInputStream,System.out);}/***上传文件操作*/@TestpublicvoidtestuploadFile()throwsException{//创建配置对象Configurationconf=newConfi持续时间();conf.set("fs.defaultFS","hdfs://uplooking01:8020");//创建文件系统对象FileSystemfs=FileSystem.get(conf);PathfromPath=newPath("文件:///f:/test01.txt");PathtoPath=newPath("/test01.txt");fs.copyFromLocalFile(false,fromPath,toPath);}/***下载文件操作*/@TestpublicvoidtestdownloadFile()throwsException{//创建配置对象Configurationconf=newConfiguration();conf.set("fs.defaultFS","hdfs://uplooking01:8020");//创建文件系统对象FileSystemfs=FileSystem.get(conf);PathfromPath=newPath("/test01.txt");PathtoPath=newPath("文件:///f:/test01.txt");fs.copyToLocalFile(false,fromPath,toPath);}/***下载文件操作*/@TestpublicvoidtestOtherFile()throwsException{//创建配置对象Configurationconf=newConfiguration();conf.set("fs.defaultFS","hdfs://uplooking01:8020");//创建文件SystemobjectFileSystemfs=FileSystem.get(conf);//BlockLocation[]blockLocations=fs.getFileBlockLocations(newPath("/test01.txt"),0,134217730);//System.out.println(blockLocations);FileStatus[]listStatus=fs.listStatus(newPath("/test01.txt"));System.out.println(listStatus);}}3、hdfs回滚编辑高级操作:hdfsdfsadmin-rollEdits进入安全模式:hdfsdfsadmin-safemode|输入|离开|得到|等待合并编辑和fsimage:hdfsdfsadmin-saveNamespace:查看fsimage:hdfsoiv-i-o-p查看编辑:hdfsoev-i-o-p4。hdfs中的Quota管理Directoryquotasettingdirectoryquotahdfsdfsadmin-setQuotandirn:指的是目录配额的个数,如果是1,则不能存放任何文件,如果是2,则只能放置一个文件,依此类推在。Cleardirectoryquotashdfsdfsadmin-clrSpaceQuotadirspacequotasetspacequotahdfsdfsadmin-setSpaceQuotandirn:指空间clearspacequotahdfsdfsadmin-clrSpaceQuotadir5的大小。获取配置hdfsgetconf-confKeykeyname6。hadoop中的RPCRPC(RemoteProcedureCall)——远程过程调用协议是一种在不知道底层网络技术的情况下,通过网络向远程计算机程序请求服务的协议。设计目的:调用远程方法和调用本地方法一样方便6.1编写RPC服务端定义协议/***定义协议*/publicinterfaceIHelloServiceextendsVersionedProtocol{publiclongversionID=123456798L;//定义协议的版本publicStringsayHello(Stringname);//协议的具体入口}定义RPC的服务器实例类/***实例类,实现协议的类*/publicclassHelloServiceImplimplementsIHelloService{@OverridepublicStringsayHello(Stringname){System.out.println("==================="+名称+"==================");返回“你好”+姓名;}@OverridepubliclonggetProtocolVersion(Stringprotocol,longclientVersion)throwsIOException{returnversionID;}@OverridepublicProtocolSignaturegetProtocolSignature(Stringprotocol,longclientVersion,intclientMethodsHash)抛出IOException{returnnewProtocolSignature();}}定义RPC程序的启动程序publicclassMyRpcServer{publicstaticvoidmain(String[]args)throwsIOException{Configurationconf=newConfiguration();RPC.Serverserver=newRPC.Builder(conf).setBindAddress("172.16.4.3")//配置主机.setPort(8899)//配置端口.setProtocol(IHelloService.class)//配置协议.setInstance(newHelloServiceImpl())//配置实例,可以配置多个.build();服务器.start();系统。out.println("RPC服务器启动成功....");}}6.2编写RPC客户端定义协议/***定义协议*/publicinterfaceIHelloServiceextendsVersionedProtocol{publiclongversionID=123456798L;//定义协议版本publicStringsayHello(Stringname);//协议的具体入口}定义客户端启动程序Configurationconf=newConfiguration();ProtocolProxyproxy=RPC.getProtocolProxy(IHelloService.class,IHelloService.versionID,newInetSocketAddress("172.16.4.3",8899),conf);IHelloServicehelloService=proxy.getProxy();Stringret=helloService.sayHello("xiaoming");System.out.println(ret);7.独立启动namenodedatanodehadoop-daemon.shstartnamenodehadoop-daemon.shstartdatanodehadoop-daemon.shstartsecondarynamenodeyarn-daemon.shstartresourcemanageryarn-daemon.shstartnodemanager8。节点服务和退役动态添加节点,不需要停止整个集群hdfs维护一个白名单和一个黑名单8.1nodeservice==operateinnamenode==hdfs-site.xmdfs.hosts/opt/hadoop/etc/hadoop/dfs.include创建白名单文件/opt/hadoop/etc/hadoop/dfs.includeuplooking03uplooking04uplooking05uplooking06刷新节点:hdfsdfsadmin-refreshNodes8.1节点退役从白名单中移除加入黑名单刷新节点从黑名单中移除停止datanode进程