1.core-site.xml

<configuration>
<property>
<name>fs.defaultFS</name>
<value>hdfs://192.168.3.61:9820</value>
</property>
<property>
<name>hadoop.tmp.dir</name>
<value>/opt/hadoopdata</value>
</property>
</configuration>

2.pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.qmkj</groupId>
<artifactId>hdfsclienttest</artifactId>
<version>0.1</version> <name>hdfsclienttest</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs-client -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs-client</artifactId>
<version>3.2.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>3.2.1</version>
</dependency> </dependencies> <build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

3.测试代码

package com.qmkj;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Before;
import org.junit.Test; import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI; /**
* Unit test for simple App.
*/
public class AppTest {
FileSystem fs = null; @Before
public void init() throws Exception { Configuration conf = new Configuration();
//设立的设置url请注意,设置core-site.xml中配置fs.defaultFS的地址
fs = FileSystem.get(new URI("hdfs://192.168.3.61:9820"), conf, "root"); } @Test
public void testAdd() throws Exception {
fs.copyFromLocalFile(new Path("D:\\KK_Movies\\kk 2020-02-20 20-45-55.mp4"), new Path("/zhanglei"));
fs.close();
} /**
* 从hdfs中复制文件到本地文件系统
*
* @throws IOException
* @throws IllegalArgumentException
*/
@Test
public void testDownloadFileToLocal() throws IllegalArgumentException, IOException { // fs.copyToLocalFile(new Path("/mysql-connector-java-5.1.28.jar"), new
// Path("d:/"));
fs.copyToLocalFile(false, new Path("test.txt"), new Path("e:/"), true);
fs.close(); } /**
* 目录操作
*
* @throws IllegalArgumentException
* @throws IOException
*/
@Test
public void testMkdirAndDeleteAndRename() throws IllegalArgumentException, IOException { // 创建目录
fs.mkdirs(new Path("/zhanglei/b1/c1")); // 删除文件夹 ,如果是非空文件夹,参数2必须给值true ,删除所有子文件夹
fs.delete(new Path("/b1"), true); // 重命名文件或文件夹
fs.rename(new Path("/zhanglei"), new Path("/qmkj")); } /**
* 查看目录信息,只显示文件
*
* @throws IOException
* @throws IllegalArgumentException
* @throws FileNotFoundException
*/
@Test
public void testListFiles() throws FileNotFoundException, IllegalArgumentException, IOException { RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true); while (listFiles.hasNext()) { LocatedFileStatus fileStatus = listFiles.next(); System.out.println(fileStatus.getPath().getName());
System.out.println(fileStatus.getBlockSize());
System.out.println(fileStatus.getPermission());
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 host : hosts) {
System.out.println(host);
} } System.out.println("--------------打印的分割线--------------"); } } /**
* 查看文件及文件夹信息
*
* @throws IOException
* @throws IllegalArgumentException
* @throws FileNotFoundException
*/
@Test
public void testListAll() throws FileNotFoundException, IllegalArgumentException, IOException {
//可以右击方法名,Run 测试一下。
FileStatus[] listStatus = fs.listStatus(new Path("/")); String flag = "";
for (FileStatus fstatus : listStatus) { if (fstatus.isFile()) {
flag = "f-- ";
} else {
flag = "d-- ";
}
System.out.println(flag + fstatus.getPath().getName());
System.out.println(fstatus.getPermission()); } } }

testDownloadFileToLocal 这里测试请注意,本地也要装hdfs才可以

更多精彩请关注公众号【lovepythoncn】

最新文章

  1. 2000条你应知的WPF小姿势 基础篇&lt;69-73 WPF Freeze机制和Template&gt;
  2. Java接口和抽象类的区别
  3. L#脚本语言,直接把DLL当脚本执行(图解说明)
  4. BAT for循环
  5. HDU(2485),最小割最大流
  6. java: cannot execute binary file
  7. 如何在Root的手机上开启ViewServer,使得HierachyViewer能够连接
  8. 使用mysql-mmm实现MySQL高可用集群
  9. wuzhi 五指 基本知识
  10. Jquery实现的几款漂亮的时间轴
  11. jq-toggle
  12. laravel MethodNotAllowedHttpException错误一个原因
  13. Java连接FTP成功,但是上传是失败,报错:Connected time out
  14. JAVA代码规范笔记(下)
  15. androd输入管理系统机制解析
  16. python获取日期加减之后的日期
  17. 【转】Redis学习笔记(四)如何用Redis实现分布式锁(1)—— 单机版
  18. svn 的权限配置
  19. Unity3D Shader 入门
  20. Redis缓存相关问题总结

热门文章

  1. Browser Security-同源策略、伪URL的域
  2. 团队项目——Beta冲刺
  3. 找不到文件异常java.io.IOException: Resource [classpath:shiro.ini] could not be found.
  4. netty EventLoop线程与当前线程的问题
  5. 浅析YYCache
  6. json通过后台获取数据库中的内容,并在前端进行显示
  7. centos最小化安装时网络配置
  8. Win2008 远程时提示&quot;要登录到此远程计算机,您必须被授予允许通过终端登录登录的权限&quot;的解决方法
  9. 啥?你想diy一个智能音箱,来吧
  10. ubuntu 14.04 如何设置静态ip