HDFS常用代码片段备忘

FileSystem fs;

DistributedFileSystem dfs;

 

得到FileSystem:    fs = new Path("/").getFileSystem(conf);

RPC调用Namenode方法:    fs.getClient().namenode.XXXXX();

得到某个文件corrupt的数据块:

      LocatedBlocks locatedBlocks =

                   fs.getClient().namenode.getBlockLocations(uriPath, 0, stat.getLen());
      for (LocatedBlock b: locatedBlocks.getLocatedBlocks()) {
        if (b.isCorrupt() ||
            (b.getLocations().length == 0 && b.getBlockSize() > 0)) {
            // the block is corrupt;
        }
      }

 

      final BlockLocation[] fileBlocks =
                  dfs.getFileBlockLocations(fileStatus, 0, fileLength);

      for (BlockLocation fileBlock: fileBlocks) {
          if (fileBlock.isCorrupt() ||
            (fileBlock.getNames().length == 0 && fileBlock.getLength() > 0)) {

              // the block is corrupt;
          }
      }

 

Path->String:  String uri = path.toUri().getPath();

String->Path:  Path path = new Path(uri);

 

java.net.URI dfsUri = DistributedFileSystem.getUri();

System.out.println(dfsUri)  ------>  hdfs://namenodehostname:9000

 

DistributedFileSystem.makeQualified(new Path("/foo")));   --------->  hdfs://namenodehostname:9000/foo

                                                                    new Path(scheme+":"+"//"+authority + pathUri.getPath();

 

 

Namenode上记录了那些LOG:

static void turnOffNameNodeLogging() {
    // change log level to ERROR: NameNode.LOG & NameNode.stateChangeLog
    ((Log4JLogger)NameNode.LOG).getLogger().setLevel(Level.ERROR);
    ((Log4JLogger)NameNode.stateChangeLog).getLogger().setLevel(Level.ERROR);
    ((Log4JLogger)NetworkTopology.LOG).getLogger().setLevel(Level.ERROR);
    ((Log4JLogger)FSNamesystem.LOG).getLogger().setLevel(Level.ERROR);
    ((Log4JLogger)FSNamesystem.auditLog).getLogger().setLevel(Level.ERROR);
    ((Log4JLogger)LeaseManager.LOG).getLogger().setLevel(Level.ERROR);
  }

 

 

切换HDFS的数据块位置选择策略:conf的dfs.block.replicator.classname

 conf.set("dfs.block.replicator.classname", "org.apache.hadoop.hdfs.server.namenode.BlockPlacementPolicyRaid");

 

 

Path p = new Path("/raid"); LOG.info(p) --->  /raid

String xorPrefix = p.toUri().getPath();  LOG.info(xorPrefix);  ----->/raid

 

Path p = new Path("hdfs://0.0.0.0:3000" + "/raid");  --->hdfs://0.0.0.0:3000/raid

String xorPrefix = p.toUri().getPath();  LOG.info(xorPrefix);  ----->/raid

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值