實驗是MapReduce的對給定的表格進行資料挖掘。《大資料技術原理與應用》林子雨的第七章實驗5的第三個實驗給出child-parent表格,挖掘其中的父子輩關系給出祖孫輩關系表格。
這是源程式代碼:
package job;
import java.io.IOException;
import java.util.Iterator;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
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.util.GenericOptionsParser;
public class ChildGrandparent {
public static int time = 0;
public static class Map extends Mapper<Object, Text, Text, Text> {
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
String childName = new String();
String parentName = new String();
String relationType = new String();
String line = value.toString();
int i = 0;
while (line.charAt(i) != ' ') {
i++;
}
String[] values = { line.substring(0, i), line.substring(i + 1) };
if (values[0].compareTo("child") != 0) {
childName = values[0];
parentName = values[1];
relationType = "1";
context.write(new Text(values[1]), new Text(relationType + "+"
+ childName + "+" + parentName));
relationType = "2";
context.write(new Text(values[0]), new Text(relationType + "+"
+ childName + "+" + parentName));
}
}
}
public static class Reduce extends Reducer<Text, Text, Text, Text> {
public void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
if (time == 0) {
context.write(new Text("grandchild"), new Text("grandparent"));
time++;
}
int grandchildnum = 0;
String grandchild[] = new String[10];
int grandparentnum = 0;
String grandparent[] = new String[10];
@SuppressWarnings("rawtypes")
Iterator ite = values.iterator();
while (ite.hasNext()) {
String record = ite.next().toString();
int len = record.length();
int i = 2;
if (len == 0) continue;
char relationType = record.charAt(0);
String childname = new String();
String parentname = new String();
while (record.charAt(i) != '+') {
childname = childname + record.charAt(i);
i++;
}
i = i + 1;
while (i < len) {
parentname = parentname + record.charAt(i);
i++;
}
if (relationType == '1') {
grandchild[grandchildnum] = childname;
grandchildnum++;
} else {
grandparent[grandparentnum] = parentname;
grandparentnum++;
}
}
if (grandparentnum != 0 && grandchildnum != 0) {
for (int m = 0; m < grandchildnum; m++) {
for (int n = 0; n < grandparentnum; n++) {
context.write(new Text(grandchild[m]), new Text(
grandparent[n]));
}
}
}
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args)
.getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println("Usage: in and out");
System.exit(2);
}
@SuppressWarnings("deprecation")
Job job = new Job(conf, "single table join");
job.setJarByClass(ChildGrandparent.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
這是報錯命令:
java.lang.Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: 12
at org.apache.hadoop.mapred.LocalJobRunner$Job.runTasks(LocalJobRunner.java:492)
at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:552)
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: 12
at java.lang.String.charAt(String.java:658)
at job.ChildGrandparent$Map.map(ChildGrandparent.java:40)
at job.ChildGrandparent$Map.map(ChildGrandparent.java:1)
at org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:146)
at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:799)
at org.apache.hadoop.mapred.MapTask.run(MapTask.java:347)
at org.apache.hadoop.mapred.LocalJobRunner$Job$MapTaskRunnable.run(LocalJobRunner.java:271)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
2020-11-19 18:10:17,110 INFO mapreduce.Job: Job job_local553096116_0001 failed with state FAILED due to: NA
2020-11-19 18:10:17,359 INFO mapreduce.Job: Counters: 0
uj5u.com熱心網友回復:
陣列下標越界,看下ChildGrandparent.java:40這一行,列印下獲取的值uj5u.com熱心網友回復:
at job.ChildGrandparent$Map.map(ChildGrandparent.java:40)找下這個類的40行代碼發出來,看是什么問題
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/225629.html
標籤:Java相關
上一篇:我把調ajax的介面都注釋了,為啥圖表中的series資料顯示不出來
下一篇:如何成為Java技術牛人
