Unable to find the cause of String Index Out of Bound Exception in my MapReduce code












0















I am trying out a MapReduce code written in Java. I need to get the number of employees who were promoted in each department but still left the organization. I am trying to pass concatenated value of department and promotion as the key and resignation as the value.



Sample Data



left promotion_last_5years deptartment



1, 0, sales



1, 1, sales



1, 1, hr



1, 0, sales



Mapper Code:



public void map(LongWritable key, Text text, Context context) throws IOException, InterruptedException
{
String row = text.toString();
String values = row.trim().split(",");
int left = 0;
int promotion = 0;
String dept = "";
String DeptPromoted = "";
try
{
if(values.length == 10 && !header.equals(row))
{
left = Integer.parseInt(values[6]);
promotion = Integer.parseInt(values[7]);
dept = values[8];
DeptPromoted = dept+"-"+values[7]; // sales-0
}
}
catch (Exception e)
{
e.printStackTrace();
}
context.write(new Text(DeptPromoted), new IntWritable(left)); //sales-0 1
}


Below is my reducer code where I am using substring to seperate values of department and promotion and then use these values to calculate the number of employees promoted but resigned.



Reducer Code:



public void reduce(Text key, Iterable<IntWritable> values, Context context throws IOException, InterruptedException
{
//sales-0 1
int count = 0;
String str = "";
str = key.toString(); //sales-0
int len = str.length(); //7
char L = str.charAt(len - 1);
if (L == '1')
{
for (IntWritable val: values)
{
if(val.get() == 1)
{
count++;
}
}
}
context.write(key, new IntWritable(count));
}


I believe the StringIndexOutofBoundException is from the reducer where I am trying to get the character value at the end of the string. Can someone help to resolve the error below?



Error: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.charAt(String.java:658)
at com.df.hra_promleft.PromLeftReducer.reduce(PromLeftReducer.java:18)
at com.df.hra_promleft.PromLeftReducer.reduce(PromLeftReducer.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.YarnChild$2.run(YarnChild.java:168)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:422)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1642)
at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:163)









share|improve this question

























  • Can you please provide the error log?

    – M S Hossain
    Nov 28 '18 at 4:14











  • I have added the error now.

    – Rich
    Nov 29 '18 at 2:21
















0















I am trying out a MapReduce code written in Java. I need to get the number of employees who were promoted in each department but still left the organization. I am trying to pass concatenated value of department and promotion as the key and resignation as the value.



Sample Data



left promotion_last_5years deptartment



1, 0, sales



1, 1, sales



1, 1, hr



1, 0, sales



Mapper Code:



public void map(LongWritable key, Text text, Context context) throws IOException, InterruptedException
{
String row = text.toString();
String values = row.trim().split(",");
int left = 0;
int promotion = 0;
String dept = "";
String DeptPromoted = "";
try
{
if(values.length == 10 && !header.equals(row))
{
left = Integer.parseInt(values[6]);
promotion = Integer.parseInt(values[7]);
dept = values[8];
DeptPromoted = dept+"-"+values[7]; // sales-0
}
}
catch (Exception e)
{
e.printStackTrace();
}
context.write(new Text(DeptPromoted), new IntWritable(left)); //sales-0 1
}


Below is my reducer code where I am using substring to seperate values of department and promotion and then use these values to calculate the number of employees promoted but resigned.



Reducer Code:



public void reduce(Text key, Iterable<IntWritable> values, Context context throws IOException, InterruptedException
{
//sales-0 1
int count = 0;
String str = "";
str = key.toString(); //sales-0
int len = str.length(); //7
char L = str.charAt(len - 1);
if (L == '1')
{
for (IntWritable val: values)
{
if(val.get() == 1)
{
count++;
}
}
}
context.write(key, new IntWritable(count));
}


I believe the StringIndexOutofBoundException is from the reducer where I am trying to get the character value at the end of the string. Can someone help to resolve the error below?



Error: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.charAt(String.java:658)
at com.df.hra_promleft.PromLeftReducer.reduce(PromLeftReducer.java:18)
at com.df.hra_promleft.PromLeftReducer.reduce(PromLeftReducer.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.YarnChild$2.run(YarnChild.java:168)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:422)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1642)
at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:163)









