Reputation:
I am using datastax and want to retrieve Map from Cassandra.
trying retrieve frozen set column from Cassandra using datastax drivers in java.But not able to print that column in std output of java or either to csv. All i get is a blank output.Below is my code what i am trying to do.its able to print all column values from that table except set one.If anyone had done this before , give me an idea .
package com.cassandra.cassandrafetch1;
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ColumnDefinitions.Definition;
import com.datastax.driver.core.DataType;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.SimpleStatement;
import com.datastax.driver.core.Statement;
import com.google.common.collect.Sets;
public class CassExport {
static int i = 0;
public static void main(String[] args) throws FileNotFoundException {
long startTime = System.currentTimeMillis();
PrintWriter pw = new PrintWriter(new File("test.csv"));
String keyspace = "xxxxxxx";
String table = "xxxxxxxxxxx";
String username = "xxxxx";
String password = "xxxxxx";
String host = "xxxxxx";
double count = 0;
Cluster.Builder clusterBuilder = Cluster.builder()
.addContactPoints(host)
.withCredentials(username, password);
Cluster cluster = clusterBuilder.build();
Session session = cluster.connect(keyspace);
Statement stmt = new SimpleStatement("SELECT names FROM " + table );
stmt.setFetchSize(2000);
ResultSet rs = session.execute(stmt);
Iterator<Row> iter = rs.iterator();
while ( !rs.isFullyFetched()) {
if (rs.getAvailableWithoutFetching() == 120 )
rs.fetchMoreResults();
Row row = iter.next();
if ( rs != null )
{
StringBuilder line = new StringBuilder();
for (Definition key : row.getColumnDefinitions().asList())
{
String val = myGetValue(key, row);
line.append("\"");
line.append(val);
line.append("\"");
line.append(',');
}
line.deleteCharAt(line.length()-1);
line.append('\n');
pw.write(line.toString());
System.out.println(line.toString());
++count;
}
}
pw.close();
session.close();
cluster.close();
System.out.println(count + "\t rows copied into csv");
long endTime = System.currentTimeMillis();
System.out.println("Took "+(endTime - startTime) + " ms");
}
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.timestamp())
{
str = row.getDate(col).toString();
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
str = fmt.format(row.getDate(col));
}
else if (key.getType().equals(DataType.frozenSet(DataType.varchar())))
{
try {
for(int i = 0; i < ; i++) {
Set<String> st = row.getSet(i, String.class );
System.out.println(st);
str = st;
}
}
else
{
str = row.getString(col); }
} catch (Exception e)
{
str = "";
}
}
return str;
}
}
Schema :
CREATE TABLE xxx.xxxx (
dynamic uuid,
source text,
view int,
names frozen<set<text>>,
nameid tinyint,
groupid uuid,
texts int,
total int static,
notin text,
PRIMARY KEY ((dynamicid, source, view), names)
)
cqlsh output for that set column:
names
--------------------------------------------------------------------
---------------------------------------------------------------------------------
{').', '0/11', 'ndf', 'STOP', 'where', 'No', 'You', 'zxz', 'are', 'at', 'forward', 'looking', 'to'}
{').', '1/17', 'STOP', 'nowhere', 'Unsubscribe', 'We', 'ndt', 'are', 'Word', 'brett', 'ndf', 'hgf'}
Upvotes: 1
Views: 1044
Reputation: 11
How to use row.getSet()
for type set<frozen<custom_class>>
?
Eg:
CREATE TABLE test.test_pal (
id bigint,
name text,
related_item set<frozen<related_item>>
PRIMARY KEY ((id,type)))
)
create table test.related_item(
public String item_id;
public String item_type;
public Map<String,String> item_attributes;
public Timestamp last_modified_date;
)
my code:
if (key.getType().getName().name()=="SET") {
Set<String> ts = row.getSet("related_item", ??????); // refering to test.test_pal
for (String string : ts) {
//... do something with set's content...
}
}`
Hence, I created a class related_item_class and used as below.
Set<String> ts = row.getSet("", related_item_class.class);
Got exception :com.datastax.driver.core.exceptions.CodecNotFoundException: Codec not found for requested operation: [set<test.related_item> <-> com.test.myutility.related_item_class]
Class public class related_item_class {
public String getItem_id() {
return item_id;
}
public void setItem_id(String item_id) {
this.item_id = item_id;
}
public String getItem_type() {
return item_type;
}
public void setItem_type(String item_type) {
this.item_type = item_type;
}
public Map<String, String> getItem_attributes() {
return item_attributes;
}
public void setItem_attributes(Map<String, String> item_attributes) {
this.item_attributes = item_attributes;
}
public Date getLast_modified_date() {
return last_modified_date;
}
public void setLast_modified_date(Timestamp last_modified_date) {
this.last_modified_date = last_modified_date;
}
public String item_id;
public String item_type;
public Map<String,String> item_attributes;
public Timestamp last_modified_date;
}
row.getSet("test.test_pal",?) should return related_item set<frozen<related_item>>
Upvotes: 0
Reputation: 87174
You did one error - you're trying to access wrong element by trying to call row.getSet(i, String.class)
- in this case you're trying to access elements of the row
by index instead of retrieving the whole set by name.
In your case code for handling of frozen set should be following:
if (key.getType().equals(DataType.frozenSet(DataType.varchar()))) {
Set<String> ts = row.getSet(col, String.class);
for (String string : ts) {
//... do something with set's content...
}
}
Upvotes: 1