HDFS连接JAVA,HDFS常用API
先在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的更多相关文章
- Java 之常用API(一)
常用API 1 API概述 2 Scanner类与String类 3 StringBuilder类 NO.one API概述 1.1 API概述 API(Application Programm ...
- Java之常用API
API概述 什么是API API (Application Programming Interface) :应用程序编程接口 java中的API 指的就是 JDK 中提供的各种功能的 Java类,这些 ...
- Java 基础 常用API (System类,Math类,Arrays, BigInteger,)
基本类型包装类 基本类型包装类概述 在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的.而程序开发中,我们需要把字符串数据,根据需求转换成指定的基本数据类型,如年龄需要转换成int类 ...
- Java 基础 常用API (Object类,String类,StringBuffer类)
Java API Java 的API(API: Application(应用) Programming(程序) Interface(接口)) Java API就是JDK中提供给我们使用的类,这些类将底 ...
- Java 之常用API(二)
Object类 & System类 日期相关类 包装类 & 正则表达式 Object类 & System类 1.1 Object类 1.1.1 概述 Object类是Java语 ...
- java selenium常用API(WebElement、iFrame、select、alert、浏览器窗口、事件、js) 一
WebElement相关方法 1.点击操作 WebElement button = driver.findElement(By.id("login")); button.clic ...
- java自学-常用api
API(Application Programming Interface),应用程序编程接口.Java API是JDK中提供给我们使用的类的说明文档.即jdk包里边写好的类,这些类将底层的代码实现封 ...
- Java的常用API
Object类 1.toString方法在我们直接使用输出语句输出对象的时候,其实通过该对象调用了其toString()方法. 2.equals方法方法摘要:类默认继承了Object类,所以可以使用O ...
- Java的常用API之System类简介
Syetem类 java.lang.System类中提供了大量的静态方法,可以获取与系统相关的信息或系统级操作,在System类的API文档中,常用的方法有: public static long c ...
随机推荐
- hive 常用日期格式转换
固定日期转换成时间戳select unix_timestamp('2016-08-16','yyyy-MM-dd') --1471276800select unix_timestamp('201608 ...
- django之“static”全局设置
1. 首先要配置静态文件路径(这些文件不输入任何app):. # 设置静态文件读取路径(这些静态文件不属于任何app) STATICFILES_DIRS = [ os.path.join(BASE_D ...
- markdown介绍和使用(超全建议收藏)
Markdown介绍 Markdown 其实在 2004 年就有了,不过之前一直很小众,这几年随着相关应用平台的发展,Markdown以其独到的优势迅速火起来了.Markdown编辑器使用一套格式标记 ...
- react之每日一更(实现canvas拖拽,增、删、改拖拽模块大小功能)
效果图: import React, { Component } from 'react'; import scaleImage from './images/scale.png'; import c ...
- JS Map与Set
笔记整理自:廖雪峰老师的JS教程 Map JavaScript的对象有个小问题,就是键必须是字符串.但实际上Number或者其他数据类型作为键也是非常合理的. 为了解决这个问题,最新的ES6规范引入了 ...
- java中args是什么意思?
1. 字符串变量名(args)属于引用变量,名字代号而已,可以自己取的. 2.总的来说就是个存放字符串数组用的, 去掉就不知道 "args" 声明的变量是什么类型了. 3.如果有 public sta ...
- Lua非常有用的工具——递归打印表数据
摘要: Lua是一种非常小巧的语言.虽小,但五脏俱全. 在Lua中,我认为最最核心的数据结构就是表.表不仅可用作数组,还可以用作字典.Lua面向对象的实现也是用表实现的. 表对于Lua实在是太重要了! ...
- @play.data.binding.NoBinding
新的@play.data.binding.NoBinding注解允许我们定义一些"不应该被绑定"的字段,以防出现安全问题.例如: public class User extends ...
- 列出ubuntu软件管理工具apt的一些用法(自由总结)
安装软件包 [root@CentOS7 ~]#apt install tree 删除软件包 [root@CentOS7 ~]# apt remove tree 列出仓库软件包 [root@CentOS ...
- 6 小时 Python 入门
6 小时 Python 入门 以下操作均在 Windows 环境下进行操作,先说明一下哈 一.安装 Python 1.官网下载 Python 进入官网(https://www.python.org), ...