Thursday, January 30, 2014

Move Column in Datastore v2

Someone asked via email about my post on moving columns upwards. The question was about having several columns, not just one.
I’m not gonna go over the whole code now, but instead of having a list of information as a NEW_COL_INFO, have a list of column infos.

NEW_COL_INFO = [
      ['C5',      101,  10]
,     ['C6',      99,   10]
,     ['C7',      120,  10]
                        ];

Now you gotta put the whole main part within a loop, so that each column is processed. But there’s another problem where the columns are not ordered. If we try to move C5 before C6, then the START_POSITION and LENGTH calculation would be all wrong. That’s why we need to sort the list by START_POSITION.

This is the code that I ended up with. As I said in the earlier post; it gets the job done.

import oracle.odi.blablabla;
TABLE_A = 'MY_FILE';
MODEL_A = 'MY_MODEL'
//COLUMN_NAME,    START_POSITION,   LENGTH
NEW_COL_INFO = [
      ['C5',      101,  10]
,     ['C6',      99,   10]
,     ['C7',      120,  10]
                        ];
//sort
NEW_COL_INFO=NEW_COL_INFO.sort{p1, p2 -> p1[1] <=> p2[1] }

ITransactionDefinition txnDef = new DefaultTransactionDefinition();
ITransactionManager tm = odiInstance.getTransactionManager();
ITransactionStatus txnStatus = tm.getTransaction(txnDef);
IOdiEntityManager mgr = odiInstance.getTransactionalEntityManager();

OdiDataStore table_a_datastore = ((IOdiDataStoreFinder)mgr.getFinder(OdiDataStore.class)).findByName(TABLE_A, MODEL_A);
println(table_a_datastore.getName());

for(int j=0; j<NEW_COL_INFO.size(); j++){
      COLUMN_NAME = NEW_COL_INFO[j][0]
      START_POSITION = NEW_COL_INFO[j][1]
      LENGTH = NEW_COL_INFO[j][2]


      OdiColumn[] col_list = table_a_datastore.getColumns();
      is_move_up = -1
      for(OdiColumn col : col_list){
            if(col.getName().equals(COLUMN_NAME)){
                  if(col.getFileDescriptor().getStartPosition() > START_POSITION){
                        is_move_up = 1
                  } else if(col.getFileDescriptor().getStartPosition() == START_POSITION){
                        is_move_up = 0
                  }
            }
      }

      if(is_move_up == 1){
            move_the_rest = 0
            for(OdiColumn col : col_list){
                  if(col.getFileDescriptor().getStartPosition() == START_POSITION){
                        move_the_rest = 1
                  }
                  if(col.getName().equals(COLUMN_NAME)){
                        col.getFileDescriptor().setStartPosition(START_POSITION);
                        col.getFileDescriptor().setBytes(LENGTH);
                        move_the_rest = 0
                  } else if(move_the_rest == 1){
                        old_start_position = col.getFileDescriptor().getStartPosition()
                        col.getFileDescriptor().setStartPosition(old_start_position + LENGTH);
                  }
            }
      } else if(is_move_up == 0) {
            println('Kolon zaten olmasi gereken yerde')
      } else {
            println('Kolonu yukari tasiyabiliyoruz. Asagi indiremiyoruz.')
      }

      //error checking
      col_list = table_a_datastore.getColumns();
      i = 1
      err = 0
      for(OdiColumn col : col_list){
            println('Calculated pos:' + i + '\tCol Name:' + col.getName() + '\tCol Index:' + col.getPosition() + '\tStart Pos:' + col.getFileDescriptor().getStartPosition() + '\tLength:' + col.getFileDescriptor().getBytes());
            if(i != col.getFileDescriptor().getStartPosition()){
                  err = 1
                  println('err')
            }
            i = i + col.getFileDescriptor().getBytes()
      }
}
if(err == 0){
      println('Saving...');
      tm.commit(txnStatus);
      println('Completed.');

}

2 comments:

  1. Saturam provides a platform to integrate your entire data infrastructure into one secure stronghold in order to provide you with greater control over your organization's data while increasing the ease of operating all aspects of your business. Analytics pipelines on your data lake will improve the efficiency of your entire organization while improving your control over your enterprise's data and the valuable, confidential data of your customers.

    ReplyDelete