Hadoop的text通过set(Text t)或set(String s),getLength与getBytes().length的值可能不同的原因

本文探讨了Hadoop中Text对象的set方法行为及其对getBytes和getLength方法的影响。通过两个示例展示了不同情况下Text对象长度的变化规律。

示例1:

Text t = new Text("hadoopp");
t.set(new Text("pig"));
byte[] b = t.getBytes();
for (byte bb : b)
System.out.print(bb + " ");
System.out.println();
System.out.println(t.getLength());
System.out.println(b.length);
System.out.println(t.toString());

输出如下:

112 105 103 111 111 112 112 
3
7
pig

示例2:

更改上例的t对象为“ha”的text对象;

输出如下:

112 105 103 
3
3
pig


个人总结:

text的set(Text  tt)方法是将tt复制到t对象中,当t初始长度大于tt的长度时getBytes().length会保留t的初始长度,当t的初始长度小于tt的长度时getBytes().length的长度按tt的长度算,而getLength()算的是各自的字符数。

一句话:用了set(Text  tt)函数时,getBytes().length的长度按两者中较长的算。

注意:set(string s)的getLength与getBytes().length的值是一样的!

package com.dajiangtai.hadoop.tv; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.util.GenericOptionsParser; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; /* * 解析机顶盒用户原始数据 */ public class ParseAndFilterLog extends Configured implements Tool { /* * 只需Mapper完成原始数据解析 */ public static class ExtractTVMsgLogMapper extends //Mapper<LongWritable, BytesWritable, Text, Text> { Mapper<LongWritable, Text, Text, Text> { //public void map(LongWritable key, BytesWritable value, Context context) public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { // 原始数据 //String data = new String(value.getBytes(), 0, value.getLength()); String data = value.toString(); // 调用接口直接解析出我们需要数据格式 // stbNum + "@" + date + "@" + sn + "@" + p+ "@" + s + "@" + e + "@" // + duration DataUtil.transData(data, context); } } public int run(String[] args) throws Exception { // TODO Auto-generated method stub Configuration conf = new Configuration(); String[] otherArgs = new GenericOptionsParser(conf, args) .getRemainingArgs(); if (otherArgs.length < 2) { System.err.println("Usage: ParseAndFilterLog [<in>...] <out>"); System.exit(2); } Job job = Job.getInstance(); // 设置输出key value分隔符 job.getConfiguration().set("mapreduce.output.textoutputformat.separator", "@"); job.setJarByClass(ParseAndFilterLog.class); job.setMapperClass(ExtractTVMsgLogMapper.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); //job.setInputFormatClass(SequenceFileInputFormat.class); // 设置输入路径 for (int i = 0; i < otherArgs.length - 1; ++i) { FileInputFormat.addInputPath(job, new Path(otherArgs[i])); } // 设置输出路径 FileOutputFormat.setOutputPath(job, new Path( otherArgs[otherArgs.length - 1])); return job.waitForCompletion(true) ? 0 : 1; } public static void main(String[] args) throws Exception { int ec = ToolRunner.run(new Configuration(),new ParseAndFilterLog(), args); System.exit(ec); } } 帮我修改一下代码使其不报错
07-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值