share|improve this question

























  • Can you please provide the error log?

    – M S Hossain
    Nov 28 '18 at 4:14











  • I have added the error now.

    – Rich
    Nov 29 '18 at 2:21














0












0








0








I am trying out a MapReduce code written in Java. I need to get the number of employees who were promoted in each department but still left the organization. I am trying to pass concatenated value of department and promotion as the key and resignation as the value.



Sample Data



left promotion_last_5years deptartment



1, 0, sales



1, 1, sales



1, 1, hr



1, 0, sales



Mapper Code:



public void map(LongWritable key, Text text, Context context) throws IOException, InterruptedException
{
String row = text.toString();
String values = row.trim().split(",");
int left = 0;
int promotion = 0;
String dept = "";
String DeptPromoted = "";
try
{
if(values.length == 10 && !header.equals(row))
{
left = Integer.parseInt(values[6]);
promotion = Integer.parseInt(values[7]);
dept = values[8];
DeptPromoted = dept+"-"+values[7]; // sales-0
}
}
catch (Exception e)
{
e.printStackTrace();
}
context.write(new Text(DeptPromoted), new IntWritable(left)); //sales-0 1
}


Below is my reducer code where I am using substring to seperate values of department and promotion and then use these values to calculate the number of employees promoted but resigned.



Reducer Code:



public void reduce(Text key, Iterable<IntWritable> values, Context context throws IOException, InterruptedException
{
//sales-0 1
int count = 0;
String str = "";
str = key.toString(); //sales-0
int len = str.length(); //7
char L = str.charAt(len - 1);
if (L == '1')
{
for (IntWritable val: values)
{
if(val.get() == 1)
{
count++;
}
}
}
context.write(key, new IntWritable(count));
}


I believe the StringIndexOutofBoundException is from the reducer where I am trying to get the character value at the end of the string. Can someone help to resolve the error below?



Error: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.charAt(String.java:658)
at com.df.hra_promleft.PromLeftReducer.reduce(PromLeftReducer.java:18)
at com.df.hra_promleft.PromLeftReducer.reduce(PromLeftReducer.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.YarnChild$2.run(YarnChild.java:168)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:422)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1642)
at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:163)









share|improve this question
















I am trying out a MapReduce code written in Java. I need to get the number of employees who were promoted in each department but still left the organization. I am trying to pass concatenated value of department and promotion as the key and resignation as the value.



Sample Data



left promotion_last_5years deptartment



1, 0, sales



1, 1, sales



1, 1, hr



1, 0, sales



Mapper Code:



public void map(LongWritable key, Text text, Context context) throws IOException, InterruptedException
{
String row = text.toString();
String values = row.trim().split(",");
int left = 0;
int promotion = 0;
String dept = "";
String DeptPromoted = "";
try
{
if(values.length == 10 && !header.equals(row))
{
left = Integer.parseInt(values[6]);
promotion = Integer.parseInt(values[7]);
dept = values[8];
DeptPromoted = dept+"-"+values[7]; // sales-0
}
}
catch (Exception e)
{
e.printStackTrace();
}
context.write(new Text(DeptPromoted), new IntWritable(left)); //sales-0 1
}


Below is my reducer code where I am using substring to seperate values of department and promotion and then use these values to calculate the number of employees promoted but resigned.



Reducer Code:



