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 JonathanEllis. The comment on this change is: r/m StorageProxy example in favor of pointing people to contrib/. http://wiki.apache.org/cassandra/ClientExamples?action=diff&rev1=70&rev2=71 -------------------------------------------------- * Java: * http://code.google.com/p/cassandra-java-client + == Internal API == + + The StorageProxy API is available to JVM-based clients, but you should use Thrift unless you have a very good reason not to. (The most common reason is wanting to use the BinaryMemtable bulk-load interface.) + = Thrift examples = The rest of this page shows examples of using the low-level [[http://incubator.apache.org/thrift/|Thrift]] interface. @@ -318, +322 @@ } } }}} - In the trunk, 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. This is due to be released '''after''' version 0.5 (likely 0.9). - - The following example comes from /contrib/client_only: - - Writing - - {{{#!java - 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 - - {{{#!java - 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. - == Python == {{{#!python #!/usr/bin/env python # encoding: utf-8
