java - Inserting to database efficiently by removing repeated code -
i have started working cassandra database
recently. , trying insert data 1 of column family
have created.
below code trying insert cassandra database.
in case have around 20 columns
in column family means need add below line
mutator.newcolumn("column name", "column value");
twenty times in below code looks ugly me. there way, can simplify below method either using reflection or other way if have more 20 columns, should not keep on adding line in 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, value) ..... ..... ..... )); mutator.execute(consistencylevel.one); }
any in simplifying above method of great me. may can use reflection here? thoughts?
you can predefine columnlist. let's pass in map of names , string versions of values in variable named "map". code might this:
mutator mutator = pelops.createmutator(thrift_connection_pool); arraylist<column> columnlist = new arraylist<column>(); (map.entry<string, string> entry : map.entryset()) { columnlist.add(mutator.newcolumn(entry.getkey(), entry.getvalue())); } mutator.writecolumns(column_family, string.valueof(userid), columnlist);
Comments
Post a Comment