public void reduce(Text key, Iterable<IntWritable> values, Context context throws IOException, InterruptedException
{
//sales-0 1
int count = 0;
String str = "";
str = key.toString(); //sales-0
int len = str.length(); //7
char L = str.charAt(len - 1);
if (L == '1')
{
for (IntWritable val: values)
{
if(val.get() == 1)
{
count++;
}
}
}
context.write(key, new IntWritable(count));
}


I believe the StringIndexOutofBoundException is from the reducer where I am trying to get the character value at the end of the string. Can someone help to resolve the error below?



Error: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.charAt(String.java:658)
at com.df.hra_promleft.PromLeftReducer.reduce(PromLeftReducer.java:18)
at com.df.hra_promleft.PromLeftReducer.reduce(PromLeftReducer.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.YarnChild$2.run(YarnChild.java:168)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:422)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1642)
at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:163)






java mapreduce






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 29 '18 at 2:21







Rich

















asked Nov 28 '18 at 3:51









RichRich

104




104













  • Can you please provide the error log?

    – M S Hossain
    Nov 28 '18 at 4:14











  • I have added the error now.

    – Rich
    Nov 29 '18 at 2:21



















  • Can you please provide the error log?

    – M S Hossain
    Nov 28 '18 at 4:14











  • I have added the error now.

    – Rich
    Nov 29 '18 at 2:21

















Can you please provide the error log?

– M S Hossain
Nov 28 '18 at 4:14





Can you please provide the error log?

– M S Hossain
Nov 28 '18 at 4:14













I have added the error now.

– Rich
Nov 29 '18 at 2:21





I have added the error now.

– Rich
Nov 29 '18 at 2:21












2 Answers
2






active

oldest

votes


















0














String str = "";
str = key.toString(); //sales-0
int len = str.length(); //7
char L = str.charAt(len - 1);


If key is an empty text then len = 0 so str.charAt(0-1) is str.charAt(-1) it causes StringIndexOutOfBoundsException. So please check if the Text key is empty or not.






share|improve this answer
























  • Thanks a lot! After just adding an if condition to check if the key is not empty, it worked!

    – Rich
    Dec 1 '18 at 0:25



















0














The IndexOutOfBoundException is probably because of the code:-



char L = str.charAt(len - 1);


Here is the reason:-



Your data includes:- 1 0 sales
and your code is splitting the data on the basis of "," (comma):-



String row = text.toString();
String values = row.trim().split(",");


Considering you are using normal TextInputFormat. your values will always have only one record.



In that scenario, the key to reducer will always be blank i.e. ""



so calling



int len = str.length(); //7
char L = str.charAt(len - 1);


should throw a StringIndexOutOfBoundsException.



a sample code:-



String s = ""
int length = s.length()
Character c = s.charAt(length - 1)


My suggestion is to make appropriate changes in your code and add necessary checks.






share|improve this answer


























  • I missed to add comma to the sample data here. My data is in a CSV file. Will it still have the issue that you had pointed out? I am not clear why the key would be blank.

    – Rich
    Nov 29 '18 at 2:23











  • It depends on the data, you can do a dry run with data to understand the issue. In the code, you are only populating the key If you have 10 elements in the array. That means you should have at least 9 commas in your data. If you are uncertain of this then you should do a check before doing string.charAt()

    – Garvit
    Nov 29 '18 at 6:14











  • Thanks for your help! My code works now.

    – Rich
    Dec 1 '18 at 0:26











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53511850%2funable-to-find-the-cause-of-string-index-out-of-bound-exception-in-my-mapreduce%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














String str = "";
str = key.toString(); //sales-0
int len = str.length(); //7
char L = str.charAt(len - 1);


If key is an empty text then len = 0 so str.charAt(0-1) is str.charAt(-1) it causes StringIndexOutOfBoundsException. So please check if the Text key is empty or not.






share|improve this answer
























  • Thanks a lot! After just adding an if condition to check if the key is not empty, it worked!

    – Rich
    Dec 1 '18 at 0:25
















0














String str = "";
str = key.toString(); //sales-0
int len = str.length(); //7
char L = str.charAt(len - 1);


If key is an empty text then len = 0 so str.charAt(0-1) is str.charAt(-1) it causes StringIndexOutOfBoundsException. So please check if the Text key is empty or not.






