AbacusUtil Docs | Download | Javadoc | FAQ | Community

CouchbaseExecutor

CouchbaseExecutor is a wrapper of Couchbase Java driver. Just like SQLExecutor, it makes things much easier to operate data with Couchbase by the straight support of Entity/Map/Query..., and almost all the java basic types: boolean...double...String...Date...Calendar.... Parameterized SQL with named parameters is supported as well. Rather than introducing something new or different, CouchbaseExecutor is created to make things simpler. Here is a simple sample:

static final String bucketName = "default";
static final Cluster cluster = CouchbaseCluster.create();
static final Bucket bucket = cluster.openBucket();
static final CouchbaseExecutor couchbaseExecutor = new CouchbaseExecutor(cluster, bucket);

static {
    CouchbaseExecutor.registerIdProeprty(Account.class, "id");

    System.setProperty("com.couchbase.queryEnabled", "true");

    //        bucket.query(Query.simple("CREATE PRIMARY INDEX `default-primary-index` ON `default' USING GSI;"));
    //        bucket.query(Query.simple("CREATE INDEX `default-id-index` ON `default`(id) USING GSI;"));
    //        bucket.query(Query.simple("CREATE INDEX `default-firstName-index` ON `default`(firstName) USING GSI;"));
    //        bucket.query(Query.simple("CREATE INDEX `default-lastName-index` ON `default`(lastName) USING GSI;"));
}

@Test
public void test_crud() {
    Account account = createAccount();
    // create
    couchbaseExecutor.insert(account);

    // read
    Account dbAccount = couchbaseExecutor.get(Account.class, account.getId());

    // update
    dbAccount.setFirstName("newFirstName");
    couchbaseExecutor.upsert(dbAccount);

    // it's testing on 4.0 Beta, which is not quite stable.
    N.sleep(2000);

    // To run sql query. enable N1QL. 
    String sql = NE3.select(CouchbaseExecutor.idNameOf(bucketName), "firstName", "lastName").from(bucketName).where(L.eq(LAST_NAME, L.QME)).sql();
    dbAccount = couchbaseExecutor.queryForEntity(Account.class, sql, dbAccount);

    // delete
    couchbaseExecutor.remove(dbAccount.getId());

    // check
    assertNull(couchbaseExecutor.get(Account.class, dbAccount.getId()));
}

Try it now. Here are the steps: