Home > Back-end >  Prompt Java. Lang. Exception: Java lang. StringIndexOutOfBoundsException for help
Prompt Java. Lang. Exception: Java lang. StringIndexOutOfBoundsException for help

Time:11-21

Experiments are graphs of the form of a given data mining, "big data technology principle and application" zi-yu Lin chapter 7 of the experiment of 5 third child - the parent forms were given in the experiment, to dig the relationship between father and son generation generation relation form between the two generations,

This is the source code:
Package the job;
import java.io.IOException;
Import the Java. Util. Iterator;
Import org, apache hadoop. Conf. Configuration;
Import org, apache hadoop. Fs. The Path;
import org.apache.hadoop.io.Text;
Import org, apache hadoop. Graphs. The Job;
Import org, apache hadoop. Graphs. Mapper;
import org.apache.hadoop.mapreduce.Reducer;
Import org, apache hadoop. Graphs. Lib. Input. FileInputFormat;
Import org, apache hadoop. Graphs. Lib. Output. FileOutputFormat;
Import org, apache hadoop. Util. GenericOptionsParser;
Public class ChildGrandparent {
Public static int time=0;

Public static class Map extends Mapper {

Public void map (Object key, the Text value, Context Context)
Throws IOException, InterruptedException {
String childName=new String ();
String parentName=new String ();
String relationType=new String ();
The String line=value. The toString ();
int i=0;
While (line. The charAt (I)!=' ') {
i++;
}
String [] values={line. The substring (0, I), the line, the substring (I + 1)};
If (values [0]. CompareTo (" child ")!=0) {
ChildName=values [0];
ParentName=values [1];
RelationType="1";
The context. Write (new Text (values [1]), the new Text (relationType + "+"
+ "+" + + childName parentName));
RelationType="2".
The context. Write (new Text (values [0]), the new Text (relationType + "+"
+ "+" + + childName parentName));
}
}
}
Public static class Reduce extends Reducer {

Public void the reduce (Text key Iterable Values, the Context Context)
Throws IOException, InterruptedException {
If (time==0) {
Context. Write (new Text (" grandchild "), and the new Text (" grandparent "));
Time++;
}
Int grandchildnum=0;
String grandchild []=new String [10].
Int grandparentnum=0;
String grandparent []=new String [10].
@ SuppressWarnings (" rawtypes ")
The Iterator ite=values. The Iterator ();
While (ite. HasNext ()) {
String record=ite. Next (). The toString ();
Int len=record. The 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 & lt; Len) {
Parentname=parentname + record. CharAt (I);
i++;
}
If (relationType=='1') {
Grandchild [grandchildnum]=childname;
Grandchildnum++;
} else {
Grandparent [grandparentnum]=parentname;
Grandparentnum++;
}
}
If (grandparentnum!=0 & amp; & Grandchildnum!=0) {
For (int m=0; M & lt; Grandchildnum; M++) {
For (int n=0; N & lt; Grandparentnum; N++) {
The context. Write (new Text (grandchild) [m], new Text (
Grandparent [n]));
}
}
}
}
}
Public static void main (String [] args) throws the Exception {
The Configuration conf=new Configuration ();
String [] otherArgs=new GenericOptionsParser (conf, args)
GetRemainingArgs ();
If (otherArgs length!=2) {
System. Err. Println (" the Usage: in and out ");
System. The 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. The exit (job. WaitForCompletion (true)? 0:1);

}
}

This is an error command:
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. The run (552) LocalJobRunner. Java:
Under Caused by: Java. Lang. StringIndexOutOfBoundsException: String index out of range: 12
The at Java. Lang. String. The charAt (658). The String Java:
At the job. ChildGrandparent $Map. The Map (ChildGrandparent. Java: 40)
At the job. ChildGrandparent $Map. The Map (ChildGrandparent. Java: 1)
At org, apache hadoop. Graphs. Mapper. The run (146) Mapper. Java:
At org, apache hadoop. Mapred. MapTask. RunNewMapper (MapTask. Java: 799)
At org, apache hadoop. Mapred. MapTask. Run (347) MapTask. Java:
At org, apache hadoop. Mapred. LocalJobRunner $$MapTaskRunnable Job. The run (271) LocalJobRunner. Java:
The at Java. Util. Concurrent. Executors $RunnableAdapter. Call (511) Executors. Java:
The at Java. Util. Concurrent. FutureTask. Run (FutureTask. Java: 266)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related