Author: eevans
Date: Sun Oct 31 14:35:48 2010
New Revision: 1029357
URL: http://svn.apache.org/viewvc?rev=1029357&view=rev
Log:
move scratch client code into a stubbed driver class
Patch by eevans
Added:
cassandra/trunk/drivers/
cassandra/trunk/drivers/java/
cassandra/trunk/drivers/java/src/
cassandra/trunk/drivers/java/src/org/
cassandra/trunk/drivers/java/src/org/apache/
cassandra/trunk/drivers/java/src/org/apache/cassandra/
cassandra/trunk/drivers/java/src/org/apache/cassandra/cql/
cassandra/trunk/drivers/java/src/org/apache/cassandra/cql/driver/
cassandra/trunk/drivers/java/src/org/apache/cassandra/cql/driver/Connection.java
Removed:
cassandra/trunk/src/java/org/apache/cassandra/cql/Client.java
Added:
cassandra/trunk/drivers/java/src/org/apache/cassandra/cql/driver/Connection.java
URL:
http://svn.apache.org/viewvc/cassandra/trunk/drivers/java/src/org/apache/cassandra/cql/driver/Connection.java?rev=1029357&view=auto
==============================================================================
---
cassandra/trunk/drivers/java/src/org/apache/cassandra/cql/driver/Connection.java
(added)
+++
cassandra/trunk/drivers/java/src/org/apache/cassandra/cql/driver/Connection.java
Sun Oct 31 14:35:48 2010
@@ -0,0 +1,123 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.cassandra.cql.driver;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.net.URL;
+import java.nio.ByteBuffer;
+import java.util.zip.Deflater;
+
+import org.apache.avro.ipc.AvroRemoteException;
+import org.apache.avro.ipc.HttpTransceiver;
+import org.apache.avro.specific.SpecificRequestor;
+import org.apache.cassandra.avro.Cassandra;
+import org.apache.cassandra.avro.Compression;
+import org.apache.cassandra.avro.CqlResult;
+import org.apache.cassandra.avro.CqlRow;
+import org.apache.cassandra.avro.InvalidRequestException;
+import org.apache.cassandra.avro.TimedOutException;
+import org.apache.cassandra.avro.UnavailableException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class Connection
+{
+ private static final Logger logger =
LoggerFactory.getLogger(Connection.class);
+
+ public String hostName;
+ public int port;
+ private Cassandra client;
+ private Compression defaultCompression = Compression.GZIP;
+
+ public Connection(String keyspaceName, String...hosts) throws IOException
+ {
+ assert hosts.length > 0;
+
+ for (String hostSpec : hosts)
+ {
+ String[] parts = hostSpec.split(":", 2);
+ this.hostName = parts[0];
+ this.port = Integer.parseInt(parts[1]);
+
+ // TODO: This will need to do connection pooling.
+ break;
+ }
+
+ HttpTransceiver tr = new HttpTransceiver(new URL("http", hostName,
port, ""));
+ client = (Cassandra)SpecificRequestor.getClient(Cassandra.class, tr);
+ client.set_keyspace(keyspaceName);
+ }
+
+ private ByteBuffer compressQuery(String queryStr, Compression compression)
+ {
+ Deflater compressor = new Deflater();
+ compressor.setInput(queryStr.getBytes());
+ compressor.finish();
+ byte[] compressedQuery = new byte[100];
+ int compressedSize = compressor.deflate(compressedQuery);
+
+ logger.trace("Compressed query statement {} bytes in length to {}
bytes",
+ queryStr.length(),
+ compressedSize);
+
+ return ByteBuffer.wrap(compressedQuery);
+ }
+
+ public CqlResult execute(String queryStr)
+ throws InvalidRequestException, UnavailableException, TimedOutException,
AvroRemoteException
+ {
+ return execute(queryStr, getDefaultCompression());
+ }
+
+ public CqlResult execute(String queryStr, Compression compression)
+ throws InvalidRequestException, UnavailableException, TimedOutException,
AvroRemoteException
+ {
+ logger.trace("Executing CQL Query: {}", queryStr);
+ return client.execute_cql_query(compressQuery(queryStr, compression),
compression);
+ }
+
+ public Compression getDefaultCompression()
+ {
+ return defaultCompression;
+ }
+
+ public void setDefaultCompression(Compression defaultCompression)
+ {
+ this.defaultCompression = defaultCompression;
+ }
+
+ public static void main(String[] args) throws Exception
+ {
+ Connection conn = new Connection("Keyspace1", "localhost:9160");
+ CqlResult result = conn.execute("UPDATE Standard2 USING
CONSISTENCY.ONE WITH ROW(\"mango\", COL(\"disposition\", \"fruit\"));");
+ String selectQ = "SELECT FROM Standard2 WHERE KEY > \"apple\" AND KEY
< \"carrot\" ROWLIMIT 5 DESC;";
+ result = conn.execute(selectQ);
+ switch (result.type)
+ {
+ case ROWS:
+ for (CqlRow row : result.rows)
+ {
+ System.out.println("Date: " + new
String(row.columns.get(0).value.array()));
+ }
+ }
+ }
+}