elads11
elads11

Reputation: 377

How do I switch between column types in Cassandra?

I have a function in my project that deals with row results from query, and switches between the column types. The problem with doing this with Cassandra, is that I can't switch between the types, because this line won't work:

type = rs.getColumnType(i);

Here is the full snippet code:

ResultSet rs =  session.execute(query);
for (int i = 0; i < rs.getColumnDefinitions().size(); i++) {
type = rs.getColumnType(i);
switch (type) {
    case Types.SMALLINT:
        break;
    case Types.INTEGER:
        break;
    case Types.BIGINT:
        break;
    case Types.DECIMAL:
        break;
    case Types.NUMERIC:
        break;
    case Types.FLOAT:
        break;
    case Types.DOUBLE:
        break;
    default:
}

}

I'm trying to figure out how to switch between column types

Upvotes: 2

Views: 99

Answers (1)

Erick Ramirez
Erick Ramirez

Reputation: 16313

What you're doing doesn't make sense because. Best practice is for your app queries to explicitly enumerate the columns being retrieved from a table.

For example, using SELECT * ... is bad practice because the returned results can be very unpredictable. If a column is added (or removed) from the table, the returned columns in the result would no longer match the columns expected by your application.

The recommended usage is to list all the columns when reading from a table. For example:

SELECT name, age, phone FROM users_by_email WHERE email = ?

If you are adhering to best practice guidelines then it follows that you should know the CQL type of the columns being returned, therefore trying to determine the CQL type from the results is unnecessary. Cheers!

Upvotes: 1

Related Questions