Hadoop HDFS (3) JAVA訪问HDFS
这个类是用来跟Hadoop的文件系统进行交互的。尽管我们这里主要是针对HDFS。可是我们还是应该让我们的代码仅仅使用抽象类FileSystem。这样我们的代码就能够跟不论什么一个Hadoop的文件系统交互了。在写測试代码时,我们能够用本地文件系统測试,部署时使用HDFS。仅仅需配置一下,不须要改动代码了。
用Hadoop URL来读取HDFS里的文件
- InputStream in = null;
- try {
- in = new URL("hdfs://host/path").openStream();
- //操作输入流in。能够读取到文件的内容
- } finally {
- IOUtils.closeStream(in);
- }
- import java.io.InputStream;
- import java.net.URL;
- import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
- import org.apache.hadoop.io.IOUtils;
- public class URLCat {
- static {
- URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
- }
- public static void main(String[] args) throws Exception {
- InputStream in = null;
- try {
- in = new URL(args[0]).openStream();
- IOUtils.copyBytes(in, System.out, 4096, false);
- } finally {
- IOUtils.closeStream(in);
- }
- }
- }
用FileSystem(org.apache.hadoop.fs.FileSystem)类来读取HDFS里的文件
- public static FileSystem get(Configuration conf) throws IOException;
- public static FileSystem get(URI uri, Configuration conf) throws IOException;
- public static FileSystem get(final URI uri, final Configuration conf, final String user) throws IOException, InterruptedException;
- public static LocalFileSystem getLocal(Configuration conf) throws IOException;
- public FSDataInputStream open(Path f) throws IOException;
- public abstract FSDataInputStream open(Path f, int bufferSize) throws IOException;
- import java.net.URI;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.fs.FSDataInputStream;
- import org.apache.hadoop.fs.FileSystem;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.IOUtils;
- public class FileSystemCat {
- public static void main(String[] args) throws Exception {
- String uri = args[0];
- Configuration conf = new Configuration();
- FileSystem fs = FileSystem.get(URI.create(uri), conf);
- //System.out.println(fs.getClass().getName()); //这里能够看到得到的实例是DistributedFileSystem,由于core-site.xml里配的是hdfs
- FSDataInputStream in = null;
- try {
- in = fs.open(new Path(uri));
- IOUtils.copyBytes(in, System.out, 4096, false);
- } finally {
- IOUtils.closeStream(in);
- }
- }
- }
- in.seek(0);
- IOUtils.copyBytes(in, System.out, 4096, false);
- public int read(long position, byte[] buffer, int offset, int length) throws IOException;
- public void readFully(long position, byte[] buffer, int offset, int length) throws IOException;
- public void readFully(long position, byte[] buffer) throws IOException;
用FileSystem类来向HDFS里写文件
- public FSDataOutputStream create(Path f) throws IOException;
- public FSDataOutputStream create(Path f, Progressable progress) throws IOException;
- public interface Progressable {
- public void progress();
- }
- public FSDataOutputStream append(Path f) throws IOException;
- import java.io.BufferedInputStream;
- import java.io.FileInputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.URI;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.fs.FileSystem;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.IOUtils;
- import org.apache.hadoop.util.Progressable;
- public class FileCopyWithProgress {
- public static void main(String[] args) throws Exception {
- String localSrc = args[0];
- String dst = args[1];
- InputStream in = new BufferedInputStream(new FileInputStream(localSrc));
- Configuration conf = new Configuration();
- FileSystem fs = FileSystem.get(URI.create(dst), conf);
- OutputStream out = fs.create(new Path(dst), new Progressable() {
- @Override
- public void progress() {
- System.out.print(".");
- // try {
- // Thread.sleep(1000);
- // } catch (Exception e) {
- // e.printStackTrace();
- // }
- }
- });
- IOUtils.copyBytes(in, out, 4096, true);
- System.out.println();
- System.out.println("end.");
- }
- }
- public long getPos() throws IOException;
创建文件夹
查询文件元信息:FileStatus(org.apache.hadoop.fs.FileStatus)
- import static org.junit.Assert.*;
- import static org.hamcrest.CoreMatchers.*;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.OutputStream;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.fs.FileStatus;
- import org.apache.hadoop.fs.FileSystem;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.hdfs.MiniDFSCluster;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
- public class ShowFileStatusTest {
- private static final String SYSPROP_KEY = "test.build.data";
- /** MiniDFSCluster类在hadoop-hdfs-2.4.1-tests.jar中,是一个专门用于測试的in-process HDFS集群 */
- private MiniDFSCluster cluster;
- private FileSystem fs;
- @Before
- public void setUp() throws IOException {
- Configuration conf = new Configuration();
- String sysprop = System.getProperty(SYSPROP_KEY);
- if (sysprop == null) {
- System.setProperty(SYSPROP_KEY, "/tmp");
- }
- cluster = new MiniDFSCluster(conf, 1, true, null);
- fs = cluster.getFileSystem();
- OutputStream out = fs.create(new Path("/dir/file"));
- out.write("content".getBytes("UTF-8"));
- out.close();
- }
- @After
- public void tearDown() throws IOException {
- if (fs != null) {
- fs.close();
- }
- if (cluster != null) {
- cluster.shutdown();
- }
- }
- @Test(expected = FileNotFoundException.class)
- public void throwsFileNotFoundForNonExistentFile() throws IOException {
- fs.getFileStatus(new Path("no-such-file"));
- }
- @Test
- public void fileStatusForFile() throws IOException {
- Path file = new Path("/dir/file");
- FileStatus stat = fs.getFileStatus(file);
- assertThat(stat.getPath().toUri().getPath(), is("/dir/file"));
- assertThat(stat.isDirectory(), is(false));
- assertThat(stat.getLen(), is(7L));
- assertTrue(stat.getModificationTime() <= System.currentTimeMillis());
- assertThat(stat.getReplication(), is((short)1));
- assertThat(stat.getBlockSize(), is(64 * 1024 * 1024L));
- assertThat(stat.getOwner(), is("norris"));
- assertThat(stat.getGroup(), is("supergroup"));
- assertThat(stat.getPermission().toString(), is("rw-r--r--"));
- }
- @Test
- public void fileStatusForDirectory() throws IOException {
- Path dir = new Path("/dir");
- FileStatus stat = fs.getFileStatus(dir);
- assertThat(stat.getPath().toUri().getPath(), is("/dir"));
- assertThat(stat.isDirectory(), is(true));
- assertThat(stat.getLen(), is(0L));
- assertTrue(stat.getModificationTime() <= System.currentTimeMillis());
- assertThat(stat.getReplication(), is((short)0));
- assertThat(stat.getBlockSize(), is(0L));
- assertThat(stat.getOwner(), is("norris"));
- assertThat(stat.getGroup(), is("supergroup"));
- assertThat(stat.getPermission().toString(), is("rwxr-xr-x"));
- }
- }
Hadoop HDFS (3) JAVA訪问HDFS的更多相关文章
- Hadoop HDFS (3) JAVA訪问HDFS之二 文件分布式读写策略
先把上节未完毕的部分补全,再剖析一下HDFS读写文件的内部原理 列举文件 FileSystem(org.apache.hadoop.fs.FileSystem)的listStatus()方法能够列出一 ...
- Hadoop-2.6.0上的C的API訪问HDFS
在通过Hadoop-2.6.0的C的API訪问HDFS的时候,编译和执行出现了不少问题,花费了几天的时间,上网查了好多的资料,最终还是把问题给攻克了 參考文献:http://m.blog.csdn.n ...
- HDFS简单介绍及用C语言訪问HDFS接口操作实践
一.概述 近年来,大数据技术如火如荼,怎样存储海量数据也成了当今的热点和难点问题,而HDFS分布式文件系统作为Hadoop项目的分布式存储基础,也为HBASE提供数据持久化功能,它在大数据项目中有很广 ...
- JAVA訪问URL
JAVA訪问URL: package Test; import java.io.BufferedReader; import java.io.IOException; import java.io.I ...
- Java 訪问权限控制:你真的了解 protected keyword吗?
摘要: 在一个类的内部,其成员(包含成员变量和成员方法)是否能被其它类所訪问,取决于该成员的修饰词:而一个类是否能被其它类所訪问,取决于该类的修饰词.Java的类成员訪问权限修饰词有四类:privat ...
- Cassandra数据库Java訪问
针对的时Cassandra 2.0 数据库 Java本地client訪问Cassandra,首先建立Javaproject,使用Maven进行管理. 引入依赖: <dependency> ...
- Hadoop学习(2)-java客户端操作hdfs及secondarynode作用
首先要在windows下解压一个windows版本的hadoop 然后在配置他的环境变量,同时要把hadoop的share目录下的hadoop下的相关jar包拷贝到esclipe 然后Build Pa ...
- HDFS的java接口——简化HDFS文件系统操作
今天闲来无事,于是把HDFS的基本操作用java写出简化程序出来给大家一些小小帮助! package com.quanttech; import org.apache.hadoop.conf.Conf ...
- 三国武将查询系统 //Java 訪问 数据库
import java.awt.*; import javax.swing.*; import java.awt.event.ActionListener; import java.awt.event ...
随机推荐
- Perl数组: shift, unshift, push, pop
pop pop函数会删除并返回数组的最后一个元素. .. ; $fred = pop(@array); # $fred变成9,@array 现在是(5,6,7,8) $barney = pop @ar ...
- OA系统权限管理设计方案学习
学习之:http://www.cnblogs.com/kivenhou/archive/2009/10/19/1586106.html 此为模型图: 据此写了sql语句: drop table if ...
- 详解函数声明VS函数表达式
函数声明 比方如下:1.我们以一个完整的语句以function开头,不加任何东西. 2.有一个函数名(add) 3.参数可带可不带(x,y) 4.有一个数体 满足以上要求的我们统称为函数声明! 附加小 ...
- rel=nofollow 是什么意思
nofollow是什么意思? nofollow是html标签的一个属性值,Google推荐使用nofollow,告诉机器(爬虫)无需追踪目标页,是指禁止蜘蛛爬行和传递权重,但是如果你是通过sitema ...
- [前端笔记]第三篇:JavaScript
JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理. 一.代码存放位置 J ...
- 03:计算(a+b)/c的值
总时间限制: 1000ms 内存限制: 65536kB 描述 给定3个整数a.b.c,计算表达式(a+b)/c的值,/是整除运算. 输入 输入仅一行,包括三个整数a.b.c, 数与数之间以一个空格 ...
- 将数据库字段从float修改为decimal
decimal(6,2) 可以表示0000.00~9999.99 alter table test modify aaa decimal(6,2); 则表里所有大于10000的数会被设置为9999.9 ...
- ubuntu下配置protobuf
http://blog.csdn.net/guoyilongedu/article/details/17093811 最近想研究protobuf ,尝试了很多次都没有成功,我用的是ubuntu,在虚拟 ...
- zip file 压缩文件
有时候我们希望 upload 文件后自动压缩, 可以节省空间. 可以使用微软提供的压缩代码 Install-Package System.IO.Compression.ZipFile -Version ...
- unity 基础学习 transform
unity 基础学习 transform 1.unity采用的是右手坐标系,X轴右手为+,Y轴向上为+,Z轴朝里为+; 但是我们从3D MAX中导入模型之后,发现轴向并没有遵从这个原理, 其实是 ...