Hadoop Java Hdfs API
1. 在本地文件系统生成一个文本文件,,读入文件,将其第101-120字节的内容写入HDFS成为一个新文件
2. 在HDFS中生成文本文件,读入这个文件,将其第101-120字节的内容写入本地文件系统成为一个新文件
环境部署:http://www.cnblogs.com/dopeter/p/4630791.html

FileBuilder.java
生成文件的工具类,包含在本地生成文件,在Hadoop生成文件,读取Hadoop指定目录的文件
package story; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
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 FileBuilder { //build default test data
public static String BuildTestFileContent()
{
StringBuilder contentBuilder=new StringBuilder(); for(int loop=0;loop<100;loop++)
contentBuilder.append(String.valueOf(loop)); String content =contentBuilder.toString(); return content;
} //build local file
public static void BuildLocalFile(String buildPath,String content) throws FileNotFoundException, UnsupportedEncodingException
{
/*
FileWriter fileWriter;
try {
fileWriter = new FileWriter(buildPath); fileWriter.write(content);
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
*/ PrintWriter out = new java.io.PrintWriter(new java.io.File(buildPath), "UTF-8");
String text = new java.lang.String(content);
out.print(text);
out.flush();
out.close(); } //upload file to hadoop
public static void BuildHdfsFile(String buildPath,byte[] fileContent) throws IOException
{
//convert to inputstream
InputStream inputStream=new ByteArrayInputStream(fileContent); //hdfs upload
Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(URI.create(buildPath), conf);
OutputStream outputStream = fs.create(new Path(buildPath), new Progressable() {
public void progress() {
System.out.print(".");
}
}); IOUtils.copyBytes(inputStream, outputStream, fileContent.length, true);
} //wrapper for upload file
public static void BuildHdfsFile(String buildPath,String fileContent) throws IOException
{
BuildHdfsFile(buildPath,fileContent.getBytes());
} //download file from hadoop
public static byte[] ReadHdfsFile(String readPath)throws IOException
{
byte[] fileBuffer;
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(readPath), conf);
InputStream in = null;
ByteArrayOutputStream out=new ByteArrayOutputStream();
try {
in = fs.open(new Path(readPath));
IOUtils.copyBytes(in, out, 4096, false); fileBuffer=out.toByteArray();
} finally {
IOUtils.closeStream(in);
} return fileBuffer;
} }
FileContentHandler.java
文件内容的处理类,读取本地文件时设置起始Position与截取的长度,读取从Hadoop下载的文件时设置起始Position与截取的长度
package story; import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException; public class FileContentHandler {
public static byte[] GetContentByLocalFile(String filePath,long beginPosition,int readLength)
{
int readBufferSize=readLength;
byte[] readBuffer=new byte[readBufferSize]; RandomAccessFile accessFile;
try {
accessFile=new RandomAccessFile (filePath,"r");
long length=accessFile.length();
System.out.println(length); if(length>beginPosition&&length>beginPosition+readBufferSize)
{
accessFile.seek(beginPosition);
accessFile.read(readBuffer);
accessFile.close();
}
} catch ( IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return readBuffer;
} public static String GetContentByBuffer(byte[] buffer,int beginPosition,int readLength) throws UnsupportedEncodingException
{
String content;
byte[] subBuffer=new byte[readLength];
for(int position=0;position<readLength;position++)
subBuffer[position]=buffer[beginPosition+position]; buffer=null; content=new String(subBuffer,"UTF-8");
System.out.println(content); return content;
} }
UploadStory.java
1的流程代码
package story;
public class UploadStory {
//public static void main(String[] args) throws Exception {}
public static void main(String[] args) throws Exception {
//also define value of parameter from arguments.
String localFilePath="F:/bulid.txt";
String hdfsFilePath="hdfs://hmaster0:9000/user/14699_000/input/build.txt";
int readBufferSize=20;
long fileBeginReadPosition=101;
//upload story begin.
//build local file
FileBuilder.BuildLocalFile(localFilePath,FileBuilder.BuildTestFileContent());
//read file
byte[] uploadBuffer=FileContentHandler.GetContentByLocalFile(localFilePath, fileBeginReadPosition, readBufferSize);
//upload
if(uploadBuffer!=null&&uploadBuffer.length>0)
FileBuilder.BuildHdfsFile(hdfsFilePath, uploadBuffer);
}
}
DownloadStory.java
2的流程代码
package story;
public class DownloadStory {
//public static void main(String[] args) throws Exception { }
public static void main(String[] args) throws Exception {
//also define value of parameter from arguments.
String localFilePath="F:/bulid.txt";
String hdfsFilePath="hdfs://hmaster0:9000/user/14699_000/input/build2.txt";
int readBufferSize=20;
int fileBeginReadPosition=101;
//build file to hadoop
FileBuilder.BuildHdfsFile(hdfsFilePath, FileBuilder.BuildTestFileContent());
//download file
byte[] readBuffer=FileBuilder.ReadHdfsFile(hdfsFilePath);
//handle buffer
String content=FileContentBuilder.GetContentByBuffer(readBuffer, fileBeginReadPosition, readBufferSize);
//write to local file
FileBuilder.BuildLocalFile(localFilePath, content);
}
}
Hadoop Java Hdfs API的更多相关文章
- Hadoop JAVA HDFS客户端操作
JAVA HDFS客户端操作 通过API操作HDFS org.apache.logging.log4jlog4j-core2.8.2org.apache.hadoophadoop-common${ha ...
- hadoop学习笔记(七):Java HDFS API
一.使用HDFS FileSystem详解 HDFS依赖的第三方包: hadoop 1.x版本: commons-configuration-1.6.jar commons-lang-2.4.jar ...
- Hadoop 之 HDFS API操作
1. 文件上传 @Slf4j public class HDFSClient { @Test public void testCopyFromLocalFile() throws Exception{ ...
- JAVA HDFS API Client 连接HA
如果Hadoop开启HA,那么用Java Client连接Hive的时候,需要指定一些额外的参数 package cn.itacst.hadoop.hdfs; import java.io.FileI ...
- hadoop系列二:HDFS文件系统的命令及JAVA客户端API
转载请在页首明显处注明作者与出处 一:说明 此为大数据系列的一些博文,有空的话会陆续更新,包含大数据的一些内容,如hadoop,spark,storm,机器学习等. 当前使用的hadoop版本为2.6 ...
- hadoop: hdfs API示例
利用hdfs的api,可以实现向hdfs的文件.目录读写,利用这一套API可以设计一个简易的山寨版云盘,见下图: 为了方便操作,将常用的文件读写操作封装了一个工具类: import org.apach ...
- Hadoop Java API 操作 hdfs--1
Hadoop文件系统是一个抽象的概念,hdfs仅仅是Hadoop文件系统的其中之一. 就hdfs而言,访问该文件系统有两种方式:(1)利用hdfs自带的命令行方式,此方法类似linux下面的shell ...
- Hadoop基础-HDFS的API常见操作
Hadoop基础-HDFS的API常见操作 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本文主要是记录一写我在学习HDFS时的一些琐碎的学习笔记, 方便自己以后查看.在调用API ...
- Hadoop学习之路(十)HDFS API的使用
HDFS API的高级编程 HDFS的API就两个:FileSystem 和Configuration 1.文件的上传和下载 package com.ghgj.hdfs.api; import org ...
随机推荐
- GPS 偏移校正(WGS-84) 至(GCJ-02) java版本号以实现
public class EvilTransform { final static double pi = 3.14159265358979324; // // // a = 6378245.0, 1 ...
- Could not load file or assembly'System.Data.SQLite.dll' or one of its depedencies
[问题] 在我本机的开发环境c#连接sqlite3没有问题,但是release版本号移植到其它的机器就提示Could not load file or assembly'System.Data. ...
- SQL Server 性能调优培训引言
原文:SQL Server 性能调优培训引言 大家好,这是我在博客园写的第一篇博文,之所以要开这个博客,是我对MS SQL技术学习的一个兴趣记录. 作为计算机专业毕业的人,自己对技术的掌握总是觉得很肤 ...
- Java 抽象工厂模式
抽象工厂模式(Abstract Factory Pattern)是工厂方法模式的进一步抽象,其英文原话"Provide an interface for creating families ...
- Vim配置说明
使用这些天一直vim,我认为vim这是一个非常强大的编辑器,尤其是后配置. 互联网参考大牛个月vim配置,然后更改加入了一部分,形成了自己的配置.让Vim变的更强大. 详细有下面几个特点: 1.自己主 ...
- WPF 绑定
WPF里分三种Binding:Binding, PriorityBinding, MultiBinding,这三种Binding的基类都是BindingBase,而BindingBase又继承于Mar ...
- HDU 2815 Mod Tree 离散对数 扩张Baby Step Giant Step算法
联系:http://acm.hdu.edu.cn/showproblem.php?pid=2815 意甲冠军: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQ ...
- hdu 2899 hdu 3400 三分/几何
hdu2899 : 水提,直接三分,事实上求导后二分也能够. #include<iostream> #include<cstdio> using namespace std; ...
- 猫学习IOS(五岁以下儿童)UI之360其他下载管理器广场UI
猫分享.必须精品 下载材料:http://blog.csdn.net/u013357243/article/details/44486651 先看效果 主要是完毕了九宫格UI的搭建 代码 - (voi ...
- Maven使用-利用Maven引入相关包(Struts2)
根据上一篇的项目搭建,接下来引入需要使用Struts2相关包 1,如何利用maven往项目中引入包? maven就像一个导包助手一样,让它知道去哪里拿什么,他就会自动完成需要的包的搬运工作. (1), ...