在上一节的学习中,我们认识了HDFS的结构,知道了HDFS的优点:适合大数据处理,无论是数据规模还是文件规模。当然也有他的缺点:不适合低延时数据访问,比如毫秒级的数据存储,那做不到,不适合对大量小文件进行存储,因为会占用namenode大量的内存存储,一个文件也只有一个写,不允许多个线程同时写。接下来,我们就用JavaAPI操作一下HDFS吧。
hdfs客户端环境准备
1、根据自己电脑的操作系统拷贝对应的编译后的hadoop jar包到非中文路径
2、配置HADOOP_HOME环境变量
3、创建一个Maven工程HdfsClientDemo
4、导入相应的依赖坐标
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
</dependencies>
5、添加日志,需要在项目的src/main/resources目录下,新建一个文件,命名为“log4j.properties”,在文件中填入
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
6、创建一个类,编写如下代码实现从hdfs上创建一个目录
public class HDFSClient {
public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {
// 1 获取文件系统
Configuration configuration = new Configuration();
// 配置在集群上运行
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"), configuration, "root");
// 2 创建目录
fs.mkdirs(new Path("/0331/test/creatDir"));
// 3 关闭资源
fs.close();
}
}
7、运行http://hadoop100:50070/explorer.html#/,查看目录创建情况,如下
文件上传
把d盘下 test.txt 文件上传到集群根目录下
1、编写代码
@Test
public void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {
// 1 获取文件系统
Configuration configuration = new Configuration();
// 设置副本数,默认为3
configuration.set("dfs.replication", "2");
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"), configuration, "root");
// 2 上传文件
fs.copyFromLocalFile(new Path("d:/test.txt"), new Path("/test.txt"));
// 3 关闭资源
fs.close();
System.out.println("完成");
}
2、刷新页面,查看上传情况
3、参数优先级
这里指的是一些参数配置,比如代码里的副本数,这个参数如果不设置,用的配置是服务器的配置,这些参数我们可以在代码修改,也可以在classpath下创建配置文件指定,但配置是有优先级的:(1)代码中设置的值(最先生效) >(2)ClassPath下的用户自定义配置文件 >(3)然后是服务器的默认配置。如果你对大数据开发感兴趣,想系统学习大数据的话,可以加入大数据技术学习交流扣扣君羊:522189307
文件下载
把集群根目录下的 test.txt 下载到d盘根目录
1、代码编写
@Test
public void testCopyToLocalFile() throws IOException, InterruptedException, URISyntaxException {
// 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"), configuration, "root");
// 2 执行下载操作
// boolean delSrc 指是否将原文件删除
// Path src 指要下载的文件路径
// Path dst 指将文件下载到的路径
// boolean useRawLocalFileSystem 是否开启文件校验
fs.copyToLocalFile(false, new Path("/test.txt"), new Path("d:/a.txt"), true);
// 3 关闭资源
fs.close();
}
2、查看d盘根目录下生成的文件
文件夹删除
删除0331文件夹(0331是自定义的,作者是取自今天日期)
1、代码编写
@Test
public void testDelete() throws IOException, InterruptedException, URISyntaxException {
// 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"), configuration, "root");
// 2 执行删除
fs.delete(new Path("/0331/"), true);
// 3 关闭资源
fs.close();
}
2、查看hadoop管理页面,是否删除了文件夹。
文件更名
把test.txt文件名称改为b.txt
1、代码编写
@Test
public void testRename() throws IOException, InterruptedException, URISyntaxException{
// 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"), configuration, "root");
// 2 修改文件名称
fs.rename(new Path("/test.txt"), new Path("/b.txt"));
// 3 关闭资源
fs.close();
}
2、查看hadoop管理页面,是否更名成功。
查看文件详情
查看文件名称,权限,长度,块信息
1、代码编写
@Test
public void testListFiles() throws IOException, InterruptedException, URISyntaxException {
// 1获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"), configuration, "root");
// 2 获取文件详情
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
while (listFiles.hasNext()) {
LocatedFileStatus status = listFiles.next();
// 输出详情
// 文件名称
System.out.println(status.getPath().getName());
// 长度
System.out.println(status.getLen());
// 权限
System.out.println(status.getPermission());
// 分组
System.out.println(status.getGroup());
// 获取存储的块信息
BlockLocation[] blockLocations = status.getBlockLocations();
for (BlockLocation blockLocation : blockLocations) {
// 获取块存储的主机节点
String[] hosts = blockLocation.getHosts();
for (String host : hosts) {
System.out.println(host);
}
}
System.out.println("-----------分割线----------");
}
// 3 关闭资源
fs.close();
}
2、查看控制台日志打印
b.txt
9
rw-r--r--
supergroup
hadoop101
hadoop102
-----------分割线----------
文件和文件夹的判断
判断是文件还是文件夹
1、代码编写
@Test
public void testListStatus() throws IOException, InterruptedException, URISyntaxException {
// 1 获取文件配置信息
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"), configuration, "root");
// 2 判断是文件还是文件夹
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for (FileStatus fileStatus : listStatus) {
// 如果是文件
if (fileStatus.isFile()) {
System.out.println("f:" + fileStatus.getPath().getName());
} else {
System.out.println("d:" + fileStatus.getPath().getName());
}
}
// 3 关闭资源
fs.close();
}
2、查看控制台打印
d:0331 //文件夹
f:b.txt //文件
以上是文件与hdfs的交互,文件的来源可以是从mysql数据库中获取的数据,也可以是从消息中间件获取的数据。