share|improve this answer
























  • Thanks a lot! After just adding an if condition to check if the key is not empty, it worked!

    – Rich
    Dec 1 '18 at 0:25














0












0








0







String str = "";
str = key.toString(); //sales-0
int len = str.length(); //7
char L = str.charAt(len - 1);


If key is an empty text then len = 0 so str.charAt(0-1) is str.charAt(-1) it causes StringIndexOutOfBoundsException. So please check if the Text key is empty or not.






share|improve this answer













String str = "";
str = key.toString(); //sales-0
int len = str.length(); //7
char L = str.charAt(len - 1);


If key is an empty text then len = 0 so str.charAt(0-1) is str.charAt(-1) it causes StringIndexOutOfBoundsException. So please check if the Text key is empty or not.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 30 '18 at 4:18









M S HossainM S Hossain

483310




483310













  • Thanks a lot! After just adding an if condition to check if the key is not empty, it worked!

    – Rich
    Dec 1 '18 at 0:25



















  • Thanks a lot! After just adding an if condition to check if the key is not empty, it worked!

    – Rich
    Dec 1 '18 at 0:25

















Thanks a lot! After just adding an if condition to check if the key is not empty, it worked!

– Rich
Dec 1 '18 at 0:25





Thanks a lot! After just adding an if condition to check if the key is not empty, it worked!

– Rich
Dec 1 '18 at 0:25













0














The IndexOutOfBoundException is probably because of the code:-



char L = str.charAt(len - 1);


Here is the reason:-



Your data includes:- 1 0 sales
and your code is splitting the data on the basis of "," (comma):-



String row = text.toString();
String values = row.trim().split(",");


Considering you are using normal TextInputFormat. your values will always have only one record.



In that scenario, the key to reducer will always be blank i.e. ""



so calling



int len = str.length(); //7
char L = str.charAt(len - 1);


should throw a StringIndexOutOfBoundsException.



a sample code:-



String s = ""
int length = s.length()
Character c = s.charAt(length - 1)


My suggestion is to make appropriate changes in your code and add necessary checks.






share|improve this answer


























  • I missed to add comma to the sample data here. My data is in a CSV file. Will it still have the issue that you had pointed out? I am not clear why the key would be blank.

    – Rich
    Nov 29 '18 at 2:23











  • It depends on the data, you can do a dry run with data to understand the issue. In the code, you are only populating the key If you have 10 elements in the array. That means you should have at least 9 commas in your data. If you are uncertain of this then you should do a check before doing string.charAt()

    – Garvit
    Nov 29 '18 at 6:14











  • Thanks for your help! My code works now.

    – Rich
    Dec 1 '18 at 0:26
















0














The IndexOutOfBoundException is probably because of the code:-



char L = str.charAt(len - 1);


Here is the reason:-



Your data includes:- 1 0 sales
and your code is splitting the data on the basis of "," (comma):-



String row = text.toString();
String values = row.trim().split(",");


Considering you are using normal TextInputFormat. your values will always have only one record.



In that scenario, the key to reducer will always be blank i.e. ""



so calling



int len = str.length(); //7
char L = str.charAt(len - 1);


should throw a StringIndexOutOfBoundsException.



a sample code:-



String s = ""
int length = s.length()
Character c = s.charAt(length - 1)


My suggestion is to make appropriate changes in your code and add necessary checks.






share|improve this answer


























  • I missed to add comma to the sample data here. My data is in a CSV file. Will it still have the issue that you had pointed out? I am not clear why the key would be blank.

    – Rich
    Nov 29 '18 at 2:23











  • It depends on the data, you can do a dry run with data to understand the issue. In the code, you are only populating the key If you have 10 elements in the array. That means you should have at least 9 commas in your data. If you are uncertain of this then you should do a check before doing string.charAt()

    – Garvit
    Nov 29 '18 at 6:14











  • Thanks for your help! My code works now.

    – Rich
    Dec 1 '18 at 0:26














