hdfs-api学习(0)

hdfs-api学习,需要导入share下的lib中的jar包,common、hdfs
jar包的依赖关系,common是最底层的

hdfs连接

Configuration conf = new Configuration();
//服务器地址端口号
conf.set("fs.defaultFS", "hdfs://192.168.31.12:9000");
try {
//连接到服务器上去
FileSystem fileSystem = FileSystem.get(conf);
//获取当前路径的状态
FileStatus fileStatus = fileSystem.getFileStatus(new Path("/upload"));
System.out.println(fileStatus.isFile());//是否是文件
System.out.println(fileStatus.isDirectory());
System.out.println(fileStatus.getPath());//文件路径
System.out.println(fileStatus.getLen());
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}

hdfs文件重命名

Configuration conf = new Configuration();
try {
FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.31.12:9000"), conf);
boolean rename = fileSystem.rename(new Path("/updata"), new Path("/upload"));
System.out.println(rename?"修改成功":"修改失败");
fileSystem.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}

文件上传

/**
* 文件上传
*/
@Test
public void upload(){

Configuration conf = new Configuration();
try {
FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.31.12:9000"), conf);
FSDataOutputStream out = fileSystem.create(new Path("/hdfs-api-study"));
FileInputStream in = new FileInputStream(new File("I:\\新建文件夹\\BigDate\\01 hadoop\\章节代码\\hdfs-api-study.rar"));
byte[] b = new byte[1024];
int len = 0;
while((len=in.read(b))!=-1) {
out.write(b,0,len);
}
in.close();
out.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}


/**
* commons-io IOuitls帮助我们简化
*/
@Test
public void upload2()
{
Configuration conf = new Configuration();
try {
FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.31.12:9000"),conf);
FSDataOutputStream out = fileSystem.create(new Path("/node"));
FileInputStream in = new FileInputStream(new File("d:\\hadoop之hdfs.md"));
IOUtils.copyBytes(in, out, conf);
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}

文件下载

/**
* 文件下载
*
*/
@Test
public void download()
{
Configuration conf = new Configuration();
try {
FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.31.12:9000"),conf);
FSDataInputStream in = fileSystem.open(new Path("/node"));
FileOutputStream out = new FileOutputStream(new File("d:\\hdfs"));
byte[] b = new byte[1024];
int len = 0;
while((len=in.read(b))!=-1){
out.write(b,0,len);
}
in.close();
out.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}

文件遍历

public static void dirandfile(Path path)
{
Configuration conf = new Configuration();
try {
FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.31.12:9000"),conf);
//获取当前目录下的文件状态
FileStatus[] listStatus = fileSystem.listStatus(path);
for(int i=0;i<listStatus.length;i++) {
if(listStatus[i].isDir()) {
Path path2 = listStatus[i].getPath();
dirandfile(path2);//当前文件是文件夹,继续遍历
}
else {
System.out.println(listStatus[i].getPath());//不是,输出文件名
}
}
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}

 

猜你喜欢

转载自www.cnblogs.com/yaoss/p/12363495.html
今日推荐