String Index Out of Bounds异常:字符串索引超出范围:38

时间:2015-12-01 01:24:15

标签: java hadoop

正如标题所述,我在使用hadoop时收到了“String Index Out of Bound Exception”。以下是我的地图工作:

public class UniversityMap extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {


 private Text word= new Text();

 public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException
 {
 
 String line = value.toString();
 
 String depart= line.substring(34,38);
 String people= line.substring(44,46);
 
 int p = Integer.parseInt(people);
 IntWritable p1 = new IntWritable(p);
 
    word.set( depart );
   output.collect(word, p1 );
 }
  public static void main(String[] args) {


  }

}

我收到的完整错误是:

hadoop University /root/Documents/test.txt ./output339
15/11/30 17:02:51 INFO Configuration.deprecation: session.id is deprecated. Instead, use dfs.metrics.session-id
15/11/30 17:02:51 INFO jvm.JvmMetrics: Initializing JVM Metrics with processName=JobTracker, sessionId=
15/11/30 17:02:51 INFO jvm.JvmMetrics: Cannot initialize JVM Metrics with processName=JobTracker, sessionId= - already initialized
15/11/30 17:02:51 WARN mapreduce.JobResourceUploader: Hadoop command-line option parsing not performed. Implement the Tool interface and execute your application with ToolRunner to remedy this.
15/11/30 17:02:51 WARN mapreduce.JobResourceUploader: No job jar file set.  User classes may not be found. See Job or Job#setJar(String).
15/11/30 17:02:51 INFO mapred.FileInputFormat: Total input paths to process : 1
15/11/30 17:02:51 INFO mapreduce.JobSubmitter: number of splits:1
15/11/30 17:02:51 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_local773905140_0001
15/11/30 17:02:51 INFO mapreduce.Job: The url to track the job: http://localhost:8080/
15/11/30 17:02:51 INFO mapreduce.Job: Running job: job_local773905140_0001
15/11/30 17:02:51 INFO mapred.LocalJobRunner: OutputCommitter set in config null
15/11/30 17:02:51 INFO mapred.LocalJobRunner: OutputCommitter is org.apache.hadoop.mapred.FileOutputCommitter
15/11/30 17:02:51 INFO output.FileOutputCommitter: File Output Committer Algorithm version is 1
15/11/30 17:02:51 INFO mapred.LocalJobRunner: Waiting for map tasks
15/11/30 17:02:51 INFO mapred.LocalJobRunner: Starting task: attempt_local773905140_0001_m_000000_0
15/11/30 17:02:51 INFO output.FileOutputCommitter: File Output Committer Algorithm version is 1
15/11/30 17:02:51 INFO mapred.Task:  Using ResourceCalculatorProcessTree : [ ]
15/11/30 17:02:51 INFO mapred.MapTask: Processing split: file:/root/Documents/test.txt:0+12
15/11/30 17:02:51 INFO mapred.MapTask: numReduceTasks: 1
15/11/30 17:02:52 INFO mapred.MapTask: (EQUATOR) 0 kvi 26214396(104857584)
15/11/30 17:02:52 INFO mapred.MapTask: mapreduce.task.io.sort.mb: 100
15/11/30 17:02:52 INFO mapred.MapTask: soft limit at 83886080
15/11/30 17:02:52 INFO mapred.MapTask: bufstart = 0; bufvoid = 104857600
15/11/30 17:02:52 INFO mapred.MapTask: kvstart = 26214396; length = 6553600
15/11/30 17:02:52 INFO mapred.MapTask: Map output collector class = org.apache.hadoop.mapred.MapTask$MapOutputBuffer
15/11/30 17:02:52 INFO mapred.LocalJobRunner: map task executor complete.
15/11/30 17:02:52 WARN mapred.LocalJobRunner: job_local773905140_0001
java.lang.Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: 38
        at org.apache.hadoop.mapred.LocalJobRunner$Job.runTasks(LocalJobRunner.java:462)
        at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:522)
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: 38
        at java.lang.String.substring(String.java:1963)
        at UniversityMap.map(UniversityMap.java:18)
        at UniversityMap.map(UniversityMap.java:8)
        at org.apache.hadoop.mapred.MapRunner.run(MapRunner.java:54)
        at org.apache.hadoop.mapred.MapTask.runOldMapper(MapTask.java:453)
        at org.apache.hadoop.mapred.MapTask.run(MapTask.java:343)
        at org.apache.hadoop.mapred.LocalJobRunner$Job$MapTaskRunnable.run(LocalJobRunner.java:243)
        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:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
15/11/30 17:02:52 INFO mapreduce.Job: Job job_local773905140_0001 running in uber mode : false
15/11/30 17:02:52 INFO mapreduce.Job:  map 0% reduce 0%
15/11/30 17:02:52 INFO mapreduce.Job: Job job_local773905140_0001 failed with state FAILED due to: NA
15/11/30 17:02:52 INFO mapreduce.Job: Counters: 0
Exception in thread "main" java.io.IOException: Job failed!
        at org.apache.hadoop.mapred.JobClient.runJob(JobClient.java:865)
        at University.main(University.java:32)

我相信我得到了这个例外,因为有4个字符长或2个字符长的离开值。我是否正确当它看到不是来自(34,38)的离开值时,会抛出异常?

1 个答案:

答案 0 :(得分:0)

&#34;字符串索引超出范围&#34;:你得到的长度超过了你的行的长度。

根据我的代码理解,在以下两行中:

ppA

这意味着该行的长度必须为46个或更多。是对的吗?!所以,我认为你可以检查:    if(line.length&gt; 46){      //实施您的业务。    }

如果该行无效,请跳过它。

希望得到这个帮助。