2018-07-10期 HadoopHDFS客户端API操作
2018-07-10 09:55
609 查看
1、新建JAVA工程
新建完成后如下图:
新建用户库,并添加外部jar文件
往hdfslib添加jar文件
由于HDFS的API只需要添加HDFS相关及hadoop通用jar包即可,包路径在E:\depslib\hadoop-2.4.1\share\hadoop下,这里添加HDFS jar包如下
同时还要添加hadoop通用jar包,路径为E:\depslib\hadoop-2.4.1\share\hadoop\common
为此,HDFS API开发相关JAR包已经添加完成,工程创建完成,下面可以进行HDFS相关API程序编写。
2、HDFS 客户端API代码测试
分享测试Demo代码如下:
package cn.qlq.hdfs;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.junit.Before;
import org.junit.Test;
public class HdfsUtil {
private FileSystem fs = null;
@Before
public void befor() throws IOException, InterruptedException, URISyntaxException{
//读取classpath下的xxx-site.xml 配置文件,并解析其内容,封装到conf对象中
Configuration conf = new Configuration();
//也可以在代码中对conf中的配置信息进行手动设置,会覆盖掉配置文件中的读取的值
conf.set("fs.defaultFS", "hdfs://localhost:9000/");
//根据配置信息,去获取一个具体文件系统的客户端操作实例对象
fs = FileSystem.get(new URI("hdfs://localhost:9000/"),conf,"root");
}
/**
上传文件,比较底层的写法
@throws Exception
*/
@Test
public void upload() throws Exception {
Path dst = new Path("hdfs://localhost:9000/aa/qingshu.txt");
FSDataOutputStream os = fs.create(dst);
FileInputStream is = new FileInputStream("/opt/download/haha.txt");
IOUtils.copy(is, os);
}
/**
上传文件,封装好的写法
@throws Exception
@throws IOException
*/
@Test
public void upload2() throws Exception, IOException{
fs.copyFromLocalFile(new Path("/opt/download/haha.txt"), new Path("hdfs://localhost:9000/aa/qingshu2.txt"));
}
/**
download file
@throws IOException
*/
@Test
public void download() throws IOException {
Path path = new Path("hdfs://localhost:9000/jdk-7u65-linux-i586.tar.gz");
FSDataInputStream input = fs.open(path);
FileOutputStream output = new FileOutputStream("/opt/download/doload.tgz");
IOUtils.copy(input, output);
}
/**
下载文件
@throws Exception
@throws IllegalArgumentException
*/
@Test
public void download2() throws Exception {
fs.copyToLocalFile(new Path("hdfs://localhost:9000/aa/qingshu2.txt"), new Path("/opt/download/haha2.txt"));
}
/**
查看文件信息
@throws IOException
@throws IllegalArgumentException
@throws FileNotFoundException
*/
@Test
public void listFiles() throws FileNotFoundException, IllegalArgumentException, IOException {
// listFiles列出的是文件信息,而且提供递归遍历
RemoteIterator<LocatedFileStatus> files = fs.listFiles(new Path("/"), true);
while(files.hasNext()){
LocatedFileStatus file = files.next();
Path filePath = file.getPath();
String fileName = filePath.getName();
System.out.println(fileName);
}
System.out.println("---------------------------------");
//listStatus 可以列出文件和文件夹的信息,但是不提供自带的递归遍历
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for(FileStatus status: listStatus){
String name = status.getPath().getName();
System.out.println(name + (status.isDirectory()?" is dir":" is file"));
}
}
/**
创建文件夹
@throws Exception
@throws IllegalArgumentException
*/
@Test
public void mkdir() throws IllegalArgumentException, Exception {
fs.mkdirs(new Path("/aaa/bbb/ccc"));
}
/**
删除文件或文件夹
@throws IOException
@throws IllegalArgumentException
*/
@Test
public void rm() throws IllegalArgumentException, IOException {
fs.delete(new Path("/aa"), true);
}
}
新建完成后如下图:
新建用户库,并添加外部jar文件
往hdfslib添加jar文件
由于HDFS的API只需要添加HDFS相关及hadoop通用jar包即可,包路径在E:\depslib\hadoop-2.4.1\share\hadoop下,这里添加HDFS jar包如下
同时还要添加hadoop通用jar包,路径为E:\depslib\hadoop-2.4.1\share\hadoop\common
为此,HDFS API开发相关JAR包已经添加完成,工程创建完成,下面可以进行HDFS相关API程序编写。
2、HDFS 客户端API代码测试
分享测试Demo代码如下:
package cn.qlq.hdfs;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.junit.Before;
import org.junit.Test;
public class HdfsUtil {
private FileSystem fs = null;
@Before
public void befor() throws IOException, InterruptedException, URISyntaxException{
//读取classpath下的xxx-site.xml 配置文件,并解析其内容,封装到conf对象中
Configuration conf = new Configuration();
//也可以在代码中对conf中的配置信息进行手动设置,会覆盖掉配置文件中的读取的值
conf.set("fs.defaultFS", "hdfs://localhost:9000/");
//根据配置信息,去获取一个具体文件系统的客户端操作实例对象
fs = FileSystem.get(new URI("hdfs://localhost:9000/"),conf,"root");
}
/**
上传文件,比较底层的写法
@throws Exception
*/
@Test
public void upload() throws Exception {
Path dst = new Path("hdfs://localhost:9000/aa/qingshu.txt");
FSDataOutputStream os = fs.create(dst);
FileInputStream is = new FileInputStream("/opt/download/haha.txt");
IOUtils.copy(is, os);
}
/**
上传文件,封装好的写法
@throws Exception
@throws IOException
*/
@Test
public void upload2() throws Exception, IOException{
fs.copyFromLocalFile(new Path("/opt/download/haha.txt"), new Path("hdfs://localhost:9000/aa/qingshu2.txt"));
}
/**
download file
@throws IOException
*/
@Test
public void download() throws IOException {
Path path = new Path("hdfs://localhost:9000/jdk-7u65-linux-i586.tar.gz");
FSDataInputStream input = fs.open(path);
FileOutputStream output = new FileOutputStream("/opt/download/doload.tgz");
IOUtils.copy(input, output);
}
/**
下载文件
@throws Exception
@throws IllegalArgumentException
*/
@Test
public void download2() throws Exception {
fs.copyToLocalFile(new Path("hdfs://localhost:9000/aa/qingshu2.txt"), new Path("/opt/download/haha2.txt"));
}
/**
查看文件信息
@throws IOException
@throws IllegalArgumentException
@throws FileNotFoundException
*/
@Test
public void listFiles() throws FileNotFoundException, IllegalArgumentException, IOException {
// listFiles列出的是文件信息,而且提供递归遍历
RemoteIterator<LocatedFileStatus> files = fs.listFiles(new Path("/"), true);
while(files.hasNext()){
LocatedFileStatus file = files.next();
Path filePath = file.getPath();
String fileName = filePath.getName();
System.out.println(fileName);
}
System.out.println("---------------------------------");
//listStatus 可以列出文件和文件夹的信息,但是不提供自带的递归遍历
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for(FileStatus status: listStatus){
String name = status.getPath().getName();
System.out.println(name + (status.isDirectory()?" is dir":" is file"));
}
}
/**
创建文件夹
@throws Exception
@throws IllegalArgumentException
*/
@Test
public void mkdir() throws IllegalArgumentException, Exception {
fs.mkdirs(new Path("/aaa/bbb/ccc"));
}
/**
删除文件或文件夹
@throws IOException
@throws IllegalArgumentException
*/
@Test
public void rm() throws IllegalArgumentException, IOException {
fs.delete(new Path("/aa"), true);
}
}
相关文章推荐
- Hadoop Java API 操作 hdfs--1
- JAVA操作HDFS API(hadoop)
- 使用Hadoop API操作HDFS文件时遇到的问题及解决总结
- hadoop中hdfs对文件的操作Api
- Hadoop学习记录(3)|HDFS API 操作|RPC调用
- hadoop的hdfs中的javaAPI操作
- hadoop系列之五JavaAPI操作HDFS文本系统
- 第二篇:Hadoop HDFS常用JAVA api操作程序
- hadoop hdfs api基本操作
- 和我一起学Hadoop(四):java客户端操作HDFS
- JAVA操作HDFS API(hadoop)
- 使用Hadoop提供的API操作HDFS
- hadoop源码 - HDFS的文件操作流 写操作(客户端)
- hadoop学习;hdfs操作;运行抛出权限异常: Permission denied;api查看源码方法;源码不停的向里循环;抽象类通过debug查找源码
- Hadoop(四)HDFS的高级API操作
- JAVA操作HDFS API(hadoop) HDFS API详解
- HDFS的API调用,创建Maven工程,创建一个非Maven工程,HDFS客户端操作数据代码示例,文件方式操作和流式操作
- Hadoop学习二(java api调用操作HDFS)
- JAVA操作HDFS API(hadoop)
- Hadoop之旅(8)— HDFS API 实战操作