HDFS的JavaAPI操作

一、搭建环境开发

1.下载maven压缩包,解压

2.在eclipse中配置maven

(1)windows > preferences > maven > installation > add > 将解压完的maven添加进去

这里写图片描述
点击apply
在maven解压目录下找到/conf/settings.xml文件,修改本地存放位置为maven仓库位置
这里写图片描述
windows > preferences > maven > User setting
这里写图片描述
等待maven仓库下载安装,安装完毕即可创建maven工程

(2)创建maven工程以及配置pom文件

创建一个maven 工程
配置pom.xml,在project标签中配置Junit版本、hadoop版本、jdk版本,可根据个人版本不同修改version标签
“`

    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-client</artifactId>
        <version>2.8.3</version>
    </dependency>

</dependencies>

<!-- maven打包插件jdk的版本  -->
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>
```

二、hdfs的Java操作

public class HDFSdemo {
    //获取一个客户端实例
    FileSystem fs = null;
    @Before
    public void init() throws Exception{
        Configuration conf = new Configuration();
        //get方法从conf中的一个参数fs.defaultFS的配置值判断
        fs  = FileSystem.get(new URI("hdfs://hadoop01:9000"), conf, "root");
    }

    /**
     * 上传文件
     */
    @Test
    public void UpLoad() throws IOException, InterruptedException, URISyntaxException{

        fs.copyFromLocalFile(new Path("D:\\a\\1.txt"),new Path("/1.txt"));
    }

    /**
     * 下载文件
     * 
     */
    @Test
    public void DownLoad() throws Exception{
        fs.copyToLocalFile(new Path("/1.txt"), new Path("d:\\1.txt"));
    }

    /**
     * 删除文件
     */
    @Test
    public void Delete() throws Exception{
        fs.delete(new Path("/1.txt"), true);
    }

    /**
     * 创建目录
     */
    @Test
    public void mkdir() throws IllegalArgumentException, IOException{
        fs.mkdirs(new Path("/aa"));
    }
    /**
     * 移动文件及修改文件名
     */
    @Test
    public void Rename() throws IllegalArgumentException, IOException{
        //移动的时候需要目录存在,不存在的时候不抛异常,但是执行不成功
        fs.rename(new Path("/1.txt"), new Path("/aa/1.txt"));
    }


    /**
     * 文件的状态    列出来的都是文件
     */
    @Test
    public void listFiles() throws FileNotFoundException, IllegalArgumentException, IOException{
        //得到有哪些文件的迭代器
        RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
        while(listFiles.hasNext()){
            //单个文件
            LocatedFileStatus fileInfo = listFiles.next();
            System.out.println(fileInfo.getLen()); //得到文件长度
            System.out.println(fileInfo.getBlockSize()); //得到文件快大小
            System.out.println(fileInfo.getPath()); //得到路径
            System.out.println(fileInfo.getAccessTime());  //得到最后修改时间
            System.out.println(fileInfo.getReplication());   //副本数量
            System.out.println("--------------------------------");
            //获取所有文件块    文件块对应的偏移量信息   存放在哪里
            BlockLocation[] blockLocations = fileInfo.getBlockLocations();

            for (BlockLocation blockLocation : blockLocations) {
                System.out.println(blockLocation);
            }
            System.out.println("+++++++++++++++");
        }

    }


    /**
     * 列出文件夹下面的所有文件以及目录
     */
    @Test
    public void listfiles2() throws FileNotFoundException, IllegalArgumentException, IOException{
        FileStatus[] listStatus = fs.listStatus(new Path("/"));
        for (FileStatus fileStatus : listStatus) {
            if(fileStatus.isDirectory()){
                System.out.println("这是一个文件夹");
            }
            if(fileStatus.isFile()){
                System.out.println("这是一个文件");
            }
            System.out.println(fileStatus.getLen()); //得到文件长度
            System.out.println(fileStatus.getBlockSize()); //得到文件快大小
            System.out.println(fileStatus.getPath()); //得到路径
            System.out.println(fileStatus.getAccessTime());  //得到最后修改时间
            System.out.println(fileStatus.getReplication());   //副本数量

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



    @After
    public void close() throws IOException{
        fs.close();
    }

}

猜你喜欢

转载自blog.csdn.net/amin_hui/article/details/81877839
今日推荐