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的更多相关文章

  1. Hadoop JAVA HDFS客户端操作

    JAVA HDFS客户端操作 通过API操作HDFS org.apache.logging.log4jlog4j-core2.8.2org.apache.hadoophadoop-common${ha ...

  2. hadoop学习笔记(七):Java HDFS API

    一.使用HDFS FileSystem详解 HDFS依赖的第三方包: hadoop 1.x版本: commons-configuration-1.6.jar commons-lang-2.4.jar ...

  3. Hadoop 之 HDFS API操作

    1. 文件上传 @Slf4j public class HDFSClient { @Test public void testCopyFromLocalFile() throws Exception{ ...

  4. JAVA HDFS API Client 连接HA

    如果Hadoop开启HA,那么用Java Client连接Hive的时候,需要指定一些额外的参数 package cn.itacst.hadoop.hdfs; import java.io.FileI ...

  5. hadoop系列二:HDFS文件系统的命令及JAVA客户端API

    转载请在页首明显处注明作者与出处 一:说明 此为大数据系列的一些博文,有空的话会陆续更新,包含大数据的一些内容,如hadoop,spark,storm,机器学习等. 当前使用的hadoop版本为2.6 ...

  6. hadoop: hdfs API示例

    利用hdfs的api,可以实现向hdfs的文件.目录读写,利用这一套API可以设计一个简易的山寨版云盘,见下图: 为了方便操作,将常用的文件读写操作封装了一个工具类: import org.apach ...

  7. Hadoop Java API 操作 hdfs--1

    Hadoop文件系统是一个抽象的概念,hdfs仅仅是Hadoop文件系统的其中之一. 就hdfs而言,访问该文件系统有两种方式:(1)利用hdfs自带的命令行方式,此方法类似linux下面的shell ...

  8. Hadoop基础-HDFS的API常见操作

    Hadoop基础-HDFS的API常见操作 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本文主要是记录一写我在学习HDFS时的一些琐碎的学习笔记, 方便自己以后查看.在调用API ...

  9. Hadoop学习之路(十)HDFS API的使用

    HDFS API的高级编程 HDFS的API就两个:FileSystem 和Configuration 1.文件的上传和下载 package com.ghgj.hdfs.api; import org ...

随机推荐

  1. 漂亮的表格样式(使用CSS样式表控制表格样式)

    根据WEB2.0风格,设计了几个表格样式,我希望你喜欢. WEB2.0推广使用div开放式布局.但并不是完全放弃使用形式,在数据表现形式而言是一个不错的选择. 本节将介绍如何使用现在CSS样式表来控制 ...

  2. php文件操作基本使用方法

    <?php /* $fp=fopen("tmp.html","r"); $str=fread($fp,filesize("tmp.html&qu ...

  3. Delphi XE7 发布时间

    Delphi7 XE7 我可以下载: http://altd.embarcadero.com/download/radstudio/xe7/delphicbuilder_xe7_win.iso 安装包 ...

  4. DateTime.Compare(t1,t2)比較两个日期大小

    DateTime.Compare(t1,t2)比較两个日期大小,排前面的小,排在后面的大,比方:2011-2-1就小于2012-3-2返回值小于零:  t1 小于 t2. 返回值等于零 : t1 等于 ...

  5. ps设计资料整理

    零基础学会建立一个简单化妆品网站—前台设计篇1[PS画草图] http://xiebiji.com/2008/09/huazhuang4/?wptheme=Plainscape&ie=1 PS ...

  6. Android 导入第三方jar时 出现ClassNotFoundException

    处理方式: 1.首先检查有没有正确的导入该jar和有没有重复导入该jar. 2.如果没有出现1的问题,那么在buildpath->order and export 中将你导入的jar左边的复选框 ...

  7. 玩转Web之easyui(三)-----easy ui dataGird 重新指定url以获取不同数据源信息

    如果已经写了一个dataGird并且已经通过url绑定数据源,能不能在其他地方改变url使其从不同数据源获取信息,从而实现查询等操作?答案当然是肯定的,而且仅需要几行代码 $('#btnq').bin ...

  8. Cocos2d-x3.0游戏实例《不要救我》三——背景滚动周期

    好.让我们来解释一下这个无限循环滚动的背景.这方面的知识一直讲到烂.我以前的文章还介绍了.所以不是那么特别清楚. 笨木头花心贡献,啥?花心?不呢,是用心~ 转载请注明,原文地址:http://www. ...

  9. Android Java 与 C++ 恒调用,路径、文件名、延长的最大长度

    /****************************************************************************  ********************* ...

  10. 【Android进阶】Android调用WebService的实现

    最近想自己搞搞服务器,就从最简单的webservice开始吧 先上效果图 项目结构 开始贴代码,注释都有,有问题的请留言 MainActivity.java package com.example.w ...