0

I'm running Cassandra 1.2.0 (CQL3) and I'm using Hector 1.0.5. Here's my table definition:

CREATE TABLE group_profiles (
    profile_id text,
    time timeuuid,
    document text,
    PRMARY KEY (profile_id, time)
) WITH COMPACT STORAGE;

I can't figure out how to connect to the the cassandra cluster and create a row. The docs for Hector are all over the place or incomplete in the case of CQL3. I can't seem to find a complete example of connecting to an existing keyspace with an existing table and adding a record using CQL3.

UPDATE

After more digging and cursing I found this example project using the cassandra driver core. I'll play around with it, but this is more what I had in mind anyway. At the moment it is just in beta2, but since this is just for personal learning I'll give this a shot and see how it goes.

Mark J Miller
  • 4,751
  • 5
  • 44
  • 74
  • Hello, at which point does your example ( https://github.com/boneill42/naughty-or-nice ) start using cassandra driver core ? All I can see there is the use of Astyanax :s – Ar3s Oct 22 '14 at 10:49

1 Answers1

0

This is how I did the connection part (I use spring/maven) :

in a data-access.properties :

jdbc.driverClassName=org.apache.cassandra.cql.jdbc.CassandraDriver
jdbc.url=jdbc:cassandra://localhost:9160/mykeyspace
# not sure if userName and Passwords are really usefull, haven't tested, I should though, I will ... Just did ... No real impact with my cassandra configuration
jdbc.username=IamG
jdbc.password=root

# Properties that control the population of schema and data for a new data source
jdbc.initLocation=classpath:db/cassandra/initDB.cql
jdbc.dataLocation=classpath:db/cassandra/populateDB.cql
jpa.showSql=true

In a data-Source-config.xml

<bean id="dataSource"
    class="org.apache.tomcat.jdbc.pool.DataSource"
    p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.url}"
    p:username="${jdbc.username}"
    p:password="${jdbc.password}"/>

<!-- Database initializer. If any of the script fails, the initialization stops. -->
<!-- As an alternative, for embedded databases see <jdbc:embedded-database/>. -->
<jdbc:initialize-database data-source="dataSource">
    <jdbc:script location="${jdbc.initLocation}"/>
    <jdbc:script location="${jdbc.dataLocation}"/>
</jdbc:initialize-database>

<bean id="keySpaceManager"
    class="org.me.cassandra.persistence.KeyspaceManagerImpl"
    p:dataSource-ref="dataSource" />

And then did all my timey-wimey wibbeley-wobbeley shenanigans with this class

import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.factory.HFactory;

import org.apache.cassandra.cql.jdbc.UtilsProxy;
import org.apache.tomcat.jdbc.pool.DataSourceProxy;


public class KeyspaceManagerImpl implements KeyspaceManager {

    private DataSourceProxy dataSource;

    @Override
    public void setDataSource(DataSourceProxy dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public Cluster getCluster() {
        String hostName = getHostName();
        String clusterName = "arbitraryClusterNameProbablyShouldBeSetInConfig.xml";
        return HFactory.getOrCreateCluster(clusterName, hostName);
    }

    @Override
    public Keyspace getKeyspace() {
        String databaseName = getDatabaseName();
        Cluster cluster = getCluster();
        return HFactory.createKeyspace(databaseName, cluster);
    }

    private String getHostName() {
        return getConnectionProperties().getProperty(UtilsProxy.TAG_SERVER_NAME);
    }

    private String getDatabaseName() {
        return getConnectionProperties().getProperty(UtilsProxy.TAG_DATABASE_NAME);
    }

    private Properties getConnectionProperties() {
        return UtilsProxy.getConnectionProperties(dataSource);
    }
}


// and this other class that was needed to proxy some very interesting but not visible (package visible) one
package org.apache.cassandra.cql.jdbc; // <- package is very important here !

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.tomcat.jdbc.pool.DataSourceProxy;
import org.apache.tomcat.jdbc.pool.PoolConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This class proxies an utility class that has been declared as "Package visible"
 * 
 * Some utility methods were added (1 so far when this javadoc was written)
 * 
 * @author gulhe
 *
 */
public class UtilsProxy extends Utils {

    private static final Logger log = LoggerFactory.getLogger(UtilsProxy.class);

    private static final Map<String, Properties> urlParseStore = new HashMap<>();

    /**
     * Gets cassandra connection properties from a dataSourceProxy
     * The information used will be the full connectionUrl.
     * Said URL will then be parsed.
     * 
     * To avoid reparsing the same string multiple times, results are stored by their url String. 
     * 
     * @return a java.util.Properties holding Cassandra connection info
     */
    public static Properties getConnectionProperties(DataSourceProxy dataSource) {
        PoolConfiguration poolProperties = dataSource.getPoolProperties();
        String url = poolProperties.getUrl();

        Properties alreadyStoredProperties = urlParseStore.get(url);
        if (alreadyStoredProperties != null) {
            return alreadyStoredProperties;
        }

        try {
            Properties urlParsedConnectionProperties = Utils.parseURL(url);
            urlParseStore.put(url, urlParsedConnectionProperties);
            return urlParsedConnectionProperties;
        } catch (SQLException e) {
            log.error("Something went wrong !", e);
        }

        return new Properties();
    }

}

I used in my pom.xml (among other things) :

<dependencies>
    <dependency>
        <groupId>org.apache-extras.cassandra-jdbc</groupId>
        <artifactId>cassandra-jdbc</artifactId>
        <version>1.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.hectorclient</groupId>
        <artifactId>hector-core</artifactId>
        <exclusions>
            <!-- some exclusions here due to my environment, I omitted them here I can add them back by popular demand -->
        </exclusions>
        <version>2.0-0</version>
    </dependency>
</dependencies>

The query part, ... well I'm still struggling with it :( (see : https://stackoverflow.com/questions/26484525/in-cassandra-i-cant-seem-to-get-column-by-composite-name-with-hector-api )

(I might, as always, have forgotten things don't hesitate to tell me, I'll add what's missing)

Community
  • 1
  • 1
Ar3s
  • 2,237
  • 2
  • 25
  • 47