Thursday, November 18, 2010

Java / Ektorp / CouchDB

Ektorp is a persistence API that uses CouchDB as storage engine. The goal of Ektorp is to combine JPA like functionality with the simplicity and flexibility that CouchDB provides ... more ...

Below is an example that I used to access my CouchDB and I ran the command curl --user user:password -X PUT http://my.couchone.com:5984/testdb/ektorp -d '{ "color": "red" }' first to create a document 'ektorp' before executing Java code.


import org.ektorp.CouchDbConnector;
import org.ektorp.CouchDbInstance;
import org.ektorp.http.HttpClient;
import org.ektorp.http.StdHttpClient;
import org.ektorp.impl.StdCouchDbConnector;
import org.ektorp.impl.StdCouchDbInstance;
import org.ektorp.support.CouchDbDocument;

public class Ektorp {
public static void main(String[] args) {
HttpClient httpClient = new StdHttpClient.Builder()
.host("my.couchone.com")
.port(5984)
.username("user")
.password("password")
.build();

CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient);
CouchDbConnector db = new StdCouchDbConnector("testdb", dbInstance);

db.createDatabaseIfNotExists();
Sofa sofa = db.get(Sofa.class, "ektorp");

if (sofa.getColor().equals("red")) {
sofa.setColor("green");
} else {
sofa.setColor("red");
}
db.update(sofa);
}
}

class Sofa extends CouchDbDocument {
private String color;
public void setColor(String s) {
color = s;
}

public String getColor() {
return color;
}
}

More examples here ...

Wednesday, November 17, 2010

Groovy / REST / CouchDB

The codes below from REST up with CouchDB and Groovy's RESTClient was modified to use login authentication based on this link ...

import groovyx.net.http.RESTClient
import static groovyx.net.http.ContentType.JSON

@Grab(group = 'org.codehaus.groovy.modules.http-builder', module = 'http-builder', version = '[0.5.0,0.5.1)')
def getRESTClient(local = false) {
RESTClient client;

if (!local) {
client = new RESTClient("http://my.couchone.com:5498/")
} else {
client = new RESTClient("http://my.couchone.com:5984/")
}

def authHash = "user:password".getBytes('iso-8859-1').encodeBase64()
client.headers.Authorization = "Basic $authHash"

return client;
}

def client = getRESTClient(true)

try {
client.get(path: "parking_tickets")
println "Deleting DB parking_tickets ..."
client.delete(path: "parking_tickets")
} catch (Exception e) {
println "Creating new DB parking_tickets ..."
}

def response = client.put(path: "parking_tickets", requestContentType: JSON, contentType: JSON)

assert response.data.ok : "response from server wasn't ok"

Note: Grab will download required jars to C:\Users\userName\.groovy\grapes\org.codehaus.groovy.modules.http-builder

Thursday, November 11, 2010

My free CouchDB account (faked)

I don't have time to learn how to make good use of it yet so I temporarily put it here to remind me its existence ...

http://my.couchone.com/_utils/


Useful commands:
> curl -x myproxy:80 -X GET http://my.couchone.com/_all_dbs
> curl -u user:password -x myproxy:80 -X PUT http://my.couchone.com/testdb
> curl -u user:password -x myproxy:80 -X GET http://my.couchone.com/_all_dbs
["testdb","mybooks","_users","test_suite_db/with_slashes"]

Monday, November 1, 2010

How to burn a DVD on Linux

Just in case I couldn't remember how to do it ...

> mkisofs -r -o MyDVD.iso /home/david/Folder_to_Burn_to_DVD

> growisofs -dvd-compat -Z /dev/dvdrw=MyDVDS.iso -speed=2

- or do the following without creating an ISO file -

> growisofs -Z /dev/cdrom -R -J /home/david/Folder_to_Burn_to_DVD