user-4870385 user-4870385 - 2 months ago 6
Java Question

Exporting Data from cassandra to mongodb

I have a program which exports the data directly from Cassandra to mongodb.It works fine But, It doesn't copies the counter type column from Cassandra to mongodb. It leaves it as 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 * FROM " + 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)
{
for (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>();
for (Definition key : row.getColumnDefinitions().asList())
{
String val = myGetValue(key, row);
ele.add(val);
}

for (int i = 0; i < ele.size() && i < 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 which gets exported to mongodb is :

{
"_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 what the problem is.The function below is not inserting the value of counter into mongodb since the val is defined as string which conflicts with the 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>();
for (Definition key : row.getColumnDefinitions().asList())
{
String val = myGetValue(key, row);
System.out.println(val);
ele.add(val);
}

for (int i = 0; i < ele.size() && i < colName.size(); i++) {
document.put(colName.get(i), ele.get(i));
}
collection.insert(document);
}
}


Can anyone please suggest the required change i need to do in the function?

Answer

You should use getLong() instead of getFloat() to 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 the fact that you do not have to create instance of wrapper type Long (as you did int all your cases). Use String.valueOf() to create string representation of your value.