Skip to content

Instantly share code, notes, and snippets.

@ingenthr
Created December 17, 2015 00:17
Show Gist options
  • Save ingenthr/21a08f5a5b6a7ed34793 to your computer and use it in GitHub Desktop.
Save ingenthr/21a08f5a5b6a7ed34793 to your computer and use it in GitHub Desktop.
Couchbase quick example of Prepared Parameterized Statements from Java SDK
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.CouchbaseCluster;
import java.util.concurrent.TimeUnit;
/**
* Created by ingenthr on 12/16/15.
*/
public class Main {
Cluster cluster;
Bucket travelSampleBucket;
public static void main(String args[]) throws InterruptedException {
Main runner = new Main();
runner.setup();
runner.run100();
Thread.sleep(3000);
runner.runForTime(60);
runner.teardown();
System.exit(0);
}
void setup() {
// Connect to localhost
cluster = CouchbaseCluster.create();
// Open the default bucket and the "beer-sample" one
// Bucket defaultBucket = cluster.openBucket();
// Bucket beerSampleBucket = cluster.openBucket("beer-sample");
travelSampleBucket = cluster.openBucket("travel-sample");
}
void teardown() {
// Disconnect and clear all allocated resources
cluster.disconnect();
}
void run100() {
for (int i=0; i<100; i++) {
PreparedParameterized.findAllPreparedByFaa(travelSampleBucket, "LAX");
}
}
void runForTime(int seconds) {
long start, stop, count;
count = 0;
start = System.nanoTime();
for (stop = System.nanoTime()+ TimeUnit.SECONDS.toNanos(seconds); stop>System.nanoTime();) {
count++;
PreparedParameterized.findAllPreparedByFaa(travelSampleBucket, "LAX");
}
long runtime = TimeUnit.NANOSECONDS.toSeconds(stop-start);
System.out.println("Run time was " + runtime + " seconds, yielding a throughput of " + count/runtime + " ops/s");
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.document.json.JsonArray;
import com.couchbase.client.java.query.*;
import com.couchbase.client.java.query.dsl.path.AsPath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static com.couchbase.client.java.query.Select.select;
import static com.couchbase.client.java.query.dsl.Expression.i;
import static com.couchbase.client.java.query.dsl.Expression.s;
import static com.couchbase.client.java.query.dsl.Expression.x;
/**
* Created by ingenthr on 12/16/15.
*/
public class PreparedParameterized {
private static final Logger LOGGER = LoggerFactory.getLogger(PreparedParameterized.class);
/**
* Find all airports using a prepared statement.
*/
public static List<Map<String, Object>> findAllPreparedByFaa(final Bucket bucket, final String params) {
Statement query;
//Statement withPlaceholders = select("airportname").from(i(bucket.name())).where(x("type").eq(x("$1")));
AsPath prefix = select("airportname").from(i(bucket.name()));
query = prefix.where(x("faa").eq(x("$1")));
logQuery(query.toString());
JsonArray queryParams = JsonArray.create().add(params.toUpperCase());
// change adhoc below to have the SDK prepare or not
N1qlQueryResult result = bucket.query(N1qlQuery.parameterized(query, queryParams, N1qlParams.build().adhoc(false)));
return extractResultOrThrow(result);
}
/**
* Extract a N1Ql result or throw if there is an issue.
*/
private static List<Map<String, Object>> extractResultOrThrow(N1qlQueryResult result) {
if (!result.finalSuccess()) {
LOGGER.warn("Query returned with errors: " + result.errors());
// throw new DataRetrievalFailureException("Query error: " + result.errors());
}
List<Map<String, Object>> content = new ArrayList<Map<String, Object>>();
for (N1qlQueryRow row : result) {
content.add(row.value().toMap());
}
return content;
}
/**
* Helper method to log the executing query.
*/
private static void logQuery(String query) {
LOGGER.info("Executing Query: {}", query);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment