0

我正在运行 Cassandra 1.2.0 (CQL3),我正在使用 Hector 1.0.5。这是我的表定义:

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

我不知道如何连接到 cassandra 集群并创建一行。Hector 的文档到处都是,或者在 CQL3 的情况下不完整。我似乎找不到使用现有表连接到现有键空间并使用 CQL3 添加记录的完整示例。

更新

经过更多的挖掘和诅咒,我找到了这个使用 cassandra 驱动程序核心的示例项目。我会玩弄它,但这更多的是我的想法。目前它只是在 beta2 中,但由于这只是为了个人学习,我会试一试,看看它是如何进行的。

4

1 回答 1

0

这就是我做连接部分的方式(我使用spring/maven):

在一个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

在一个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" />

然后在这门课上做了我所有的时间-wimey wibbeley-wobbeley 恶作剧

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();
    }

}

我在我的pom.xml(除其他外)中使用过:

<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>

查询部分,......好吧,我仍在努力解决它:((参见:https ://stackoverflow.com/questions/26484525/in-cassandra-i-cant-seem-to-get-column-by-复合名称与赫克托 API

(我可能一如既往地忘记了一些事情,请不要犹豫告诉我,我会添加缺少的内容)

于 2014-10-22T11:31:54.753 回答