Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Cassandra Wiki" for 
change notification.

The "ClientExamples" page has been changed by gdusbabek.
The comment on this change is: Adding fat client example..
http://wiki.apache.org/cassandra/ClientExamples?action=diff&rev1=40&rev2=41

--------------------------------------------------

      }
  }
  }}}
+ 
+ Alternatively, there is a Java "Fat Client" that can be used to bring up a 
node in client-only mode.  A client node may participate in reads or writes and 
has the added benefit of avoid thrift-related overhead.  The following example 
comes from /contrib/client_only:
+ 
+ Writing
+ {{{
+ StorageService.instance().initClient();
+ // sleep for a bit so that gossip can do its thing.
+ try
+ {
+     Thread.sleep(10000L);
+ }
+ catch (Exception ex)
+ {
+ }
+ 
+ // do some writing.
+ for (int i = 0; i < 100; i++)
+ {
+     RowMutation change = new RowMutation("Keyspace1", "key" + i);
+     ColumnPath cp = new ColumnPath("Standard1", null, ("colb").getBytes());
+     change.add(new QueryPath(cp), ("value" + i).getBytes(), 0);
+ 
+     // don't call change.apply().  The reason is that is makes a static call 
into Table, which will perform
+     // local storage initialization, which creates local directories.
+     // change.apply();
+ 
+     StorageProxy.insert(change);
+     try
+     {
+         Thread.sleep(50L);
+     }
+     catch (Exception ex)
+     {
+     }
+     System.out.println("wrote key" + i);
+ }
+ System.out.println("Done writing.");
+ StorageService.instance().stopClient();
+ }}}
+ 
+ Reading
+ {{{
+ StorageService.instance().initClient();
+ // sleep for a bit so that gossip can do its thing.
+ try
+ {
+     Thread.sleep(10000L);
+ }
+ catch (Exception ex)
+ {
+ }
+ 
+ // do some queries.
+ Collection<byte[]> cols = new ArrayList<byte[]>()
+ {{
+     add("colb".getBytes());
+ }};
+ for (int i = 0; i < 100; i++)
+ {
+     List<ReadCommand> commands = new ArrayList<ReadCommand>();
+     SliceByNamesReadCommand readCommand = new 
SliceByNamesReadCommand("Keyspace1", "key" + i, new QueryPath("Standard1", 
null, null), cols);
+     readCommand.setDigestQuery(false);
+     commands.add(readCommand);
+     try
+     {
+         List<Row> rows = StorageProxy.readProtocol(commands, 
ConsistencyLevel.ONE);
+         assert rows.size() == 1;
+         Row row = rows.get(0);
+         ColumnFamily cf = row.cf;
+         if (cf != null)
+         {
+             for (IColumn col : cf.getSortedColumns())
+             {
+                 System.out.println(new String(col.name()) + ", " + new 
String(col.value()));
+             }
+         }
+         else
+             System.err.println("This output indicates that nothing was 
read.");
+     }
+     catch (UnavailableException e)
+     {
+         throw new RuntimeException(e);
+     }
+     catch (TimedOutException e)
+     {
+         throw new RuntimeException(e);
+     }
+ 
+ }
+ 
+ // no need to do this:
+ // StorageService.instance().decommission();
+ // do this instead:
+ StorageService.instance().stopClient();
+ }}}
+ 
+ A caveat of doing things this way is that a client cannot go up and down, and 
then up again without shutting down the entire VM.  I.e., you can't 
initClient(), stopClient() and then initClient() again.
  
  == Ruby ==
  

Reply via email to