2
\$\begingroup\$

I have started working with Cassandra database recently and I was trying to insert some data into one of my column family that I have created. Below is the code by which I am trying to insert into Cassandra database.

In my case I have around 20 columns in my column family so that means I need to add below line

mutator.newColumn("Column Name", "Column Value");

Twenty times in my below code which looks ugly to me. Is there any way, I can simplify the below method either by using reflection or some other way so that If I have more than 20 columns, I should not keep on adding extra line in my below code.

for (int userId = id; userId < id + noOfTasks; userId++) {

    Mutator mutator = Pelops.createMutator(thrift_connection_pool);

    mutator.writeColumns(column_family, String.valueOf(userId),
            mutator.newColumnList(
            mutator.newColumn("a_account", "{\"lv\":[{\"v\":{\"regSiteId\":null,\"userState\":null,\"userId\":" + userId + "},\"cn\":1}],\"lmd\":20130206211109}"), 
            mutator.newColumn("a_advertising", "{\"lv\":[{\"v\":{\"regSiteId\":null,\"userState\":null,\"userId\":" + userId + "},\"cn\":1}],\"lmd\":20130206211109}"), 
            mutator.newColumn("a_avg_selling_price_main_cats", "{\"lv\":[{\"v\":{\"regSiteId\":null,\"userState\":null,\"userId\":" + userId + "},\"cn\":1}],\"lmd\":20130206211109}"), 
            mutator.newColumn("a_cat_and_keyword_rules", "{\"lv\":[{\"v\":{\"regSiteId\":null,\"userState\":null,\"userId\":" + userId + "},\"cn\":1}],\"lmd\":20130206211109}"), 
            mutator.newColumn("a_csa_categories_purchased", "{\"lv\":[{\"v\":{\"regSiteId\":null,\"userState\":null,\"userId\":" + userId + "},\"cn\":1}],\"lmd\":20130206211109}"), 
            mutator.newColumn("a_customer_service", "{\"lv\":[{\"v\":{\"regSiteId\":null,\"userState\":null,\"userId\":" + userId + "},\"cn\":1}],\"lmd\":20130206211109}"), 
            mutator.newColumn("a_demographic", "{\"lv\":[{\"v\":{\"regSiteId\":null,\"userState\":null,\"userId\":" + userId + "},\"cn\":1}],\"lmd\":20130206211109}"), 
            mutator.newColumn("a_favorite_searches", "{\"lv\":[{\"v\":{\"regSiteId\":null,\"userState\":null,\"userId\":" + userId + "},\"cn\":1}],\"lmd\":20130206211109}"), 
            mutator.newColumn("a_favorite_sellers", "{\"lv\":[{\"v\":{\"regSiteId\":null,\"userState\":null,\"userId\":" + userId + "},\"cn\":1}],\"lmd\":20130206211109}"), 
            mutator.newColumn("a_financial", "{\"lv\":[{\"v\":{\"regSiteId\":null,\"userState\":null,\"userId\":" + userId + "},\"cn\":1}],\"lmd\":20130206211109}")
            mutator.newColumn(some othe column, its value)
            .....
            .....
            .....
            ));


    mutator.execute(ConsistencyLevel.ONE);
}

Any help in simplifying the above method will be of great help to me. I was thinking of using a Constant class file like below which will have below methods for each column:

public static void setAccount_epu(final Mutator mutator, final int userId) throws SQLException {

    final String A_ACCOUNT = "{\"lv\":[{\"v\":{\"regSiteId\":null,\"userState\":null,\"userId\":" + userId + "},\"cn\":1}],\"lmd\":20130206211109}";

    mutator.newColumn("a_account", A_ACCOUNT);
}

public static void setAdvertising_epu(final Mutator mutator, final int userId) throws SQLException {

    final String A_ADVERTISING = "{\"lv\":[{\"v\":{\"thirdPartyAdsOnEbay\":null,\"ebayAdsOnThirdParty\":null,\"userId\":" + userId + "},\"cn\":2}],\"lmd\":20130206211109}";

    mutator.newColumn("a_advertising", A_ADVERTISING);
}

public static void setClv_info_epu(final Mutator mutator, final int userId) throws SQLException {

    final String A_CLV_INFO = "{\"lv\":[{\"v\":{\"tenureSiteReg\":null,\"bghtItms\":48,\"pnlValue\":105.478409,\"byrSgmnt\":2,\"cstmrId\":\"814296998\",\"slrRevRnk\":-99.0,\"soldItms\":0,\"slrSgmnt\":6,\"byrRevRnk\":0.013,\"mainAcct\":78,\"gmv\":0.0,\"cstmrRevRnk\":0.021,\"pnlRev\":313.438843,\"cstmrSgmnt\":51,\"gmb\":4674.76,\"totalVal\":142.536293,\"userId\":" + userId + "},\"cn\":42}],\"lmd\":20130206212543}";

    mutator.newColumn("a_clv_info", A_CLV_INFO);

}

public static void setClv_behavior_epu(final Mutator mutator, final int userId) throws SQLException {

    final String A_CLV_BEHAVIOR = "{\"lv\":[{\"v\":{\"behaviorInfo\":\"111:0.56,113:0.33,116:0.10,118:0.03,117:0.00,69:0.00,73:0.00,115:0.00,108:0.00\",\"usrId\":1001116884,\"userId\":" + userId + "},\"cn\":41}],\"lmd\":20130206212543}";

    mutator.newColumn("a_clv_behavior", A_CLV_BEHAVIOR);
}

public static void setClv_churn_epu(final Mutator mutator, final int userId) throws SQLException {

    final String A_CLV_CHURN = "{\"lv\":[{\"v\":{\"churnPrdctvDecileNum\":9,\"churnPrdctvScoreVal\":671.38116,\"userId\":" + userId + "},\"cn\":44}],\"lmd\":20130206212543}";

    mutator.newColumn("a_clv_churn", A_CLV_CHURN);

}

Can I then use some reflection around that?

\$\endgroup\$
1
\$\begingroup\$

It looks like the pattern is alway the same, I am right? Then you could just do:

String commonColumnValue = "{\"lv\":[{\"v\":{\"regSiteId\":null,\"userState\":null,\"userId\":" + userId + "},\"cn\":1}],\"lmd\":20130206211109}"
String[] columns = {"a_account", "a_advertising", ..., "a_financial"} //extend by your needs

for(String columnsItem : columns)
    mutator.newColumn(columnsItem, commonColumnValue);
\$\endgroup\$
  • \$\begingroup\$ No that is for demonstration purpose. In general it will be different for all the columns. \$\endgroup\$ – arsenal Apr 9 '13 at 19:44
  • 3
    \$\begingroup\$ ok. How should we help, if we do not know the code? I do not understand the question then. \$\endgroup\$ – tb- Apr 10 '13 at 4:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.