0












0








0







The IndexOutOfBoundException is probably because of the code:-



char L = str.charAt(len - 1);


Here is the reason:-



Your data includes:- 1 0 sales
and your code is splitting the data on the basis of "," (comma):-



String row = text.toString();
String values = row.trim().split(",");


Considering you are using normal TextInputFormat. your values will always have only one record.



In that scenario, the key to reducer will always be blank i.e. ""



so calling



int len = str.length(); //7
char L = str.charAt(len - 1);


should throw a StringIndexOutOfBoundsException.



a sample code:-



String s = ""
int length = s.length()
Character c = s.charAt(length - 1)


My suggestion is to make appropriate changes in your code and add necessary checks.






share|improve this answer















The IndexOutOfBoundException is probably because of the code:-



char L = str.charAt(len - 1);


Here is the reason:-



Your data includes:- 1 0 sales
and your code is splitting the data on the basis of "," (comma):-



String row = text.toString();
String values = row.trim().split(",");


Considering you are using normal TextInputFormat. your values will always have only one record.



In that scenario, the key to reducer will always be blank i.e. ""



so calling



int len = str.length(); //7
char L = str.charAt(len - 1);


should throw a StringIndexOutOfBoundsException.



a sample code:-



String s = ""
int length = s.length()
Character c = s.charAt(length - 1)


My suggestion is to make appropriate changes in your code and add necessary checks.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 28 '18 at 5:46

























answered Nov 28 '18 at 4:35









GarvitGarvit

154




154













  • I missed to add comma to the sample data here. My data is in a CSV file. Will it still have the issue that you had pointed out? I am not clear why the key would be blank.

    – Rich
    Nov 29 '18 at 2:23











  • It depends on the data, you can do a dry run with data to understand the issue. In the code, you are only populating the key If you have 10 elements in the array. That means you should have at least 9 commas in your data. If you are uncertain of this then you should do a check before doing string.charAt()

    – Garvit
    Nov 29 '18 at 6:14











  • Thanks for your help! My code works now.

    – Rich
    Dec 1 '18 at 0:26



















  • I missed to add comma to the sample data here. My data is in a CSV file. Will it still have the issue that you had pointed out? I am not clear why the key would be blank.

    – Rich
    Nov 29 '18 at 2:23











  • It depends on the data, you can do a dry run with data to understand the issue. In the code, you are only populating the key If you have 10 elements in the array. That means you should have at least 9 commas in your data. If you are uncertain of this then you should do a check before doing string.charAt()

    – Garvit
    Nov 29 '18 at 6:14











  • Thanks for your help! My code works now.

    – Rich
    Dec 1 '18 at 0:26

















I missed to add comma to the sample data here. My data is in a CSV file. Will it still have the issue that you had pointed out? I am not clear why the key would be blank.

– Rich
Nov 29 '18 at 2:23





I missed to add comma to the sample data here. My data is in a CSV file. Will it still have the issue that you had pointed out? I am not clear why the key would be blank.

– Rich
Nov 29 '18 at 2:23













It depends on the data, you can do a dry run with data to understand the issue. In the code, you are only populating the key If you have 10 elements in the array. That means you should have at least 9 commas in your data. If you are uncertain of this then you should do a check before doing string.charAt()

– Garvit
Nov 29 '18 at 6:14





It depends on the data, you can do a dry run with data to understand the issue. In the code, you are only populating the key If you have 10 elements in the array. That means you should have at least 9 commas in your data. If you are uncertain of this then you should do a check before doing string.charAt()

– Garvit
Nov 29 '18 at 6:14













Thanks for your help! My code works now.

– Rich
Dec 1 '18 at 0:26





Thanks for your help! My code works now.

– Rich
Dec 1 '18 at 0:26


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53511850%2funable-to-find-the-cause-of-string-index-out-of-bound-exception-in-my-mapreduce%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)