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】

hadoop3自学入门笔记(3)-java 操作hdfs的更多相关文章

  1. hadoop3自学入门笔记(2)—— HDFS分布式搭建

    一些介绍 Hadoop 2和Hadoop 3的端口区别 Hadoop 3 HDFS集群架构 我的集群规划 name ip role 61 192.168.3.61 namenode,datanode ...

  2. hadoop3自学入门笔记(1)——虚拟机安装和网络配置

    前言 年过30惶惶不安,又逢疫情,还是不断学习,强化自己的能力.hadoop的视频和书籍在15年的时候就看过,但是一直没动手实践过,要知道技术不经过实战,一点提升也没有.因此下定决心边学边做,希望能有 ...

  3. hadoop集群配置和在windows系统上运用java操作hdfs

    安装 配置 概念 hadoop常用shell命令 使用java操作hadoop 本文介绍hadoop集群配置和在windows系统上运用java操作hdfs 安装 http://mirror.bit. ...

  4. hadoop学习(三)HDFS常用命令以及java操作HDFS

    一.HDFS的常用命令 1.查看根目录下的信息:./hadoop dfs -ls 2.查看根目录下的in目录中的内容:./hadoop dfs -ls in或者./hadoop dfs -ls ./i ...

  5. java操作hdfs实例

    环境:window7+eclipse+vmware虚拟机+搭建好的hadoop环境(master.slave01.slave02) 内容:主要是在windows环境下,利用eclipse如何来操作hd ...

  6. 使用java操作HDFS

    新建Java Project; 1,右击项目,属性,Java Build Path,Libraries,Add External JARs(haddopp根目录下的所以jar): 2,做一下项目关联, ...

  7. Mongodb快速入门之使用Java操作Mongodb

    [IT168 专稿]在上一篇文章中,我们学习了Mongodb的安装和初步使用,在本文中,将学习如何使用Java去编程实现对Mongodb的操作. HelloWorld程序 学习任何程序的第一步,都是编 ...

  8. Mongodb入门并使用java操作Mongodb

    转载请注意出处:http://blog.csdn.net/zcm101 最近在学习NoSql,先从Mongodb入手,把最近学习的总结下. Mongodb下载安装 Mongodb的下载安装就不详细说了 ...

  9. java操作hdfs到数据库或者缓存

    使用hadoop工具将数据分析出来以后,须要做入库处理或者存到缓存中.不然就没了意义 一下是使用javaAPI操作hdfs存入缓存的代码: <span style="font-fami ...

随机推荐

  1. c#数字图像处理(三)灰度直方图

    灰度直方图是灰度的函数,描述的是图像中具有该灰度级的像素的个数.如果用直角坐标系来表示,则它的横坐标是灰度级,纵坐标是该灰度出现的概率(像素的个数). using System; using Syst ...

  2. AntV F2 数据可视化填坑,图表横向滚动

    柱状图横向滚动 思路 通过 Interaction 实现平移,通过 ScrollBar 显示滚动条 1.Interaction F2 提供一套交互机制,以达到通用交互行为的封装和复用.基于此机制,我们 ...

  3. 线性最长cover(无讲解)

    #include<bits/stdc++.h> using namespace std; ; int n,f[maxn],cover[maxn],R[maxn]; char str[max ...

  4. Android教程2020 - RecyclerView实际使用

    示例,用RecyclerView的item做出一个列表. Android教程2020 - 系列总览 本文链接 前面我们已经知道如何用RecyclerView显示一列数据.这里我们做出一个具体的例子.尽 ...

  5. Java8新特性一点通 | 回顾字符转日期&JoinArray使用

    StringToDate日期转换 Join Array使用 StringToDate日期转换 Convert string to date in ISO8601 format 利用LocalDate. ...

  6. centos7.2 部署k8s集群

    一.背景 二.使用范围 ♦ 测试环境及实验环境 三.安装前说明 ♦ k8s网络基本概念 ♦  集群规划图 ♦ 软件版本选取 Name Version Description docker-ce 18. ...

  7. 按照相应的格式获取系统时间并将其转化为SQL中匹配的(date)时间格式

    在获取时间时需要对时间格式进行设置,此时就需要用到SimpleDateFormat 类 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM ...

  8. 数学建模之优劣解距法(TOPSIS)

    优劣解距法简称TOPSIS,是一种综合评价方法,利用原始数据反映各评价方案之间的差距 优劣解距法的步骤通常为: 先将原始数据针具做正向化处理,得到正向化矩阵 再对正向化矩阵标准化处理以消除各指标纲量的 ...

  9. linux下的 $ ./configure $ sudo make && sudo make install

    $ ./configure $ sudo make && sudo make install 1)./configure命令就是执行当前目录的名为configure的脚本,主要的作用是 ...

  10. Windows虚拟器的安装与使用

    前言 先说:你们要的东西我会放在文中和文章末尾,拿文件记得点赞,一手交钱一手交货 马上初中朋友们就要开始上网课了哎!(博主也是一位初中生) 有人可能会受网课的影响而不能……, 所以各路神仙研究出了各种 ...