Hadoop-HDFS常用API,我给封装好了相应的模板,使用哪个粘贴哪个即可

在 Java中操作 HDFS,首先要获得一个客户端实例(HDFS的所以操作都要依赖这个客户端实例来进行):

Configuration conf = new Configuration(); 
FileSystem fs = FileSystem.get(conf);

1.建立文件夹

/** * 创建文件夹 */
@Test 
public void testMkdir() throws Exception {
	 System.out.println(fs.mkdirs(new Path("/ccc/bbb/aaa"))); }

2.上传文件

/** * 上传文件 */
@Test 
public void testCopyFromLocal() throws Exception { 
// src : 要上传的文件所在的本地路径 
// dst : 要上传到hdfs的目标路径 
Path src = new Path("C:/software/hadoop-eclipse-plugin-2.7.7.jar"); 
Path dst = new Path("/"); 
fs.copyFromLocalFile(src, dst); }

3.下载文件

* 下载文件
*/
@Test 
public void testCopyToLocal() throws Exception {
    fs.copyToLocalFile(new Path("/wordcount/input/helloWorld.txt"), new Path("c:/"));
}

4.删除文件

* 删除文件 或者 文件夹
*/
@Test 
public void testRemoveFileOrDir() throws Exception {
    // 删除文件或者文件夹,如果文件夹不为空,这第二个参数必须有, 而且要为true
    fs.delete(new Path("/ccc/bbb"), true); }

5.重命名文件

* 重命名文件 或者 文件夹
*/
@Test 
public void testRenameFileOrDir() throws Exception {
    // fs.rename(new Path("/ccc"), new Path("/vvv"));
    fs.rename(new Path("/hadoop-eclipse-plugin-2.6.4.jar"), new Path("/eclipsePlugin.jar"));
}

6. 查看目录信息,只显示该文件夹下的文件信息

* 查看指定文件下 的 文件 或者 文件夹。 不包含子文件夹下的内容
*/
@Test 
public void testListStatus() throws Exception {
    FileStatus[] listStatus = fs.listStatus(new Path("/wordcount"));
    String flag = "";
    for (FileStatus status : listStatus) {
        if (status.isDirectory()) {
            flag = "Directory";
        } else {
            flag = "File";
        }
        System.out.println(flag + "\t" + status.getPath());
    }
}

7.查看文件及文件夹信息

     * 显示 指定文件夹下  所有的文件
     */
@Test 
public void testListFiles() throws Exception {
    RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/wordcount"), true);
    while (listFiles.hasNext()) {
        LocatedFileStatus fileStatus = listFiles.next();
        System.out.print(fileStatus.getPath() + "\t");
        System.out.print(fileStatus.getPath().getName() + "\t");
        System.out.print(fileStatus.getBlockSize() + "\t");
        System.out.print(fileStatus.getPermission() + "\t");
                System.out.print(fileStatus.getReplication() + "\t");
        System.out.println(fileStatus.getLen());

        BlockLocation[] blockLocations = fileStatus.getBlockLocations();
        for (BlockLocation bl : blockLocations) {
            System.out.println("Block Length:" + bl.getLength() + "   Block OffSet:" + bl.getOffset());
            String[] hosts = bl.getHosts();
            for (String str : hosts) {
                System.out.print(str + "\t");
            }
            System.out.println();
        }

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值