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 ...
随机推荐
- android选择和裁剪图像拍摄的图像
转载请注明出处:http://blog.csdn.net/allen315410/article/details/39994913 近期从曾经的项目中扒下来一个经常使用的模块.在这里有必要记录一下的. ...
- Oracle SQL Lesson (5) - 使用组函数输出聚合数据
组函数AVGCOUNTMAXMINSUMVARIANCE:方差STDDEV:标准差 SELECT AVG(salary), MAX(salary), MIN(salary), SUM(salary)F ...
- HOJ2275 Number sequence
Number sequence My Tags tag=&type=or" style="margin:0px; padding:0px; color:rgb(27,87, ...
- 凝视条件推断浏览器<!--[if !IE]><!--[if IE]><!--[if lt IE 6]><!--[if gte IE 6]>
<!--[if !IE]><!--> 除IE外可识别 <!--<![endif]--> <!--[if IE]> 所有的IE可识别 <![e ...
- Grub2配置详解(转)
grub2基础教程-修订版 smallapple 目录 一.grub2新特性 二.grub2安装与启动 三.grub2配置文件 ...
- Java执行批处理.bat文件(有问题???求高手帮忙解答!!!)
Java执行批处理.bat文件(有问题???求高手帮忙解答!!!) 在项目开发中常常都会遇到需要在代码中调用批处理bat脚本,把自己在项目中遇到过的总结下 ...
- 【Swift】学习笔记(一)——熟知 基础数据类型,编码风格,元组,主张
自从苹果宣布swift之后,我一直想了解,他一直没有能够把它的正式学习,从今天开始,我会用我的博客来驱动swift得知,据我们了解还快. 1.定义变量和常量 var 定义变量,let定义常量. 比如 ...
- 发布Ubuntu/Linux系统cache,增加可用内存空间
桌面Ubuntu总内存4G,但free只有内存有100M 重视top命令检查看到真正的能力free内存.以下是真正的内存使用情况的看法有一个命令. watch -n 1 cat /proc/memin ...
- CMake入门(二)
CMake入门(二) 最后更新日期:2014-04-25 by kagula 阅读前提:<CMake入门(一)>.Linux的基本操作 环境: Windows 8.1 64bit英文版.V ...
- Codeforces Round #223 (Div. 2)--A. Sereja and Dima
Sereja and Dima time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...