數據集的大小超過一臺獨立的計算機的存儲能力時,就要經過網絡中的多個機器來存儲數據集,把管理網絡中多臺計算機組成的文件系統,稱爲分佈式文件系統java
分佈式node
高可用shell
通透性apache
namenode瀏覽器
datanode安全
流式數據的訪問服務器
商用硬件網絡
低時間延時的數據訪問架構
大量的小文件分佈式
多用戶寫入,任意修改文件
block: 數據塊
爲何會有以上的設計
合併的時機
http://namenode:50070
<dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>2.6.4</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>2.6.4</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>2.6.4</version> </dependency>
import org.apache.commons.compress.utils.IOUtils; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import org.junit.Test; public class HdfsTest { /** * 寫文件操做 */ @Test public void testWriteFile() throws Exception { //建立配置對象 Configuration conf = new Configuration(); conf.set("fs.defaultFS", "hdfs://uplooking01:8020"); //建立文件系統對象 FileSystem fs = FileSystem.get(conf); Path path = new Path("/test002.txt"); FSDataOutputStream fsDataOutputStream = fs.create(path, true); fsDataOutputStream.write("hello".getBytes()); fsDataOutputStream.flush(); fsDataOutputStream.close(); } /** * 讀文件操做 */ @Test public void testReadFile() throws Exception { //建立配置對象 Configuration conf = new Configuration(); conf.set("fs.defaultFS", "hdfs://uplooking01:8020"); //建立文件系統對象 FileSystem fs = FileSystem.get(conf); Path path = new Path("/test002.txt"); FSDataInputStream fsDataInputStream = fs.open(path); IOUtils.copy(fsDataInputStream, System.out); } /** * 上傳文件操做 */ @Test public void testuploadFile() throws Exception { //建立配置對象 Configuration conf = new Configuration(); conf.set("fs.defaultFS", "hdfs://uplooking01:8020"); //建立文件系統對象 FileSystem fs = FileSystem.get(conf); Path fromPath = new Path("file:///f:/test01.txt"); Path toPath = new Path("/test01.txt"); fs.copyFromLocalFile(false, fromPath, toPath); } /** * 下載文件操做 */ @Test public void testdownloadFile() throws Exception { //建立配置對象 Configuration conf = new Configuration(); conf.set("fs.defaultFS", "hdfs://uplooking01:8020"); //建立文件系統對象 FileSystem fs = FileSystem.get(conf); Path fromPath = new Path("/test01.txt"); Path toPath = new Path("file:///f:/test01.txt"); fs.copyToLocalFile(false, fromPath, toPath); } /** * 下載文件操做 */ @Test public void testOtherFile() throws Exception { //建立配置對象 Configuration conf = new Configuration(); conf.set("fs.defaultFS", "hdfs://uplooking01:8020"); //建立文件系統對象 FileSystem fs = FileSystem.get(conf); // BlockLocation[] blockLocations = fs.getFileBlockLocations(new Path("/test01.txt"), 0, 134217730); // System.out.println(blockLocations); FileStatus[] listStatus = fs.listStatus(new Path("/test01.txt")); System.out.println(listStatus); } }
回滾edits: hdfs dfsadmin -rollEdits
進入安全模式: hdfs dfsadmin -safemode | enter | leave| get| wait
融合edits和fsimage: hdfs dfsadmin -saveNamespace:
查看fsimage: hdfs oiv -i -o -p
查看edits: hdfs oev -i -o -p
目錄配額
設置目錄配額
清除目錄配額
空間配額
設置空間配額
hdfs dfsadmin -setSpaceQuota n dir
清除空間配額
hdfs getconf -confKey keyname
設計目的:
定義協議
/** * 定義協議 */ public interface IHelloService extends VersionedProtocol { public long versionID = 123456798L;//定義協議的版本 public String sayHello(String name);//協議的具體條目 }
定義RPC的服務器實例類
/** * 實例類,實現了協議的類 */ public class HelloServiceImpl implements IHelloService { @Override public String sayHello(String name) { System.out.println("==================" + name + "=================="); return "hello" + name; } @Override public long getProtocolVersion(String protocol, long clientVersion) throws IOException { return versionID; } @Override public ProtocolSignature getProtocolSignature(String protocol, long clientVersion, int clientMethodsHash) throws IOException { return new ProtocolSignature(); } }
定義RPC程序的啓動程序
public class MyRpcServer { public static void main(String[] args) throws IOException { Configuration conf = new Configuration(); RPC.Server server = new RPC.Builder(conf) .setBindAddress("172.16.4.3")//配置主機 .setPort(8899)//配置端口 .setProtocol(IHelloService.class)//配置協議 .setInstance(new HelloServiceImpl())//配置實例,能夠配置多個 .build(); server.start(); System.out.println("RPC服務器啓動成功...."); } }
定義協議
/** * 定義協議 */ public interface IHelloService extends VersionedProtocol { public long versionID = 123456798L;//定義協議的版本 public String sayHello(String name);//協議的具體條目 }
定義客戶端啓動程序
Configuration conf = new Configuration(); ProtocolProxy<IHelloService> proxy = RPC.getProtocolProxy(IHelloService.class, IHelloService.versionID, new InetSocketAddress("172.16.4.3", 8899), conf); IHelloService helloService = proxy.getProxy(); String ret = helloService.sayHello("xiaoming"); System.out.println(ret);
hadoop-daemon.sh start namenode
hadoop-daemon.sh start datanode
hadoop-daemon.sh start secondarynamenode
yarn-daemon.sh start resourcemanager
yarn-daemon.sh start nodemanager
==在namenode中操做==
hdfs-site.xm
<!-- 白名單--> <property> <name>dfs.hosts</name> <value>/opt/hadoop/etc/hadoop/dfs.include</value> </property>
建立白名單文件
/opt/hadoop/etc/hadoop/dfs.include
uplooking03
uplooking04
uplooking05
uplooking06
刷新節點:
hdfs dfsadmin -refreshNodes