用程序对hdfs进行操作。,程序hdfs进行
用程序对hdfs进行操作。,程序hdfs进行
调试加安装了半天,怎么也没有配置好怎么通过Eclipse直接连接hdfs,最后我还是打成一个jar包放到Linux虚拟机中执行的。
执行命令Java -jar XXX.jar.
其中对hdfs的操作比较简单,主要就FileSystem这一个类,这个东西搞懂了,你对通过程序进行对hdfs的操作自然而然的也就非常熟练了。
下面我简单的举一个简单的从hdfs上读取文件内容的例子。大家分享一下。
package com.pzoom.hdfs; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URI; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FileStatus; 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 PutFileToHdfs { /** * 从HDFS上读取文件 */ private static void readFromHdfs() throws FileNotFoundException, IOException { String dst = "hdfs://ubuntu:9000/"; Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(URI.create(dst), conf); String path = "/README.txt"; FSDataInputStream hdfsInStream = fs.open(new Path(path)); IOUtils.copyBytes(hdfsInStream, System.out, conf, true); /* OutputStream out = new FileOutputStream("/home/chenlongquan/output"); byte[] ioBuffer = new byte[1024]; int readLen = hdfsInStream.read(ioBuffer); while (-1 != readLen) { out.write(ioBuffer, 0, readLen); readLen = hdfsInStream.read(ioBuffer); } out.close(); hdfsInStream.close(); fs.close(); */ } /** * main函数 * * @param args * @throws Exception */ public static void main(String[] args) throws Exception { try { //uploadToHdfs(); readFromHdfs(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { } } }
hadoop计算需要在hdfs文件系统上进行,文件上传到hdfs上通常有三种方法:a hadoop自带的dfs服务,put;b hadoop的API,Writer对象可以实现这一功能;c 调用OTL可执行程序,数据从数据库直接进入hadoop
hadoop计算需要在hdfs文件系统上进行,因此每次计算之前必须把需要用到的文件(我们称为原始文件)都上传到hdfs上。文件上传到hdfs上通常有三种方法:
a hadoop自带的dfs服务,put;
b hadoop的API,Writer对象可以实现这一功能;
c 调用OTL可执行程序,数据从数据库直接进入hadoop
由于存在ETL层,因此第三种方案不予考虑
将a、b方案进行对比,如下:
1 空间:方案a在hdfs上占用空间同本地,因此假设只上传日志文件,则保存一个月日志文件将消耗掉约10T空间,如果加上这期间的各种维表、事实表,将占用大约25T空间
方案b经测试,压缩比大约为3~4:1,因此假设hdfs空间为100T,原来只能保存约4个月的数据,现在可以保存约1年
2 上传时间:方案a的上传时间经测试,200G数据上传约1小时
方案b的上传时间,程序不做任何优化,大约是以上的4~6倍,但存在一定程度提升速度的余地
3 运算时间:经过对200G数据,大约4亿条记录的测试,如果程序以IO操作为主,则压缩数据的计算可以提高大约50%的速度,但如果程序以内存操作为主,则只能提高5%~10%的速度
4 其它:未压缩的数据还有一个好处是可以直接在hdfs上查看原始数据。压缩数据想看原始数据只能用程序把它导到本地,或者利用本地备份数据
压缩格式:按照hadoop api的介绍,压缩格式分两种:BLOCK和RECORD,其中RECORD是只对value进行压缩,一般采用BLOCK进行压缩。
对压缩文件进行计算,需要用SequenceFileInputFormat类来读入压缩文件,以下是计算程序的典型配置代码:
JobConf conf = new JobConf(getConf(), log.class);
conf.setJobName(”log”);
conf.setOutputKeyClass(Text.class);//set the map output key type
conf.setOutputValueClass(Text.class);//set the map output value type
conf.setMapperClass(MapClass.class);
//conf.setCombinerClass(Reduce.class);//set the combiner class ,if havenot, use Recuce class for default
conf.setReducerClass(Reduce.class);
conf.setInputFormat(SequenceFileInputFormat.class);//necessary if use compress
接下来的处理与非压缩格式的处理一样...余下全文>>
B
评论暂时关闭