Data persistency

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Data persistency

Ivan Chang
I tried to insert and retrieve data from a standalone Java program.  While I am able to insert and retrieve the correct data from within the Java session.  After I terminate the session, and rerun only the data retrieval part, the previous inserted data does not exist anymore, throwing a null exception.  Here's the code:

        // Get storage-config file location
        System.out.println("storage-config="+DatabaseDescriptor.getConfigFileName());

        // Insert some data with key "partner1"
        RowMutation rm = new RowMutation("Table1", "partner1");
        ColumnFamily cf = new ColumnFamily("Standard1", "Standard");
        long now = Calendar.getInstance().getTimeInMillis();
        System.out.println(now);
        cf.addColumn("firstname", "John1".getBytes(), now);
        cf.addColumn("lastname", "Doe1".getBytes(), now);
        rm.add(cf);
        try {
            rm.apply();
        } catch (Exception e) {
        }
       
        // Retrieve data for key "partner1"
        Table table = Table.open("Table1");

        try {
            Row result = table.getRow("partner1", "Standard1");
            System.out.println(result.toString());
            ColumnFamily cres = result.getColumnFamily("Standard1");
            Map cols = cres.getColumns();
            System.out.println(cols.size());
            Set c = cols.keySet();
            Iterator it = c.iterator();
            while (it.hasNext()) {
                String cn = (String) it.next();
                System.out.println(cn);
                System.out.println(new String(cres.getColumn(cn).value()));
            }
        } catch (Exception e) {
            System.out.println("Ex: " + e.getMessage());
        }

the print out from above is

storage-config=~/Cassandra/trunk/conf/storage-conf.xml
1245270260114
Row(partner1 [ColumnFamily(Standard1 [firstname:false:5@1245270260114, lastname:false:4@1245270260114]))]
2
lastname
Doe1
firstname
John1

However, when I commented out the insert part of the above code and try retrieve data again by rerunning the main code, I got an exception:

Row(partner1 [)]
Ex: null

So the data doesn't seem to persist across sessions.

Could someone explain what's wrong with the code?

Thanks,
Ivan
Reply | Threaded
Open this post in threaded view
|

Re: Data persistency

Jonathan Ellis-3
You're using internal APIs.  Don't do that unless you know what you're doing. :)

The client API is in Cassandra.Client.

We have some sample code here: http://wiki.apache.org/cassandra/ClientExamples

(although none in Java yet, it should still be pretty clear.)

-Jonathan

On Wed, Jun 17, 2009 at 3:54 PM, Ivan Chang<[hidden email]> wrote:

> I tried to insert and retrieve data from a standalone Java program.  While I
> am able to insert and retrieve the correct data from within the Java
> session.  After I terminate the session, and rerun only the data retrieval
> part, the previous inserted data does not exist anymore, throwing a null
> exception.  Here's the code:
>
>         // Get storage-config file location
>
> System.out.println("storage-config="+DatabaseDescriptor.getConfigFileName());
>
>         // Insert some data with key "partner1"
>         RowMutation rm = new RowMutation("Table1", "partner1");
>         ColumnFamily cf = new ColumnFamily("Standard1", "Standard");
>         long now = Calendar.getInstance().getTimeInMillis();
>         System.out.println(now);
>         cf.addColumn("firstname", "John1".getBytes(), now);
>         cf.addColumn("lastname", "Doe1".getBytes(), now);
>         rm.add(cf);
>         try {
>             rm.apply();
>         } catch (Exception e) {
>         }
>
>         // Retrieve data for key "partner1"
>         Table table = Table.open("Table1");
>
>         try {
>             Row result = table.getRow("partner1", "Standard1");
>             System.out.println(result.toString());
>             ColumnFamily cres = result.getColumnFamily("Standard1");
>             Map cols = cres.getColumns();
>             System.out.println(cols.size());
>             Set c = cols.keySet();
>             Iterator it = c.iterator();
>             while (it.hasNext()) {
>                 String cn = (String) it.next();
>                 System.out.println(cn);
>                 System.out.println(new String(cres.getColumn(cn).value()));
>             }
>         } catch (Exception e) {
>             System.out.println("Ex: " + e.getMessage());
>         }
>
> the print out from above is
>
> storage-config=~/Cassandra/trunk/conf/storage-conf.xml
> 1245270260114
> Row(partner1 [ColumnFamily(Standard1 [firstname:false:5@1245270260114,
> lastname:false:4@1245270260114]))]
> 2
> lastname
> Doe1
> firstname
> John1
>
> However, when I commented out the insert part of the above code and try
> retrieve data again by rerunning the main code, I got an exception:
>
> Row(partner1 [)]
> Ex: null
>
> So the data doesn't seem to persist across sessions.
>
> Could someone explain what's wrong with the code?
>
> Thanks,
> Ivan
>
Reply | Threaded
Open this post in threaded view
|

