hdfs api读写文写件个人练习

看下hdfs的读写原理,主要是打开FileSystem,获得InputStream or OutputStream;

那么主要用到的FileSystem类是一个实现了文件系统的抽象类,继承来自org.apache.hadoop.conf.Configured,并且实现了Close able接口,可以适用于如本地文件系统file://,ftp,hdfs等多种文件系统,所以呢

若是自己要实现一个系统可以通过继承这个类,做出相应的配置,并且实现相应的抽象方法;

public abstract class FileSystem extends Configured implements Closeable {
public static FileSystem get(Configuration conf) throws IOException {
return get(getDefaultUri(conf), conf);
}
public static URI getDefaultUri(Configuration conf) {
return URI.create(fixName(conf.get(FS_DEFAULT_NAME_KEY, DEFAULT_FS)));
}
public static FileSystem get(URI uri, Configuration conf) throws IOException {
String scheme = uri.getScheme();
String authority = uri.getAuthority();

if (scheme == null && authority == null) { // use default FS
return get(conf);
}

if (scheme != null && authority == null) { // no authority
URI defaultUri = getDefaultUri(conf);
if (scheme.equals(defaultUri.getScheme()) // if scheme matches default
&& defaultUri.getAuthority() != null) { // & default has authority
return get(defaultUri, conf); // return default
}
}

String disableCacheName www.xycheng178.com= String.format("fs.%s.impl.disable.cache", scheme);
if (conf.getBoolean(disableCacheName, false)) {
return createFileSystem(uri, conf);
}

return CACHE.get(uri, conf);
}
}
从部分源码看下,get方法根据conf获取具体的文件系统对象,,而get(uri,conf)方法基于uri和conf创建文件系统对象;

那么看一个简单的应用,用java的api打开一个文件,并且打印出来

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.Path;
public class OpenMethod {
public static void main(String args[]) throws Exception{
Configuration conf = new Configuration();
conf.set("fs.defaultFs", "hdfs://10.192.4.33:9000/");//配置conf
FileSystem fs = FileSystem.get(conf);//根据创建FileSystem对象
Path path = new Path("/home/overshow/hehe.txt");
FSDataInputStream fis = fs.open(path);
byte b[] = new byte[200];
int i = fis.read(b);
System.out.println(new String(b,0,i));

}

}
这里还有三个需要注意的类,一个是Path,一个是FSDataInputStream,一个是conf

let me show you something

first: Configuration

public class Configuration implements Iterable<Map.Entry<String,String>>,Writable {
public void set(String name, String value) {
set(name, value, www.ysyl157.com null);
}

/**
* Set the <code>value</code> of the <www.mcyllpt.com code>www.dasheng178.com name</code> property. If
* <code>name</code> is deprecated, it also sets the <code>value</code> to
* the keys that replace the deprecated key. Name will be trimmed before put
* into configuration.
*
* @param name property name.
* @param value property value.
* @param source the place that this configuration value came from
* (For debugging).
* @throws IllegalArgumentException when the value or name is null.
*/
}
这个类是作业的配置信息类,通过Configuration可以实现在多个mapper和多个reducer任务之间共享信息,所以任何作用的配置信息必须通过Configuration传递,该类实现了Iterable和Writable两个接口,首先Iterable是迭代出Configuration对象加载到内存中的所有name-value键值对。而Writable是为了实现hadoop框架要求的序列化,可以将内存中的name-value序列化到硬盘;其中的set方法设置Configuration的名称和链接;

而Path类继承了fs类,

