java向hdfs提交命令_Java语言操作HDFS常用命令测试代码

本文主要向大家介绍了Java语言操作HDFS常用命令测试代码,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

package com.yxc.hdfs;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.*;

import org.apache.hadoop.io.IOUtils;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.net.URI;

import java.net.URISyntaxException;

import java.util.Date;

/**

* 测试HDFS的API

*

* @author yxc

* @create 2018-05-03 18:21

**/

public class TestHDFS04 {

private Configuration cf = null;

private FileSystem fs = null;

/**

* 获取配置信息,以及获取文件系统

*/

@Before

public void beforeRun() throws URISyntaxException, IOException, InterruptedException {

//获取配置信息

cf = new Configuration();

//获取文件系统

fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), cf, "yxc");

}

/**

* 创建文件目录

*/

@Test

public void mkdir () throws IOException {

boolean mkdirs = fs.mkdirs(new Path("/input/"));

if (mkdirs) {

System.out.println("文件创建成功");

} else {

System.out.println("文件创建失败");

}

}

/**

* 判断目录是否存在

*/

@Test

public void isDirectory() throws IOException {

boolean directory = fs.isDirectory(new Path("/input/"));

if (directory) {

System.out.println("存在input 的这个目录");

} else {

System.out.println("不存在input的这个目录");

}

}

/**

* 删除文件或文件目录

*/

@Test

public void delete() throws IOException {

boolean delete = fs.delete(new Path("/input/"), true);

if (delete) {

System.out.println("成功删除");

} else {

System.out.println("删除失败");

}

}

/**

* 获取文件下的资源

*/

@Test

public void getFile() throws IOException {

RemoteIterator listFiles = fs.listFiles(new Path("/"), true);

if (listFiles == null) {

System.out.println("文件下没有资源");

} else {

System.out.println("文件下有资源");

}

while (listFiles.hasNext()) {

LocatedFileStatus file = listFiles.next();

if (file.isDirectory()) {

System.out.println("是文件夹");

} else {

System.out.println("是文件");

}

System.out.println("文件名:"+file.getPath().getName());

}

}

/**

* 上传文件

*/

@Test

public void upFile() throws IOException {

//配置信息

//获取文件系统

//获取输入流

FileInputStream inputStream = new FileInputStream(new File("D:/yue5.txt"));

//获取输出流

FSDataOutputStream outputStream = fs.create(new Path(

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注编程语言JAVA频道!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值