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. net Mvc模块化开发

    Asp.net Mvc模块化开发之“部分版本部分模块更新(上线)” 项目开发从来就不是一个简单的问题.更难的问题是维护其他人开发的项目,并且要修改bug.如果原系统有重大问题还需要重构. 怎么重构系统 ...

  2. 【译】使用微软企业库5.0进行WCF服务边界上的异常保护

    在Windows Communication Foundation (WCF)中,为了阻止服务的实现细节从服务的安全边界泄露,未知的异常不应该被发送至客服端.在WCF配置中将<serviceDe ...

  3. Eclipse项目崩溃,使用MyEclipse解决

    在今天的项目,Eclipse  在Rwenjian崩溃,导致项目全红 叉 并且不提示任务的错误信息. 无奈之下想起MyEclipse老板. 复制项目MyEclipse文件夹下. 之后,在MyEclip ...

  4. STL顺序容器【vector】【deque】【list】

    我们都知道,stl在集装箱船分为两类,订购集装箱和相关的容器. 顺序容器有三种即动态数组vector,双端队列deque,以及链表list (对csdn的文字排版严重吐槽.写好的版发表了就变了) 一: ...

  5. ASP.Net 重写IHttpModule 来拦截 HttpApplication 实现HTML资源压缩和空白过滤

    务实直接上代码: 1. 重写FilterModule.cs using System; using System.Collections.Generic; using System.Linq; usi ...

  6. 解决android3.0版本号以上应用接收不到开机广播问题

    如今是2014-07-16 下午15:27. 好久没写过东西,突然间灵感喷发想写点东西(事实上是刚刚弄好了一个棘手的问题,自豪中..呵呵呵呵 我牛掰).废话不多说,进入正题. 不知道你们又没有碰到这问 ...

  7. hdu2993坡dp+二进制搜索

    MAX Average Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  8. 左右Map

    Person p=new Person("黄雄"); Map map=new HashMap(); map.put("p", p); p.setName(&qu ...

  9. [Unity3D]脚本中Start()和Awake()的差别

    Unity3D刚開始学习的人常常把Awake和Start混淆. 简单说明一下,Awake在MonoBehavior创建后就立马调用,Start将在MonoBehavior创建后在该帧Update之前. ...

  10. Cocos2d-x学习笔记(9)(CCTextFieldTTF使用输入框)

    1.CCTextFieldTTF创建和使用 CCTextFieldTTF::create(const char* placeholder,const char* fontName.float font ...