Skip to main content
deleted 26 characters in body; edited tags
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I'm doing this in Hadoop Java where I'm reading a String. The string is huge that has been tokenized and put in an array. It has key-value pairs but they are not in any order. I want this order to be rigid so I can load that as a table. So in SQL, if I select a column (after loading this in a table), all the keys of one type should be in colA.

I'm checking each word of the String array and copying them in a new string in a fixed position. The way I thought of doing this is using if else ladder like this: //row is the tokenized unordered String

//row is the tokenized unordered String

String[] newRow = new String[150];

for (int i = 0; i < row.length; ++i) {

    if(row[i].equals("token1")){
               newRow[0] = row[i]; //key
               newRow[1] = row[i+1];//value
           }

           else if(row[i].equals("token2")){
               newRow[2] = row[i];
               newRow[3] = row[i+1];
           }//...and so on. Elseif ladder at least is at least 100 long.

I wanted to know if there is a more efficient way to do this?

PS: I'm not sorting the string. Example: row1row1 String is {apple,good,banana,bad}, row2row2 String is {banana,good,apple,bad} where appleapple and bananabanana are keys. Now in my output I will have two records with say apple as the first key and then banana. So output will be : newRow1newRow1: {apple,good,banana,bad}  , newRow2newRow2: {apple,bad,banana,good}. Essentially I'm rearranging all input to a fixed output.

I'm doing this in Hadoop Java where I'm reading a String. The string is huge that has been tokenized and put in an array. It has key-value pairs but they are not in any order. I want this order to be rigid so I can load that as a table. So in SQL, if I select a column (after loading this in a table), all the keys of one type should be in colA.

I'm checking each word of the String array and copying them in a new string in a fixed position. The way I thought of doing this is using if else ladder like this: //row is the tokenized unordered String

 String[] newRow = new String[150];

for (int i = 0; i < row.length; ++i) {

if(row[i].equals("token1")){
               newRow[0] = row[i]; //key
               newRow[1] = row[i+1];//value
           }

           else if(row[i].equals("token2")){
               newRow[2] = row[i];
               newRow[3] = row[i+1];
           }//...and so on. Elseif ladder at least is at least 100 long.

I wanted to know if there is a more efficient way to do this?

PS: I'm not sorting the string. Example: row1 String is {apple,good,banana,bad}, row2 String is {banana,good,apple,bad} where apple and banana are keys. Now in my output I will have two records with say apple as the first key and then banana. So output will be : newRow1: {apple,good,banana,bad}  , newRow2: {apple,bad,banana,good}. Essentially I'm rearranging all input to a fixed output.

I'm doing this in Hadoop Java where I'm reading a String. The string is huge that has been tokenized and put in an array. It has key-value pairs but they are not in any order. I want this order to be rigid so I can load that as a table. So in SQL, if I select a column (after loading this in a table), all the keys of one type should be in colA.

I'm checking each word of the String array and copying them in a new string in a fixed position. The way I thought of doing this is using if else ladder like this:

//row is the tokenized unordered String

String[] newRow = new String[150];

for (int i = 0; i < row.length; ++i) {

    if(row[i].equals("token1")){
        newRow[0] = row[i]; //key
        newRow[1] = row[i+1];//value
    }

    else if(row[i].equals("token2")){
        newRow[2] = row[i];
        newRow[3] = row[i+1];
    }//...and so on. Elseif ladder at least is at least 100 long.

I wanted to know if there is a more efficient way to do this?

PS: I'm not sorting the string. Example: row1 String is {apple,good,banana,bad}, row2 String is {banana,good,apple,bad} where apple and banana are keys. Now in my output I will have two records with say apple as the first key and then banana. So output will be : newRow1: {apple,good,banana,bad}, newRow2: {apple,bad,banana,good}. Essentially I'm rearranging all input to a fixed output.

Tweeted twitter.com/#!/StackCodeReview/status/362040860176678912
Source Link

Efficient way to copy unordered String into ordered String

I'm doing this in Hadoop Java where I'm reading a String. The string is huge that has been tokenized and put in an array. It has key-value pairs but they are not in any order. I want this order to be rigid so I can load that as a table. So in SQL, if I select a column (after loading this in a table), all the keys of one type should be in colA.

I'm checking each word of the String array and copying them in a new string in a fixed position. The way I thought of doing this is using if else ladder like this: //row is the tokenized unordered String

 String[] newRow = new String[150];

for (int i = 0; i < row.length; ++i) {

if(row[i].equals("token1")){
               newRow[0] = row[i]; //key
               newRow[1] = row[i+1];//value
           }

           else if(row[i].equals("token2")){
               newRow[2] = row[i];
               newRow[3] = row[i+1];
           }//...and so on. Elseif ladder at least is at least 100 long.

I wanted to know if there is a more efficient way to do this?

PS: I'm not sorting the string. Example: row1 String is {apple,good,banana,bad}, row2 String is {banana,good,apple,bad} where apple and banana are keys. Now in my output I will have two records with say apple as the first key and then banana. So output will be : newRow1: {apple,good,banana,bad} , newRow2: {apple,bad,banana,good}. Essentially I'm rearranging all input to a fixed output.