Re: Data persistency

Ivan Chang
Using Cassandra.Client works.  However more questions arise, specifically regarding Super Columns, while the following code persist the super column "sc1"with 3 simple columns.  How do I create nested super columns?  A super column with multiple super columns and standard columns?  Thanks, Ivan

            // Super Column
            batch_mutation_super_t bt = new batch_mutation_super_t();
            bt.key = "testkey";
            bt.table = tablename_;
            bt.cfmap = new HashMap<String,List<superColumn_t>>();
            List<superColumn_t> superColumn_arr = new ArrayList<superColumn_t>();
            List<column_t> column_arr2 = new ArrayList<column_t>();
            column_arr2.add(new column_t("c1", "v1".getBytes(), now));
            column_arr2.add(new column_t("c2", "v2".getBytes(), now));
            column_arr2.add(new column_t("c3", "v3".getBytes(), now));
            superColumn_arr.add(new superColumn_t("sc1", column_arr2));
            bt.cfmap.put("Super1", superColumn_arr);
            peerstorageClient.batch_insert_superColumn(bt, false);

On Wed, Jun 17, 2009 at 5:01 PM, Jonathan Ellis <[hidden email]> wrote:
You're using internal APIs.  Don't do that unless you know what you're doing. :)

The client API is in Cassandra.Client.

We have some sample code here: http://wiki.apache.org/cassandra/ClientExamples

(although none in Java yet, it should still be pretty clear.)

-Jonathan

On Wed, Jun 17, 2009 at 3:54 PM, Ivan Chang<[hidden email]> wrote:
> I tried to insert and retrieve data from a standalone Java program.  While I
> am able to insert and retrieve the correct data from within the Java
> session.  After I terminate the session, and rerun only the data retrieval
> part, the previous inserted data does not exist anymore, throwing a null
> exception.  Here's the code:
>
>         // Get storage-config file location
>
> System.out.println("storage-config="+DatabaseDescriptor.getConfigFileName());
>
>         // Insert some data with key "partner1"
>         RowMutation rm = new RowMutation("Table1", "partner1");
>         ColumnFamily cf = new ColumnFamily("Standard1", "Standard");
>         long now = Calendar.getInstance().getTimeInMillis();
>         System.out.println(now);
>         cf.addColumn("firstname", "John1".getBytes(), now);
>         cf.addColumn("lastname", "Doe1".getBytes(), now);
>         rm.add(cf);
>         try {
>             rm.apply();
>         } catch (Exception e) {
>         }
>
>         // Retrieve data for key "partner1"
>         Table table = Table.open("Table1");
>
>         try {
>             Row result = table.getRow("partner1", "Standard1");
>             System.out.println(result.toString());
>             ColumnFamily cres = result.getColumnFamily("Standard1");
>             Map cols = cres.getColumns();
>             System.out.println(cols.size());
>             Set c = cols.keySet();
>             Iterator it = c.iterator();
>             while (it.hasNext()) {
>                 String cn = (String) it.next();
>                 System.out.println(cn);
>                 System.out.println(new String(cres.getColumn(cn).value()));
>             }
>         } catch (Exception e) {
>             System.out.println("Ex: " + e.getMessage());
>         }
>
> the print out from above is
>
> storage-config=~/Cassandra/trunk/conf/storage-conf.xml
> 1245270260114
> Row(partner1 [ColumnFamily(Standard1 [firstname:false:5@1245270260114,
> lastname:false:4@1245270260114]))]
> 2
> lastname
> Doe1
> firstname
> John1
>
> However, when I commented out the insert part of the above code and try
> retrieve data again by rerunning the main code, I got an exception:
>
> Row(partner1 [)]
> Ex: null
>
> So the data doesn't seem to persist across sessions.
>
> Could someone explain what's wrong with the code?
>
> Thanks,
> Ivan
>

Reply | Threaded
Open this post in threaded view
|

Re: Data persistency

Jonathan Ellis-3
You don't.  Supercolumns are not arbitrarily nestable.

A columnfamily is either super or normal; a super columnfamily
contains supercolumns, which in turn contain Columns.  A normal
columnfamily contains Columns directly.  You can't mix-and-match
supercolumns and normal columns (at the same level of nesting) in a
single columnfamily.

