AbacusUtil Docs | Download | Javadoc | FAQ | Community

CassandraExecutor

CassandraExecutor is a wrapper of Cassandra Java driver. Just like SQLExecutor, it makes things much easier to operate data with Casssandra 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, CassandraExecutor is created to make things simpler. Here is a simple sample:

static final String keySpace = "codes";

static final String sql_createKeySpace = "CREATE KEYSPACE IF NOT EXISTS " + keySpace
        + " WITH replication={'class':'SimpleStrategy', 'replication_factor':3}";
static final String sql_dropTable = "DROP TABLE IF EXISTS account";
static final String sql_createTable = "CREATE TABLE IF NOT EXISTS account(id varchar PRIMARY KEY, gui varchar, first_name varchar, last_name varchar, status int, last_update_time timestamp, create_time timestamp)";

static final CassandraExecutor cassandraExecutor = new CassandraExecutor(Cluster.builder().addContactPoint("127.0.0.1").build(), keySpace);

static {
    cassandraExecutor.execute(sql_createKeySpace);
    cassandraExecutor.execute(sql_dropTable);
    cassandraExecutor.execute(sql_createTable);
}

@Test
public void test_crud() {
    Account account = createAccount();
    // create
    String sql_insert = NE.insert(ID, GUI, FIRST_NAME, LAST_NAME, LAST_UPDATE_TIME, CREATE_TIME).into(Account.class).sql();
    cassandraExecutor.execute(sql_insert, account);

    // read
    String sql_selectByGUI = NE.select(ID, GUI, FIRST_NAME, LAST_NAME).from(Account._).where(L.eq(ID, L.QME)).sql();
    Account dbAccount = cassandraExecutor.queryForEntity(Account.class, sql_selectByGUI, account);

    // update
    String sql_updateByLastName = NE.update(Account.class).set(FIRST_NAME).where(L.eq(ID, L.QME)).sql();
    dbAccount.setFirstName("newFirstName");
    cassandraExecutor.execute(sql_updateByLastName, dbAccount);

    // delete
    String sql_deleteByFirstName = NE.deleteFrom(Account.class).where(L.eq(ID, L.QME)).sql();
    cassandraExecutor.execute(sql_deleteByFirstName, dbAccount);

    // check
    assertFalse(cassandraExecutor.exists(sql_selectByGUI, dbAccount));
}

User-defined types is supported by TypeCodec in Cassandra java driver 3.0: Refer to: http://datastax.github.io/java-driver/manual/custom_codecs/. Here is a simple sample by CassandraExecutor

static final CassandraExecutor cassandraExecutor;

static {
	final CodecRegistry codecRegistry = new CodecRegistry();
	final Cluster cluster = Cluster.builder().withCodecRegistry(codecRegistry).addContactPoint("127.0.0.1").build();

	codecRegistry.register(new UDTCodec<Users.Name>(TypeCodec.userType(cluster.getMetadata().getKeyspace("simplex").getUserType("fullname")), Users.Name.class) {
	// or codecRegistry.register(new UDTCodec<Users.Name>(cluster, "simplex", "fullname", Users.Name.class) {
		@Override
		protected Users.Name deserialize(UDTValue value) {
			if (value == null) {
				return null;
			}
			Users.Name name = new Users.Name();
			name.setFirstName(value.getString("firstName"));
			name.setLastName(value.getString("lastName"));
			return name;
		}

		@Override
		protected UDTValue serialize(Users.Name value) {
			return value == null ? null
					: newUDTValue().setString("firstName", value.getFirstName()).setString("lastName",
							value.getLastName());
		}
	});
	
	codecRegistry.register(new UDTCodec<Users.Address>(TypeCodec.userType(cluster.getMetadata().getKeyspace("simplex").getUserType("address")), Users.Address.class) {
	// or codecRegistry.register(new UDTCodec<Users.Address>(cluster, "simplex", "address", Users.Address.class) {
		@Override
		protected Users.Address deserialize(UDTValue value) {
			if (value == null) {
				return null;
			}
			Users.Address address = new Users.Address();
			address.setStreet(value.getString("street"));
			address.setCity(value.getString("city"));
			address.setZipCode(value.getInt("zipCode"));
			return address;
		}

		@Override
		protected UDTValue serialize(Users.Address value) {
			return value == null ? null
					: newUDTValue().setString("street", value.getStreet()).setInt("zipcode", value.getZipCode());
		}
	});

	cassandraExecutor = new CassandraExecutor(cluster);
}

Try it now. Here are the steps: