문제

Im inserting data with hector and getting it back with JDBC.

The data makes no sence apart from the indexed column, im aware that this is because cassandra stores data as bytes and then converts it when its read (client converts it)

Does anyone know how to do this in JDBC for cassandra ?.

My code:

public static void main(String[] args) throws SQLException
    {
        Connection con = null;
        String _KS = "StringMsg";
        String _CFDef = "Tweets";

        try 
        {
            Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver");
            con = DriverManager.getConnection("jdbc:cassandra://localhost:9160/" + _KS);

            String query = "SELECT username, email, msg, id FROM " + _CFDef + " WHERE 'username'='LTodorov'";

            Statement stmt = con.createStatement();
            ResultSet result = stmt.executeQuery(query);

            System.out.println("hi");
            int i=0;
            while (result.next())
            {
                System.out.println(result.getString("id"));
                System.out.println(result.getString("username"));
                System.out.println(result.getString("msg"));
                System.out.println(result.getString("email"));
                System.out.print("count:" + i + "\n\n");i++;
            }
        } 
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        finally 
        {
            if (con != null) 
            {
                try 
                {
                    con.close();
                } 
                catch (SQLException e) 
                {
                    e.printStackTrace();
                }
                con = null;
            }
        }
    }

Results:

hi
java.nio.HeapByteBuffer[pos=237 lim=258 cap=798]
LTodorov
java.nio.HeapByteBuffer[pos=184 lim=209 cap=798]
java.nio.HeapByteBuffer[pos=144 lim=155 cap=798]
count:0

Cli Result:

GET Tweets Where username=LTodorov;
-------------------
RowKey: LTodorov1330045080096
=> (column=email, value=6c5f6c74404162762e6267, timestamp=1330045080214000)
=> (column=id, value=4c546f646f726f7631333330303435303830303936, timestamp=13300
45080098000)
=> (column=msg, value=54686973206973206d616b696e67206d652073616466616365, timest
amp=1330045080210000)
=> (column=username, value=LTodorov, timestamp=1330045080217000)

Cli Result with "assume Tweets validator as utf8";

RowKey: LTodorov1330045080096
=> (column=email, value=l_ld@Abv.bg, timestamp=1330045080214000)
=> (column=id, value=LTodorov1330045080096, timestamp=1330045080098000)
=> (column=msg, value=This is making me sadface, timestamp=1330045080210000)
=> (column=username, value=LTodorov, timestamp=1330045080217000)

Column Family Definition:

CREATE COLUMN FAMILY Tweets
WITH comparator = UTF8Type
AND key_validation_class=UTF8Type
AND column_metadata = [
{column_name: id, validation_class: UTF8Type}
{column_name: email, validation_class: UTF8Type}
{column_name: msg, validation_class: UTF8Type}
{column_name: user, validation_class: UTF8Type}

Any cassandra gurus got any pointers? I'm getting the data back but should it be converted inside java, or should the JDBC api do the work or is it a cassandra thing ? (i think this cant be solved by cassandra)

올바른 솔루션이 없습니다

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top