Skip to content

Instantly share code, notes, and snippets.

@jaekwonpark
Created September 24, 2017 08:02
Show Gist options
  • Save jaekwonpark/9c22b6e75e9fab932f7d37105528f8e3 to your computer and use it in GitHub Desktop.
Save jaekwonpark/9c22b6e75e9fab932f7d37105528f8e3 to your computer and use it in GitHub Desktop.
package com.couchbase;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.CouchbaseCluster;
import com.couchbase.client.java.document.JsonDocument;
import com.couchbase.client.java.document.json.JsonArray;
import com.couchbase.client.java.document.json.JsonObject;
import com.couchbase.client.java.query.N1qlQuery;
import com.couchbase.client.java.query.N1qlQueryResult;
import com.couchbase.client.java.query.N1qlQueryRow;
public class Kvoperation {
static Bucket bucket = null;
static JsonObject arthur = null;
public static class Upsert extends Thread {
@Override
public void run() {
int i = 0;
while (true) {
try {
bucket.upsert(JsonDocument.create("u:king_arthur" + i, arthur));
} catch (Exception e) {
System.out.printf("%Error on Upsert:%s", e.getMessage());
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
System.out.printf("Interrupt\n");
}
i++;
if ( i > 1000 ) {
i = 0;
}
}
}
}
public static class Get extends Thread {
@Override
public void run() {
int i = 0;
while (true) {
try {
JsonDocument doc = bucket.get("u:king_arthur" + i);
System.out.printf("content:%s", doc.content());
try {
Thread.sleep(10);
} catch (InterruptedException e) {
System.out.printf("Interrupt\n");
}
i++;
} catch (Exception e) {
System.out.printf("%Error on Get:%s", e.getMessage());
}
}
}
}
public static void main(String... args) throws Exception {
// Initialize the Connection
Cluster cluster = CouchbaseCluster.create("172.23.123.252");
String bucketName = args[0];
bucket = cluster.openBucket(bucketName, "password");
System.out.printf("%s\n", bucketName);
// Create a JSON Document
arthur = JsonObject.create()
.put("name", "Arthur")
.put("email", "[email protected]")
.put("interests", JsonArray.from("Holy Grail", "African Swallows"));
int i = 0;
while (true) {
bucket.upsert(JsonDocument.create("u:king_arthur"+i, arthur));
try {
Thread.sleep(10);
} catch (InterruptedException e) {
System.out.printf("Interrupt\n");
}
i++;
if ( i == 1000 ) {
break;
}
}
Thread upsert = new Upsert();
upsert.start();
Thread get = new Get();
get.start();
try {
upsert.join();
get.join();
}catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment