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. C++经典面试题(二)

    近期看一些面试题,认为假设自己被问到了,并不能非常利落的回答出来. 一是从来没有这个意识,二是没有认真的梳理下. 以下对这些题做出分析,哈!个人能力有限,当中难免有疏漏,欢迎大家补充咯. 11.引用与 ...

  2. IOS加强知识(1)理解力Objective-C

    一直想写一般Objective-C帖子,总是没时间.所以,我希望有一个巨大的知识更小.温馨提示小的变化.写一点点,每天.东西把他们的学习分享,好了废话不多. 1.一门动态的语言OC Object-C( ...

  3. Add/Remove listview web part in publish site via powershell

    1. Here is the code: Add WebPart in Publish Site Example : AddWebPartPublish http://localhost  " ...

  4. 企业邮件系统-Postfix安装使用

    Postfix是目前流行的一套邮件传输代理软件(MTA),其作者Wietst Venema最初开发这套软件时就对总体设计.扩展能力.可用性及系统安全等方面进行了充分的考虑.由于Postfix在稳定.效 ...

  5. 呈现怎样的香蕉饼路线Android系统

    无话可说,该系统的第一版,Android有的还可以,路由设置确实有闪光现象背.与其他的稳定版本发布,被能够机顶盒和路由组合.其次是SSD,还是很不错的. 硬件 watermark/2/text/aHR ...

  6. 建立Hibernate二级Cache

    建立Hibernate二级Cache它需要两个步骤:首先,一定要使用什么样的数据并发策略,然后配置缓存过期时间,并设置Cache提供器. 有4种内置的Hibernate数据并发冲突策略,代表数据库隔离 ...

  7. 9种CSS3 blend模式制作的鼠标滑过图片标题特效

    这是一款使用CSS3 background-blend-mode制作的鼠标滑过图片标题特效.该图片标题特效在鼠标滑过一张图片的时候,图片的标题会对应的动画,而且图片会使用css blend模式渲染为很 ...

  8. FTP文件操作之获取文件列表

    前面已经介绍了很多关于FTP对文件的操作,今天再跟大家介绍一个获取文件列表的功能.这个功能应该算是最简单的一个了,它只是获取了一下文件信息,而没有进行实质上的数据传输. 下面是是该功能的核心代码:   ...

  9. or1200中IMMU分析(续)

    下面内容摘自<步步惊芯--软核处理器内部设计分析>一书 2 IMMU中的特殊寄存器 OR1200处理器中的IMMU包括第2组特殊寄存器,如表10.1所看到的. ITLBW0MRx是指令TL ...

  10. python网络爬虫学习笔记

    python网络爬虫学习笔记 By 钟桓 9月 4 2014 更新日期:9月 4 2014 文章文件夹 1. 介绍: 2. 从简单语句中開始: 3. 传送数据给server 4. HTTP头-描写叙述 ...