public class Path implements www.michenggw.com/ Comparable {
private void checkPathArg( String path ) throws IllegalArgumentException {
// disallow construction of a Path from an empty string
if ( path == null ) {
throw new IllegalArgumentException(
"Can not create a Path from a null string");
}
if( path.length() =www.leyouzaixian2.com= 0 ) {
throw new IllegalArgumentException(
"Can not create a Path from an empty string");
}
}

/** Construct a path from a String. Path strings are URIs, but with
* unescaped elements and some additional normalization. */
public Path(String pathString) throws IllegalArgumentException {
checkPathArg( pathString );

// We can‘t use ‘new URI(String)‘ directly, since it assumes things are
// escaped, which we don‘t require of Paths.

// add a slash in front of paths with Windows drive letters
if (hasWindowsDrive(pathString) && pathString.charAt(0) != ‘/‘) {
pathString = "/" + pathString;
}

// parse uri components
String scheme = null;
String authority = null;

int start = 0;

// parse uri scheme, if any
int colon = pathString.indexOf(‘:‘);
int slash = pathString.indexOf(‘/‘);
if ((colon != -1) &&
((slash == -1) || (colon < slash))) { // has a scheme
scheme = pathString.substring(0, colon);
start = colon+1;
}

// parse uri authority, if any
if (pathString.startsWith("//", start) &&
(pathString.length()-start > 2)) { // has authority
int nextSlash = pathString.indexOf(‘/‘, start+2);
int authEnd = nextSlash > 0 ? nextSlash : pathString.length();
authority = pathString.substring(start+2, authEnd);
start = authEnd;
}

// uri path is the rest of the string -- query & fragment not supported
String path = pathString.substring(start, pathString.length());

initialize(scheme, authority, path, null);
}

/**
* Construct a path from a URI
*/
public Path(URI aUri) {
uri = aUri.normalize(www.hengy178.com);
}
}
好吧,其实就是设置了hdfs的地址;

最后一个类,FSDataInputStream,额,不想看,太长了,

用fs的open方法创建一个FSDataInputStream类的实例,然后简单来说,读文件的流程就是,客户端到最近的(Namenode说了算)DATa Node上调用FSDataInputStream的read方法,通过反复的调用read方法,将数据从DataNode传递到客户端。

值得一提的是,它创建string的那个构造方法,我找了半天源码,似乎是这个,

public String(byte bytes[www.mengzhidu178.com], int offset, int length) {
checkBounds(bytes, offset,www.gangchengyuLe178.com length);
this.value = StringCoding.decode(bytes, offset, length);
}
/**
* Constructs a new {@code String} by decoding the specified array of bytes
* using the platform‘s www.yongxinzaixian.cn default charset. The length of the new {@code
* String} is a function of the charset, and hence may not be equal to the
* length of the byte array.
*
* <p> The behavior of this constructor when the given bytes are not valid
* in the default charset is unspecified. The {@link
* java.nio.charset.CharsetDecoder} class should be used when more control
* over the decoding process is required.
*
* @param bytes
* The bytes to be decoded into characters
*
* @since JDK1.1
**/
#######################################################################

写文件流程差不多一致,不过用到的是另外一个输出流的类FSDataOutputStream;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.Path;

public class Create_Method {
public static void main(String args[]) throws Exception{
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://10.192.4.33:9000/");
FileSystem fs = FileSystem.get(conf);
FSDataOutputStream fos = fs.create(new Path("/words.txt"));
fos.writeChars("hello world");

}
}
当然,fs还有一个用处就是查看文件目录,但是注意它的类型,是一个特殊的可迭代对象;

import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.RemoteIterator;
public class listStatus {
public static void main(String args[])throws Exception{
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://10.192.4.33:9000/data");
FileSystem fs = FileSystem.get(conf);
Path path = new Path("/");
RemoteIterator<LocatedFileStatus> list = fs.listFiles(path, true);
while(list.hasNext()) {
System.out.println(list.next());
}
}
}
看下listFiles方法的源码

