先在pom.xml中导入依赖包

<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.6</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.6</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.6</version>
</dependency> <!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.3</version>
</dependency> </dependencies>

hdfs连接Java

1.先获取配置文件(hdfs-site.xml)

Configuration cg = new Configuration();  导入的是import org.apache.hadoop.conf.Configuration;
cg.set("dfs.replication","1"); 1是备份数量

2.获取连接地址(core-site.xml)

URI uri = new URI("hdfs://master:9000");

3.创建(获取)hdfs文件管理系统的对象,同过对象操作hdfs

FileSystem fs = FileSystem.get(uri, cg);

常用HDFSAPi:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Before;
import org.junit.Test; import java.io.*;
import java.net.URI; public class HdfsApi {
FileSystem fs; @Before
public void main() throws Exception {
Configuration cg = new Configuration();
cg.set("dfs.replication", "1");
URI uri = new URI("hdfs://master:9000");
fs = FileSystem.get(uri, cg); } @Test
public void mk() throws IOException {
boolean mk = fs.mkdirs(new Path("/test"));
} @Test
public void del() throws IOException {
//false表示不迭代删除也可以不加,true可以进行多目录迭代删除
boolean del = fs.delete(new Path("/test"),false);
System.out.println(del); } @Test
public void listStatus() throws IOException { //对比图在下
//查看目录下文件列表
FileStatus[] fileStatuses = fs.listStatus(new Path("/data/data"));
System.out.println(fileStatuses); //[Lorg.apache.hadoop.fs.FileStatus;@106cc338
System.out.println("-------------------------------");
for (FileStatus fileStatus : fileStatuses) {
System.out.println(fileStatus.getLen()); //文件大小 以B字节为单位
System.out.println(fileStatus.getReplication()); //副本个数
System.out.println(fileStatus.getPermission()); //读写状态
System.out.println(fileStatus.getBlockSize()); //固定的一个block大小128MB
System.out.println(fileStatus.getAccessTime()); //创建文件时的时间戳
System.out.println(fileStatus.getPath()); //文件路径
System.out.println("-----------------");
}
/*输出结果
log4j:WARN No appenders could be found for logger (org.apache.hadoop.util.Shell).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
[Lorg.apache.hadoop.fs.FileStatus;@106cc338
-------------------------------
180
1
rw-r--r--
134217728
1631969331482
hdfs://master:9000/data/data/cource.txt
-----------------
138540
1
rw-r--r--
134217728
1631963067585
hdfs://master:9000/data/data/score.txt
-----------------
41998
1
rw-r--r--
134217728
1631963067890
hdfs://master:9000/data/data/students.txt
-----------------
*/ } @Test
public void listBlockLocation() throws IOException { //对比图在下
BlockLocation[] fbl =
fs.getFileBlockLocations(
new Path("/data/data/students.txt"),0,1000000000);
for (BlockLocation bl : fbl) {
String[] hosts = bl.getHosts();
for (String host : hosts) {
System.out.println(host);
} //node1 表示文件存在node1,因为文件小于一个block,所以这里只存在一个节点 System.out.println(bl.getLength()); //41998 size大小 String[] names = bl.getNames();
for (String name : names) {
System.out.println(name);
} //192.168.163.120:50010 node1的地址 System.out.println(bl.getOffset()); //0 偏移量 String[] topologyPaths = bl.getTopologyPaths();
for (String topologyPath : topologyPaths) {
System.out.println(topologyPath);
} // /default-rack/192.168.163.120:50010 } } @Test
public void open() throws IOException {
FSDataInputStream open = fs.open(new Path("/data/data/students.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(open)); //因为文件中有中文,所以将字节流转为字符流来读取
String len;
while ((len=br.readLine())!=null){
System.out.println(len);
}
br.close();
} @Test
public void create() throws IOException {
FSDataOutputStream fos = fs.create(new Path("/data/data/test.txt"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
bw.write("你好");
bw.write("世界");
bw.newLine();
bw.write("我和我的祖国");
bw.flush();
bw.close(); } }

HDFS连接JAVA,HDFS常用API的更多相关文章

  1. Java 之常用API(一)

    常用API  1 API概述  2 Scanner类与String类  3 StringBuilder类 NO.one API概述 1.1 API概述 API(Application Programm ...

  2. Java之常用API

    API概述 什么是API API (Application Programming Interface) :应用程序编程接口 java中的API 指的就是 JDK 中提供的各种功能的 Java类,这些 ...

  3. Java 基础 常用API (System类,Math类,Arrays, BigInteger,)

    基本类型包装类 基本类型包装类概述 在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的.而程序开发中,我们需要把字符串数据,根据需求转换成指定的基本数据类型,如年龄需要转换成int类 ...

  4. Java 基础 常用API (Object类,String类,StringBuffer类)

    Java API Java 的API(API: Application(应用) Programming(程序) Interface(接口)) Java API就是JDK中提供给我们使用的类,这些类将底 ...

  5. Java 之常用API(二)

    Object类 & System类 日期相关类 包装类 & 正则表达式 Object类 & System类 1.1 Object类 1.1.1 概述 Object类是Java语 ...

  6. java selenium常用API(WebElement、iFrame、select、alert、浏览器窗口、事件、js) 一

     WebElement相关方法 1.点击操作 WebElement button = driver.findElement(By.id("login")); button.clic ...

  7. java自学-常用api

    API(Application Programming Interface),应用程序编程接口.Java API是JDK中提供给我们使用的类的说明文档.即jdk包里边写好的类,这些类将底层的代码实现封 ...

  8. Java的常用API

    Object类 1.toString方法在我们直接使用输出语句输出对象的时候,其实通过该对象调用了其toString()方法. 2.equals方法方法摘要:类默认继承了Object类,所以可以使用O ...

  9. Java的常用API之System类简介

    Syetem类 java.lang.System类中提供了大量的静态方法,可以获取与系统相关的信息或系统级操作,在System类的API文档中,常用的方法有: public static long c ...

随机推荐

  1. Lesson3——Pandas Series结构

    1 什么是Series结构? Series 结构,也称 Series 序列,是 Pandas 常用的数据结构之一,它是一种类似于一维数组的结构,由一组数据值(value)和一组标签组成,其中标签与数据 ...

  2. Linux性能优化实战(一)

    一.优化方向 1,性能指标 从应用负载的视角出发,考虑"吞吐"和"延时" 从系统资源的视角出发,考虑资源使用率.饱和度等 2,性能优化步骤 选择指标评估应用程序 ...

  3. chmod 权限-rw-r--r--表示什么含义

    感谢原文作者:bugcoder321 原文链接:https://blog.csdn.net/li_canhui/article/details/89452134 在linux中,有时候可以看到一个文件 ...

  4. python-字符串操作分类小结

    切片 str[start:end:step] # 包括头,不包括尾巴.step为步长,意思是每隔step-1个元素,取一个字符 [::-1] #反向取字符串,实现字符串的反转 "abcde& ...

  5. sql注入,xss攻击,csrf(模拟请求),防盗链

    如何防止别人模拟请求? 使用令牌token解决模拟请求  好处是 唯一性只能有一次请求 已经拿到生成的token  如何防止呢?  怎样防止非人工? 使用验证码 xss攻击? xss攻击也叫脚本注入 ...

  6. 重力感应 加速计- By严焕培

    //  加速计-传统用法 // //  Created by 严焕培 on 15-05-19. //  Copyright (c) 2015年 sibu. All rights reserved. / ...

  7. YOLOv5模型训练及检测

    一.为什么使用YOLOv5 二.软件工具 2.1 Anaconda https://www.anaconda.com/products/individual 2.2 PyCharm https://w ...

  8. Python实现不带头结点的单链表

    1 # 创建一个节点类 2 class Node: 3 def __init__(self, item): 4 self.item = item 5 self.next = None 6 7 8 # ...

  9. 4、前端--浮动、定位、是否脱离文档流、溢出属性、z-index、透明度、JavaScript简介

    浮动 # ps:html代码时没有缩进一说的 全部写在一行也可以 """浮动主要就是用于页面布局的!!!""" # 浮动带来的负面影响 &q ...

  10. 【Kotlin】初识Kotlin之面向对象

    [Kotlin]初识Kotlin之面向对象 1.类 在Kotlin中,类用关键字class来定义 如果一个类具有类体,那么需要使用{ }来写类体内容,如果不需要类体,那么只需要定义类名就可以了 // ...