java - Exporting Data from cassandra to mongodb -
i have program exports data directly cassandra mongodb.it works fine but, doesn't copies counter type column cassandra mongodb. leaves blank.
public class export { static string keyspace = "db1"; static string table = "results"; static string host = "localhost"; @suppresswarnings("deprecation") static mongo mongo = new mongo("localhost", 27017); @suppresswarnings("deprecation") static db db = mongo.getdb("mydb"); static dbcollection collection = db.getcollection("results"); public static void main( string[] args ) throws ioexception { cluster.builder clusterbuilder = cluster.builder() .addcontactpoints(host); cluster cluster = clusterbuilder.build(); session session = cluster.connect(keyspace); statement stmt = new simplestatement("select * " + table); stmt.setfetchsize(1); resultset rs = session.execute(stmt); iterator<row> iter = rs.iterator(); arraylist<string> colname = new arraylist<string>(); row row1 = iter.next(); if (row1 != null) { (definition key1 : row1.getcolumndefinitions().aslist()) { string val = key1.getname(); colname.add(val); } } while (!rs.isfullyfetched()) { rs.fetchmoreresults(); row row = iter.next(); if (row != null) { basicdbobject document = new basicdbobject(); arraylist<string> ele = new arraylist<string>(); (definition key : row.getcolumndefinitions().aslist()) { string val = mygetvalue(key, row); ele.add(val); } (int = 0; < ele.size() && < colname.size(); i++) { document.put(colname.get(i), ele.get(i)); } collection.insert(document); } } session.close(); cluster.close(); } public static string mygetvalue(definition key, row row) { string str = ""; if (key != null) { string col = key.getname(); try { if (key.gettype() == datatype.cdouble()) { str = new double(row.getdouble(col)).tostring(); } else if (key.gettype() == datatype.cint()) { str = new integer(row.getint(col)).tostring(); } else if (key.gettype() == datatype.uuid()) { str = row.getuuid(col).tostring(); } else if (key.gettype() == datatype.cfloat()) { str = new float(row.getfloat(col)).tostring(); } else if (key.gettype() == datatype.counter()) { str = new float(row.getfloat(col)).tostring(); } else if (key.gettype() == datatype.timestamp()) { str = row.getdate(col).tostring(); simpledateformat fmt = new simpledateformat("yyyy-mm-dd hh:mm:ssz"); str = fmt.format(row.getdate(col)); } else { str = row.getstring(col); } } catch (exception e) { str = ""; } } return str; } }
and data gets exported mongodb :
{ "_id" : objectid("558c0202209c02284d30df05"), "term" : "gamma red eye tennis dampener", "year" : "2015", "month" : "05", "day" : "29", "hour" : "09", "dayofyear" : "176", "weekofyear" : "26", "productcount" : "1", "count" : "" } { "_id" : objectid("558c0202209c02284d30df06"), "term" : "headtie", "year" : "2015", "month" : "06", "day" : "01", "hour" : "05", "dayofyear" : "176", "weekofyear" : "26", "productcount" : "29", "count" : "" } { "_id" : objectid("558c0202209c02284d30df07"), "term" : "dryed roller court", "year" : "2015", "month" : "06", "day" : "08", "hour" : "15", "dayofyear" : "176", "weekofyear" : "26", "productcount" : "362", "count" : "" }
i know problem is.the function below not inserting value of counter mongodb since val defined string conflicts counter data type of cassandra.
while (!rs.isfullyfetched()) { rs.fetchmoreresults(); row row = iter.next(); if (row != null) { basicdbobject document = new basicdbobject(); arraylist<string> ele = new arraylist<string>(); (definition key : row.getcolumndefinitions().aslist()) { string val = mygetvalue(key, row); system.out.println(val); ele.add(val); } (int = 0; < ele.size() && < colname.size(); i++) { document.put(colname.get(i), ele.get(i)); } collection.insert(document); } }
can please suggest required change need in function?
you should use getlong()
instead of getfloat()
retrieve counter value, i.e. change code:
else if (key.gettype() == datatype.counter()) { str = new float(row.getfloat(col)).tostring(); }
to
else if (key.gettype() == datatype.counter()) { str = string.valueof(row.getlong(col)); }
btw pay attention on fact not have create instance of wrapper type long
(as did int cases). use string.valueof()
create string representation of value.
Comments
Post a Comment