-Jonathan

On Thu, Jun 18, 2009 at 12:12 PM, Ivan Chang<[hidden email]> wrote:

> Using Cassandra.Client works.  However more questions arise, specifically
> regarding Super Columns, while the following code persist the super column
> "sc1"with 3 simple columns.  How do I create nested super columns?  A super
> column with multiple super columns and standard columns?  Thanks, Ivan
>
>             // Super Column
>             batch_mutation_super_t bt = new batch_mutation_super_t();
>             bt.key = "testkey";
>             bt.table = tablename_;
>             bt.cfmap = new HashMap<String,List<superColumn_t>>();
>             List<superColumn_t> superColumn_arr = new
> ArrayList<superColumn_t>();
>             List<column_t> column_arr2 = new ArrayList<column_t>();
>             column_arr2.add(new column_t("c1", "v1".getBytes(), now));
>             column_arr2.add(new column_t("c2", "v2".getBytes(), now));
>             column_arr2.add(new column_t("c3", "v3".getBytes(), now));
>             superColumn_arr.add(new superColumn_t("sc1", column_arr2));
>             bt.cfmap.put("Super1", superColumn_arr);
>             peerstorageClient.batch_insert_superColumn(bt, false);
>
> On Wed, Jun 17, 2009 at 5:01 PM, Jonathan Ellis <[hidden email]> wrote:
>>
>> You're using internal APIs.  Don't do that unless you know what you're
>> doing. :)
>>
>> The client API is in Cassandra.Client.
>>
>> We have some sample code here:
>> http://wiki.apache.org/cassandra/ClientExamples
>>
>> (although none in Java yet, it should still be pretty clear.)
>>
>> -Jonathan
>>
>> On Wed, Jun 17, 2009 at 3:54 PM, Ivan Chang<[hidden email]> wrote:
>> > I tried to insert and retrieve data from a standalone Java program.
>> > While I
>> > am able to insert and retrieve the correct data from within the Java
>> > session.  After I terminate the session, and rerun only the data
>> > retrieval
>> > part, the previous inserted data does not exist anymore, throwing a null
>> > exception.  Here's the code:
>> >
>> >         // Get storage-config file location
>> >
>> >
>> > System.out.println("storage-config="+DatabaseDescriptor.getConfigFileName());
>> >
>> >         // Insert some data with key "partner1"
>> >         RowMutation rm = new RowMutation("Table1", "partner1");
>> >         ColumnFamily cf = new ColumnFamily("Standard1", "Standard");
>> >         long now = Calendar.getInstance().getTimeInMillis();
>> >         System.out.println(now);
>> >         cf.addColumn("firstname", "John1".getBytes(), now);
>> >         cf.addColumn("lastname", "Doe1".getBytes(), now);
>> >         rm.add(cf);
>> >         try {
>> >             rm.apply();
>> >         } catch (Exception e) {
>> >         }
>> >
>> >         // Retrieve data for key "partner1"
>> >         Table table = Table.open("Table1");
>> >
>> >         try {
>> >             Row result = table.getRow("partner1", "Standard1");
>> >             System.out.println(result.toString());
>> >             ColumnFamily cres = result.getColumnFamily("Standard1");
>> >             Map cols = cres.getColumns();
>> >             System.out.println(cols.size());
>> >             Set c = cols.keySet();
>> >             Iterator it = c.iterator();
>> >             while (it.hasNext()) {
>> >                 String cn = (String) it.next();
>> >                 System.out.println(cn);
>> >                 System.out.println(new
>> > String(cres.getColumn(cn).value()));
>> >             }
>> >         } catch (Exception e) {
>> >             System.out.println("Ex: " + e.getMessage());
>> >         }
>> >
>> > the print out from above is
>> >
>> > storage-config=~/Cassandra/trunk/conf/storage-conf.xml
>> > 1245270260114
>> > Row(partner1 [ColumnFamily(Standard1 [firstname:false:5@1245270260114,
>> > lastname:false:4@1245270260114]))]
>> > 2
>> > lastname
>> > Doe1
>> > firstname
>> > John1
>> >
>> > However, when I commented out the insert part of the above code and try
>> > retrieve data again by rerunning the main code, I got an exception:
>> >
>> > Row(partner1 [)]
>> > Ex: null
>> >
>> > So the data doesn't seem to persist across sessions.
>> >
>> > Could someone explain what's wrong with the code?
>> >
>> > Thanks,
>> > Ivan
>> >
>
>