我想在mapreduce中开发一个程序,从.tbl文件中获取cust\u key和balance值。我将这两个值连接成字符串,然后将其发送到reducer,因此我将计算cust\u key并找到每个段的平均余额。这就是为什么我将该段添加为key。
我想拆分字符串并将这两个值分开,以便计算cust键数并对余额求和以找到平均值。但是拆分数组[0]给出的是整个字符串,而不是字符串的第一个值。同时拆分数组[1]引发ArrayYoutofBounds异常。我希望清楚。
代码如下
public class MapReduceTest {
public static class TokenizerMapper extends Mapper<Object, Text, Text, Text>{
private Text segment = new Text();
private Text word = new Text();
private float balance = 0;
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
String[] line = value.toString().split("\\|");
balance = Float.parseFloat(line[5]);
String cust_key = line[1];
int nation = Integer.parseInt(line[3]);
if((balance > 8000) && ( nation < 15) && (nation > 1)){
segment.set(line[6]);
//word.set(cust_key+","+balance);
word.set(cust_key+","+balance);
context.write(segment,word);
}
}
}
public static class AvgReducer extends Reducer<Text,Text,Text,Text> {
Text val = new Text();
public void reduce(Text key, Iterable<Text> values,Context context) throws IOException, InterruptedException {
String cust_key = "";
float avg,sum = 0;
int count = 0;
for(Text v : values){
String[] a = v.toString().trim().split(",");
cust_key +=a[0];
}
val.set(cust_count);
context.write(key, val);
}
}
输入数据
8794|Customer#000008794|6dnUgJZGX73Kx1idr6|18|28-434-484-9934|7779.30|HOUSEHOLD|deposits detect furiously even requests. furiously ironic packages are slyly into th
8795|Customer#000008795|oA1cLUtWOAIFz5Douypbq1jHv glSE|9|19-829-732-8102|9794.80|BUILDING|totes. blithely unusual theodolites integrate carefully ironic foxes. unusual excuses cajole carefully carefully fi
8796|Customer#000008796|CzCzpV7SDojXUzi4165j,xYJuRv wZzn grYsyZ|24|34-307-411-6825|4323.03|AUTOMOBILE|s. pending, bold accounts above the sometimes express accounts
8797|Customer#000008797|TOWDryHNNqp8bvgMW6 FAhRoLyG1ldu2bHcJCM6|2|12-517-522-5820|219.78|FURNITURE|ly bold pinto beans can nod blithely quickly regular requests. fluffily even deposits ru
8798|Customer#000008798|bIegyozQ5kzprN|15|25-472-647-6270|6832.96|AUTOMOBILE|es-- silent instructions nag blithely
堆栈跟踪
java.lang.Exception: java.lang.ArrayIndexOutOfBoundsException: 1
at org.apache.hadoop.mapred.LocalJobRunner$Job.runTasks(LocalJobRunner.java:462)
at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:529)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
at MapReduceTest$AvgReducer.reduce(MapReduceTest.java:69)
at MapReduceTest$AvgReducer.reduce(MapReduceTest.java:1)
at org.apache.hadoop.mapreduce.Reducer.run(Reducer.java:171)
at org.apache.hadoop.mapred.ReduceTask.runNewReducer(ReduceTask.java:627)
at org.apache.hadoop.mapred.ReduceTask.run(ReduceTask.java:389)
at org.apache.hadoop.mapred.LocalJobRunner$Job$ReduceTaskRunnable.run(LocalJobRunner.java:319)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
17/04/12 18:40:33 INFO mapreduce.Job: Job job_local806960399_0001 running in uber mode : false
17/04/12 18:40:33 INFO mapreduce.Job: map 100% reduce 0%
17/04/12 18:40:33 INFO mapreduce.Job: Job job_local806960399_0001 failed with state FAILED due to: NA
17/04/12 18:40:33 INFO mapreduce.Job: Counters: 35
更新
减速机
public static class AvgReducer extends Reducer<Text,Text,Text,Text> {
Logger log = Logger.getLogger(AvgReducer.class.getName());
public void reduce(Text key, Iterable<Text> values,Context context) throws IOException, InterruptedException {
float sumBalance=0,avgBalance = 0;
int cust_count = 1;
for(Text v : values){
String[] a = v.toString().trim().split(",");
//c2 += " i "+i+" "+a[0]+"\n";
sumBalance +=Float.parseFloat(a[a.length-1]);
cust_count++;
}
avgBalance = sumBalance / cust_count;
context.write(key,new Text(avgBalance+" "+cust_count));
}
}
堆栈跟踪
java.lang.Exception: java.lang.NumberFormatException: For input string: "8991.715 289"
提前谢谢。
2条答案
按热度按时间7y4bm7vi1#
pig运行mapreduce(如果这样配置的话)。它也比在mapreduce上乱搞干净得多,并且安装在主要的hadoop发行版上。
和输出
不确定你真的想要平均值,这里只有一个元素,但是你需要
GROUP BY
上segment
以及customer_key
,这样您就可以方便地使用AVG
功能。如果您更熟悉sql,那么hive也可能是一种更直接的方法。
(除非另有配置,否则也通过mapreduce运行)
然后,会是这样的
我将进入apachespark示例,但sparksql本质上就是这个配置单元查询。
nkhmeac62#
如果您真的想在javamapreduce中尝试这一点,请尝试标准化您的输入并快速捕获错误。
返回以丢弃有问题的记录
然后,如果您试图编写类似的输出记录,那么reducer应该理想地生成相同的格式
(代码未测试)