ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

HDFS API操作

2022-04-22 21:00:29  阅读:128  来源: 互联网

标签:HDFS fs Configuration API IOException conf Path new 操作


一、导入依赖包

在File->Project Structure->Modules->Dependencies下导入Hadoop->share文件下的相应jar包,包括:

common下的:hadoop-common-3.2.1.jar、

hadoop-nfs-3.2.1.jar以及所有lib下的jar包

hdfs下以及hdfs/lib下的所有jar包

 

 

 

二、api操作

 

package hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.fs.permission.FsPermission;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;
import org.junit.Test;

import java.io.*;import java.net.URI;

public class hdfsapi {
/**
 * ./hadoop fs -ls /
 * @throws IOException
 */
@Test
public void createFolder() throws IOException {
    Configuration conf=new Configuration();
    FileSystem fs=FileSystem.get(URI.create("hdfs://192.168.253.128:9000"),conf);
    Path path=new Path("/test2");
    fs.mkdirs(path, FsPermission.getDirDefault());
    System.out.println("finished");
}

/**
 * ./hadoop fs -ls /test2/
 * @throws IOException
 */
@Test
public void createFile() throws IOException {
    Configuration conf=new Configuration();
    FileSystem fs=FileSystem.get(URI.create("hdfs://192.168.253.128:9000"),conf);
    Path path=new Path("/test2/a1.txt");
    FSDataOutputStream out = fs.create(path);
    out.write("11111".getBytes());

    System.out.println("finished");
}

/**
 * ./hadoop fs -ls /test2/
 * @throws IOException
 */
@Test
public void renameFile() throws IOException {
    Configuration conf=new Configuration();
    FileSystem fs=FileSystem.get(URI.create("hdfs://192.168.253.128:9000"),conf);
    Path path=new Path("/test2/a1.txt");
    Path path2=new Path("/test2/a2.txt");
    boolean result = fs.rename(path, path2);

    System.out.println(result);
}

/**
 * ./hadoop fs -ls /test2/
 * @throws IOException
 */
@Test
public void uploadFile() throws IOException {
    Configuration conf=new Configuration();
    FileSystem fs=FileSystem.get(URI.create("hdfs://192.168.253.128:9000"),conf);
    Path path=new Path("F:\\5、开发语言\\java\\it.cast\\knowledge\\src\\main\\resources\\1.txt");
    Path path2=new Path("/test2/a3.txt");
    fs.copyFromLocalFile(path,path2);

    System.out.println("finished.");
}

/**
 * 上传文件方式2 显示进度
 * @throws IOException
 */
@Test
public void uploadFile2() throws IOException {
    Configuration conf=new Configuration();
    FileSystem fs=FileSystem.get(URI.create("hdfs://192.168.253.128:9000"),conf);
    InputStream in =new BufferedInputStream(new FileInputStream(new File("D:\\software\\CodeSmithGeneratoTemplates.7z")));
    FSDataOutputStream out=fs.create(new Path("/test2/a4.7z"),new Progressable(){
        @Override
        public void progress(){
            System.out.println("...");
        }
    });
    IOUtils.copyBytes(in,out,4096);

    System.out.println("finished.");
}

/**
 * ./hadoop fs -ls /test2/
 * @throws IOException
 */
@Test
public void getFileList() throws IOException {
    Configuration conf=new Configuration();
    FileSystem fs=FileSystem.get(URI.create("hdfs://192.168.253.128:9000"),conf);
    Path path=new Path("/test2");
    FileStatus[] fileStatuses = fs.listStatus(path);
    for (FileStatus fileStatus:fileStatuses) {
        System.out.println(fileStatus.getPath());
    }
}

/**
 * ./hadoop fs -ls /test2/
 * @throws IOException
 */
@Test
public void getFileBlock() throws IOException {
    Configuration conf=new Configuration();
    FileSystem fs=FileSystem.get(URI.create("hdfs://192.168.253.128:9000"),conf);
    Path path=new Path("/test2/a4.7z");
    FileStatus fileStatus=fs.getFileStatus(path);
    BlockLocation[] fileBlockLocations = fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
    for(BlockLocation loc:fileBlockLocations){
        for(int i=0;i<loc.getHosts().length;i++){
            System.out.println(loc.getHosts()[i]);
        }
    }
}

 

}

 

三、创建问题

 

1、提示无权限

 

vim hdfs-site.xml

<configuration>

     <property>

           <name>dfs.permissions.enabled</name>

         <value>false</value>

     </property>
</configuration>

 

标签:HDFS,fs,Configuration,API,IOException,conf,Path,new,操作
来源: https://www.cnblogs.com/binfirechen/p/16180487.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有