• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
导航菜单
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • windows
  • 服务器硬件
  • 服务器运维
  • 云计算
  • 虚拟化
  • IIS教程
  • Linux
  • Apache
  • Ftp
  • DNS
  • Nginx
您的位置:首页 > 服务器 >云计算 > 自定义输出文件名,输出文件名

自定义输出文件名,输出文件名

作者:网友 字体:[增加 减小] 来源:互联网

本文主要包含自定义文件名,自定义英文名,qq炫舞自定义戒指名,自定义函数名,易语言自定义进程名等服务器相关知识,网友希望可以进行参考

自定义输出文件名,输出文件名


<p>package selfname;</p>
	import java.io.IOException;

	import org.apache.hadoop.conf.Configuration;
	import org.apache.hadoop.fs.Path;
	import org.apache.hadoop.io.Text;
	//import org.apache.hadoop.mapreduce.lib.input.CombineFileInputFormat;
	//import org.apache.hadoop.mapreduce.lib.input.MultipleTextOutputFormat;
	import org.apache.hadoop.mapreduce.Job;
	import org.apache.hadoop.mapreduce.Mapper;
	import org.apache.hadoop.mapreduce.Reducer;
	import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
	import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
	import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;

	public class Selfname {
	        public static class FileMap extends Mapper<Object,Text,Text,Text>{
	        	public void map(Object key,Text value,Context context) throws IOException, InterruptedException{
	        		String line = value.toString();
	        		if(line.length()>=93){
	        		String keys = line.substring(15, 19);
	        		String values = line.substring(87,93);
	        		context.write(new Text(keys), new Text(values));
	        		}
	        	}
	        }
	        public static class FileReduce extends Reducer<Text,Text,Text,Text>{
	        	<strong>private MultipleOutputs<Text, Text> multipleOutputs;
	        	protected void setup(Context context)throws IOException, InterruptedException {
	        		multipleOutputs = new MultipleOutputs<Text, Text>(context);
	        		}</strong>
	        	public void reduce(Text key,Iterable<Text>values,Context context) throws IOException, InterruptedException{
	        		String value = new String();
	        		for(Text val:values){
	        			value+=val.toString()+",";
	        		}
	        		<strong>multipleOutputs.write(key, new Text(value), key.toString());</strong>

	        	}
	        	<strong>protected void cleanup(Context context)throws IOException, InterruptedException {
	        		multipleOutputs.close();
	        		}</strong>
	        }
	        public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{
	        	Configuration conf = new Configuration();
	        	Job job = new Job(conf,"myfile");
	        	job.setJarByClass(Selfname.class);
	        	job.setMapperClass(FileMap.class);
	        	job.setReducerClass(FileReduce.class);
	        	job.setOutputKeyClass(Text.class);
	        	job.setOutputValueClass(Text.class);
	        	job.setNumReduceTasks(1);
	        	FileInputFormat.addInputPath(job, new Path(args[0]));
	        	FileOutputFormat.setOutputPath(job, new Path(args[1]));
	        	System.exit(job.waitForCompletion(true)?0:1);
	        	
	        }
	}

上面的代码实现了自定义的输出文件名,关键部分由粗体字标出。

使用MultipleOutputs类动态的实现输出文件的命名,动态,灵活。可以满足用户的定制需求,而且相对与其他的方法其代码量也非常少。

这里我用的数据是权威指南上边的气象数据,将相同年份的气温存储到一个文件中。

<strong>private MultipleOutputs<Text, Text> multipleOutputs;</strong>
<strong>声明一个MultipleOutputs变量。</strong>
<strong></strong><pre name="code" class="java"><strong>protected void setup(Context context)throws IOException, InterruptedException {
	        		multipleOutputs = new MultipleOutputs<Text, Text>(context);
	        		}</strong>
创建一个MultipleOutput变量。

<strong></strong><pre name="code" class="java"><strong>multipleOutputs.write(key, new Text(value), key.toString());</strong>
输出文件,类似于context.write。第一个变量为key,第二个变量为value,第三个变量为输出文件路径名。

<strong>最后需要使用cleanup函数将这个对象关闭。</strong>
<strong>
</strong>

版权声明:本文为博主原创文章,未经博主允许不得转载。

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • 自定义输出文件名,输出文件名

相关文章

  • Hadoop集群优化,hadoop集群
  • input from 表单提交 使用 属性 disabled=&quot;disabled&quot; 后台接收不到name=&quot;username&quot;的值,disabledusername
  • R语言数据分析系列之一,数据分析系列之一
  • Andrew Ng机器学习课程10,andrewng机器课程
  • ZooKeeper+Hadoop2.6.0的ResourceManager HA搭建,hadoop2.6集群搭建
  • Hadoop对小文件的解决方案,
  • libvirt网络过滤规则:禁止客户机(bridge方式)连接外网,libvirtbridge
  • word2vec.c源码分析,word2vec.c源码
  • OutOfMemoryError PermGen space 的解决办法(头痛了几天),permgenspace
  • hadoop 2.6.0 安装过程,hadoop2.6.0

文章分类

  • windows
  • 服务器硬件
  • 服务器运维
  • 云计算
  • 虚拟化
  • IIS教程
  • Linux
  • Apache
  • Ftp
  • DNS
  • Nginx

最近更新的内容

    • HDFS概述---namenode的设计详解,hdfs概述---namenode
    • Hadoop之——HBASE结合MapReduce批量导入数据,hadoopmapreduce
    • 在Java程序中调用Salesforce REST API,salesforcerest
    • hadoop单机存储均衡和坏block处理,hadoop单机block
    • spark-OutOfMemory:GC overhead limit exceeded 解决,timelimitexceeded
    • Hbase集群扩展,hbase集群
    • Andrew Ng机器学习课程9-补充,andrew9-
    • openstack中glance组件images的所有python API 汇总,openstackglance
    • Hadoop之——Java操作HBase,hadoopjavahbase
    • 整理得很全面的Nginx学习资源,nginx学习资源

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有