ITPub博客

首页 > 大数据 > Hadoop > hadoop 将文件上传到指定的datanode 查看文件所在的块

hadoop 将文件上传到指定的datanode 查看文件所在的块

原创 Hadoop 作者:hgs19921112 时间:2020-06-15 13:41:43 0 删除 编辑
package hgs.dfsclient.test;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.CreateFlag;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.permission.FsCreateModes;
import org.apache.hadoop.fs.permission.FsPermission;
import org.apache.hadoop.hdfs.DFSClient;
import org.apache.hadoop.hdfs.DFSOutputStream;
import org.apache.hadoop.hdfs.protocol.*;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.EnumSet;
import java.util.List;
import java.util.Random;
public class MainTest {
    public static void main(String[] args) throws IOException, URISyntaxException {
        Configuration conf =  new Configuration();
        conf.addResource(new File("D://hdfsconf/core-site.xml").toURI().toURL());
        conf.addResource(new File("D://hdfsconf/hdfs-site.xml").toURI().toURL());
        String url = conf.get("fs.defaultFS");
        System.out.println(url);
        Path path = new Path("/user/test.txt");
        DFSClient client = new DFSClient(new URI("hdfs://192.168.0.191:9000/"),conf);
        //favor datanode
        InetSocketAddress [] favor = {new InetSocketAddress("192.168.0.191", 50012)};
        createAndFillFile(client,conf,favor,"/user/test.txt4");
        listBlockLocation(client, conf, "/user/test.txt4",true );
        client.close();
    }
        //将数据传输到指定的datanode
    public static void createAndFillFile(DFSClient client,Configuration conf,
                                         InetSocketAddress [] favor,String src) throws IOException {
        DFSOutputStream dfsOutputStream = client.create(src, FsCreateModes.applyUMask(
                FsPermission.getFileDefault(), FsPermission.getUMask(conf)), EnumSet.of(CreateFlag.CREATE, CreateFlag.OVERWRITE),
                true, (short) 1, 32 * 1024 * 1024,
                null, 8192, null, favor, null);
        byte[] b = new byte[1024*3*3];
        for(int i = 0 ;i<100000;i++){
            fileByte(b);
            dfsOutputStream.write(b);
        }
        dfsOutputStream.close();
    }
    static Random rand = new Random(System.nanoTime());
    public static void fileByte(byte[] b){
        if(b != null ){
            rand.nextBytes(b);
        }
    }
        //获取文件的block位置
    public static void listBlockLocation(DFSClient client,Configuration conf,String src,boolean needLocation) throws IOException {
        DirectoryListing directoryListing = client.listPaths(src, HdfsFileStatus.EMPTY_NAME,
                needLocation);
        HdfsFileStatus[] fileStatuses = directoryListing.getPartialListing();
        for(int i=0 ; i <fileStatuses.length ; i++){
            HdfsLocatedFileStatus hl = (HdfsLocatedFileStatus)fileStatuses[i];
            LocatedBlocks locatedBlocks = hl.getLocatedBlocks();
            List<LocatedBlock> locatedBlocks1 = locatedBlocks.getLocatedBlocks();
            for(LocatedBlock lb : locatedBlocks1 ){
                DatanodeInfo[] locations = lb.getLocations();
                for(DatanodeInfo li : locations){
                    System.out.println("addr:"+li.getIpAddr()+"--port:"+li);
                }
            }
        }
    }
}


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31506529/viewspace-2698379/,如需转载,请注明出处,否则将追究法律责任。

下一篇: linux cook
请登录后发表评论 登录
全部评论

注册时间:2017-11-22

  • 博文量
    105
  • 访问量
    148857