@@ -1387,6 +1387,224 @@ LocalFileSystem 从ChecksumFileSystem 类派生,主要实现其它一些支持
1387
1387
LocalFileSystem 有一个属性rfs,用来表原生的文件系统。LocalFileSystem 重写了一些方法,
1388
1388
如copyFromLocalFile 和copyToLocalFile 等。
1389
1389
1390
+
1391
+ 7 AbstractFileSystem 分析
1392
+ -------------------------
1393
+ 在分析该类层次结构时,可以将AbstractFileSystem 与FileSystem 对应,FilterFs 与FilerFileSystem 对应,
1394
+ ChecksumFs与ChecksumFileSystem 对应,LocalFs与LocalFileSystem 对应,RawLocalFs与RawLocalFileSystem 对应。
1395
+ 他们完成的功能及其相似。
1396
+
1397
+ ###7 .1 AbstractFileSystem 抽象类
1398
+ AbstractFileSystem 是0.21 版本新出现的API,应该是用来替代FileSystem 的。
1399
+ 该类为 Hadoop文件系统的实现提供了一个接口。AbstractFileSystem 是一个抽象类。
1400
+ 它与FileSystem 有很多同名的方法。这些同名的方法完成相同的功能。
1401
+ 比如 get,create 等等。其它的方法大都是抽象方法。
1402
+
1403
+ 图7-1:
1404
+ ![ img] ( ./images/7-1.jpg )
1405
+
1406
+ get 方法调用了createFileSystm 方法实现。
1407
+ 而createFileSystm 方法实现与FileSystm的createFileSystm 可以说是完全一样。
1408
+ create 方法首先解析参数,然后调用createInternal 抽象方法。
1409
+
1410
+ ###7 .2 FilterFs抽象类
1411
+ 图7-2:
1412
+
1413
+ ![ img] ( ./images/7-2.png )
1414
+
1415
+ FilterFs也是一个抽象类,它同FilterFileSystem 完全一样,只是拥有一个AbstractFileSystem类型的属性myFs。
1416
+ FilterFs将其作为基本的文件系统。FilterFs类几乎将所有重写的方法交给了其内部保存的myFs来处理。
1417
+ 但在交给 myFs处理之前,自己可以做一些处理,以此来实现过滤。如:
1418
+
1419
+ ``` java
1420
+ public FSDataOutputStream createInternal(Path f,
1421
+ EnumSet<CreateFlag > flag, FsPermission absolutePermission, int bufferSize,
1422
+ short replication, long blockSize, Progressable progress,
1423
+ ChecksumOpt checksumOpt, boolean createParent)
1424
+ throws IOException , UnresolvedLinkException {
1425
+ checkPath(f);
1426
+ return myFs. createInternal(f, flag, absolutePermission, bufferSize,
1427
+ replication, blockSize, progress, checksumOpt, createParent);
1428
+ }
1429
+ ```
1430
+
1431
+ ###7 .3 ChecksumFs抽象类
1432
+ ChecksumFs抽象类的实现与ChecksumFileSystem 的实现完全一样。
1433
+ 只是ChecksumFs使用的是ChecksumFs.ChecksumFSInputStream ,
1434
+ 而ChecksumFileSystem使用的是ChecksumFileSystem.ChecksumFSInputStream。
1435
+
1436
+ 图7-3:
1437
+
1438
+ ![ img] ( ./images/7-3.png )
1439
+
1440
+ ###7 .4 LocalFs类
1441
+ LocalFs仅仅有两个构造函数。如图7-4:
1442
+
1443
+ ![ img] [ 7-4.png ]
1444
+
1445
+ DelegateToFileSystem抽象类:
1446
+
1447
+ ![ img] [ 7-5.png ]
1448
+
1449
+ ###7 .5 DelegateToFileSystem
1450
+ 顾名思义,DelegateToFileSystem 是一个代理类。
1451
+ 它简单的将所有的操作交给 FileSystm类型的属性fsImpl 来处理。
1452
+
1453
+ 目前Hadoop源码中只有FtpFs和RawLocalFs是从其派生的。
1454
+
1455
+ ###7 .6 FileContext类
1456
+ FileContext 类是0.21 版中提供的新的API,它为应用程序编写者提供了访问Hadoop文件系统的接口,例如create open list等方法。
1457
+
1458
+ 类图如图7-6:
1459
+
1460
+ ![ img] [ 7-6.jpg ]
1461
+
1462
+ #####7 .6.1 Hadoop中的路径
1463
+
1464
+ * Hadoop中使用Path 来表示一个文件路径。Path 中含有文件所在的URI 。
1465
+ * Hadoop支持URI 名字空间和URI 名字。Hadoop中URI 名字非常灵活,使用者可以在不知道
1466
+ * 服务器地址或名字的情况下,访问默认的文件系统。默认的文件系统通过配置文件来制定。
1467
+
1468
+ Hadoop中路径名有三种形式:
1469
+ 1 . 完全限定的URI :scheme://authority/path
1470
+ 2 . 以斜杠开头的路径:/path 表示相对于默认的文件系统,即相当于default-scheme:// default-authority/path
1471
+ 3 . 相对路径:path 相对于工作目录
1472
+ 完全限定的URI 和以斜杠开头的路径称为绝对路径。
1473
+ 但如下形式的路径则是非法的:scheme: foo /bar
1474
+
1475
+ Hadoop 中文件和目录的路径用 Path 类表示。Path 的表示与 Unix 路径相似,使用’/’来分隔目
1476
+ 录,而不是’\’。Path类图如图7-7:
1477
+
1478
+ ![ img] [ 7-7.jpg ]
1479
+
1480
+ ####7 .6.2 server side属性
1481
+ 所有的文件系统实例(例如文件系统的部署)都有默认属性。这些属性叫做 server side (SS)
1482
+ defaults。像 create 这样的操作允许选择多种属性:要么当做参数传给它,要么使用 SS 属性。
1483
+
1484
+ SS 属性由 FsServerDefaults 表示, 如图7-8:
1485
+
1486
+ ![ img] [ 7-8.jpg ]
1487
+
1488
+ 与文件系统有关的 SS 属性有
1489
+
1490
+ * the home directory (default is "/user/userName")
1491
+ * the initial wd (only for local fs)
1492
+ * replication factor 分片因子
1493
+ * block size
1494
+ * buffer size
1495
+ * bytesPerChecksum (if used).
1496
+
1497
+ ###7 .6.3 FileContext
1498
+ FileContext 由默认文件系统、工作目录和umask 定义。
1499
+
1500
+ 获得一个FileContext 可以使用FileContext 的静态方法: getFileContext();getLocalFSFileContext()
1501
+
1502
+ getFileContext 有多重重载的形式。但最终都是调用了如下函数:
1503
+
1504
+ ``` java
1505
+ /**
1506
+ * Protected Static Factory methods for getting a FileContexts
1507
+ * that take a AbstractFileSystem as input. To be used for testing.
1508
+ */
1509
+
1510
+ /**
1511
+ * Create a FileContext with specified FS as default using the specified
1512
+ * config.
1513
+ *
1514
+ * @param defFS
1515
+ * @param aConf
1516
+ * @return new FileContext with specifed FS as default.
1517
+ */
1518
+ public static FileContext getFileContext(final AbstractFileSystem defFS,
1519
+ final Configuration aConf) {
1520
+ return new FileContext (defFS, FsPermission . getUMask(aConf), aConf);
1521
+ }
1522
+
1523
+ /**
1524
+ * Create a FileContext for specified file system using the default config.
1525
+ *
1526
+ * @param defaultFS
1527
+ * @return a FileContext with the specified AbstractFileSystem
1528
+ * as the default FS.
1529
+ */
1530
+ protected static FileContext getFileContext(
1531
+ final AbstractFileSystem defaultFS) {
1532
+ return getFileContext(defaultFS, new Configuration ());
1533
+ }
1534
+
1535
+ ```
1536
+
1537
+ public void setWorkingDirectory(final Path newWDir) throws IOException 用来设置工作目录。
1538
+
1539
+ FileContext 中的很多方法是用来取代FileSystem 中的方法的。
1540
+ 包括create,mkdir,delete,open ,setReplication ,rename ,
1541
+ setPermission ,setOwner ,setTimes ,getFileStatus,
1542
+ getFileLinkStatus,getLinkTarget ,getFileBlockLocations等方法在AbstractFileSystem 中都有对应的方法。
1543
+ 这些方法的实现具有相同的形式。比如open 方法的代码如下:
1544
+
1545
+ ``` java
1546
+ /**
1547
+ * Opens an FSDataInputStream at the indicated Path.
1548
+ *
1549
+ * @param f the file name to open
1550
+ * @param bufferSize the size of the buffer to be used.
1551
+ *
1552
+ * @throws AccessControlException If access is denied
1553
+ * @throws FileNotFoundException If file <code >f</code> does not exist
1554
+ * @throws UnsupportedFileSystemException If file system for <code >f</code> is
1555
+ * not supported
1556
+ * @throws IOException If an I/O error occurred
1557
+ *
1558
+ * Exceptions applicable to file systems accessed over RPC:
1559
+ * @throws RpcClientException If an exception occurred in the RPC client
1560
+ * @throws RpcServerException If an exception occurred in the RPC server
1561
+ * @throws UnexpectedServerException If server implementation throws
1562
+ * undeclared exception to RPC server
1563
+ */
1564
+ public FSDataInputStream open(final Path f, final int bufferSize)
1565
+ throws AccessControlException , FileNotFoundException ,
1566
+ UnsupportedFileSystemException , IOException {
1567
+ final Path absF = fixRelativePart(f);
1568
+ return new FSLinkResolver<FSDataInputStream > () {
1569
+ @Override
1570
+ public FSDataInputStream next (final AbstractFileSystem fs , final Path p )
1571
+ throws IOException , UnresolvedLinkException {
1572
+ return fs. open(p, bufferSize);
1573
+ }
1574
+ }. resolve(this , absF);
1575
+ }
1576
+ ```
1577
+
1578
+ 内部FSLinkResolver<T >抽象模板使用来解析路径中的符号链接的。
1579
+ public abstract T next(final AbstractFileSystem fs, final Path p)为其抽象方法。
1580
+ resolve 方法如下:
1581
+
1582
+ ``` java
1583
+ /**
1584
+ * Resolves all symbolic links in the specified path.
1585
+ * Returns the new path object.
1586
+ */
1587
+ protected Path resolve(final Path f) throws FileNotFoundException ,
1588
+ UnresolvedLinkException , AccessControlException , IOException {
1589
+ return new FSLinkResolver<Path > () {
1590
+ @Override
1591
+ public Path next (final AbstractFileSystem fs , final Path p )
1592
+ throws IOException , UnresolvedLinkException {
1593
+ return fs. resolvePath(p);
1594
+ }
1595
+ }. resolve(this , f);
1596
+ }
1597
+ ```
1598
+
1599
+ FileContext 的getFSofPath 方法用来获得支持指定路径path 的文件系统。
1600
+ 该方法首先检查路径path 是不是属于默认文件系统支持的路径,
1601
+ 如果是则返回 FileContext.defaultFS 属性;否则AbstractFileSystem.get来获取对应的文件系统。
1602
+
1603
+ 在获得文件系统fs 之后,resolve 会调用next 方法,并返回其结果。
1604
+ 如果出现异常,则解析路径中的符号链接,再次调用next 。
1605
+
1606
+ 总的来说,FileContext.open 是通过调用AbstractFileSystem.open来实现的。
1607
+
1390
1608
[ 3-1.jpg ] : ./images/3-1.jpg
1391
1609
[ 4-1.png ] : ./images/4-1.png
1392
1610
[ 4-2.png ] : ./images/4-2.png
@@ -1402,3 +1620,10 @@ LocalFileSystem 有一个属性rfs,用来表原生的文件系统。LocalFileS
1402
1620
[ 5-7.jpg ] : ./images/5-7.jpg
1403
1621
[ 5-8.jpg ] : ./images/5-8.jpg
1404
1622
[ 5-9.jpg ] : ./images/5-9.jpg
1623
+ [ 7-4.png ] : ./images/7-4.png
1624
+ [ 7-5.png ] : ./images/7-5.png
1625
+ [ 7-6.jpg ] : ./images/7-6.jpg
1626
+ [ 7-7.jpg ] : ./images/7-7.jpg
1627
+ [ 7-8.jpg ] : ./images/7-8.jpg
1628
+
1629
+
0 commit comments