public RemoteIterator<LocatedFileStatus> listFiles(
final Path f, final boolean recursive)
throws FileNotFoundException, IOException {
return new RemoteIterator<LocatedFileStatus>() {
private Stack<RemoteIterator<LocatedFileStatus>> itors =
new Stack<RemoteIterator<LocatedFileStatus>>();
private RemoteIterator<LocatedFileStatus> curItor =
listLocatedStatus(f);
private LocatedFileStatus curFile;

@Override
public boolean hasNext() throws IOException {
while (curFile == null) {
if (curItor.hasNext()) {
handleFileStat(curItor.next());
} else if (!itors.empty()) {
curItor = itors.pop();
} else {
return false;
}
}
return true;
}

/**
* Process the input stat.
* If it is a file, return the file stat.
* If it is a directory, traverse the directory if recursive is true;
* ignore it if recursive is false.
* @param stat input status
* @throws IOException if any IO error occurs
*/

原文地址:https://www.cnblogs.com/qwangxiao/p/9951451.html

时间: 2024-10-12 10:22:09

hdfs api读写文写件个人练习的相关文章

API的文档自动生成——基于CDIF的SOA基本能力

当前,作为大部分移动app和云服务后台之间的标准连接方式,REST API已经得到了绝大部分开发者的认可和广泛的应用.近年来,在新兴API经济模式逐渐兴起,许多厂商纷纷将自己的后台业务能力作为REST API开放出来,给更广泛的第三方开发者使用. 但是,管理REST API并非是一件容易的工作.由于缺乏有效的接口数据schema约束,加上设计REST API时resource endpoint的安排,以及发送http请求的方式又都五花八门,REST API开发完成后,大多数情况下API开发者仍然

Api接口文档管理工具,你知道哪些呢?

上周看到有人在我的Github开源项目中提了个issue,说是否考虑接入swagger.那今天我就用swagger与其他接口文档工具做对比,同时说说Api接口文档工具的那点事.如今,在前后端分离开发的这个年代,Api接口文档管理工具越来越显得重要.完整的Api接口文档能大大提升前后端开发协作的效率. image 目前市场有哪些比较优秀的接口文档管理工具呢?Swagger Api接口文档工具到底如何,我大致汇总一下吧! 一.Swagger 说到Swagger,他确实是为开发者发明的一款神器,他可以

Hadoop之HDFS文件读写过程

一.HDFS读过程 1.1 HDFS API 读文件 1 Configuration conf = new Configuration(); 2 FileSystem fs = FileSystem.get(conf); 3 Path file = new Path("demo.txt"); 4 FSDataInputStream inStream = fs.open(file); 5 String data = inStream.readUTF(); 6 System.out.pri

HDFS API的java代码分析与实例

HDFS API的java代码分析与实例 1.HDFS常用的方法,我已经写好,我们看一下 // Create()方法,直接在HDFS中写入一个新的文件,path为写入路径,text为写入的文本内容 public static void  Create(String path,String text) throws IOException {             Configuration conf=new Configuration();                  conf.set(

NSUserDefaults API中英文文档简介及使用

Overview The NSUserDefaults class provides a programmatic interface for interacting with the defaults system. The defaults system allows an application to customize its behavior to match a user's preferences. For example, you can allow users to deter

Python读写文本文档详解

以下3步问正确的程序片段: 1.写文件 #! /usr/bin/python3 'makeTextFile.py -- create text file' import os def write_file(): "used to write a text file." ls = os.linesep #get filename fname = input("Please input filename:") while True: if os.path.exists(

Openstack python api 学习文档

Openstack python api 学习文档 转载请注明http://www.cnblogs.com/juandx/p/4953191.html 因为需要学习使用api接口调用openstack,所以上一篇写了一些使用openstack的纯api调用的方法, 但是openstack还提供了更好的python的api,只需要python的包即可,感觉更好使用. 对于compute的api,包是放在了/usr/lib/python2.7/site-packages/novaclient/目录,

什么是API帮助文档?

API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件的以访问一组例程的能力,而又无需访问源码,或理解内部工作机制的细节. API帮助文档就是对这些函数写的文档,帮助开发人员了解函数的使用方法和功能. 检查浏览器对FileReader接口提供支持: if(typeof FileReader='undefined') { alert('你的浏览器为实现FileReader接口') }? els

app后端开发二:API接口文档工具

悲伤的历史 在进行app后端开发过程中,后端会提供出来很多的api接口供前端开发使用,为了让前端开发人员顺利使用,我们会写好一份文档,告诉他们这个接口你该用 GET 还是 POST 来访问,同时访问的时候该给我传递一些什么参数,以及正确的时候我会返回什么给你,已经返回的数据样式以及字段解释等等这些事情,我们都需要在文档中写好写清楚. 在 app后端开发一:基于swagger-ui构建api接口文档工具 这篇博客中,我写了 swagger-ui 的好处以及优势.但是在使用过程中,发现不够给力.我想