在java中使用hector和cql3

b4wnujal  于 2021-06-15  发布在  Cassandra
关注(0)|答案(1)|浏览(305)

我运行的是Cassandra1.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集群并创建一行。赫克托的文件到处都是,或者cql3的文件不完整。我似乎找不到使用现有表连接到现有键空间并使用cql3添加记录的完整示例。
更新
经过更多的挖掘和诅咒,我发现这个例子项目使用Cassandra驱动核心。我会好好玩玩的,不过这是我想的。目前,它只是在beta2,但由于这只是个人学习,我会给这个机会,看看它如何进行。

nfeuvbwi

nfeuvbwi1#

我就是这样做连接部分的(我使用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 ```

<jdbc:initialize-database data-source="dataSource">
<jdbc:script location="${jdbc.initLocation}"/>
<jdbc:script location="${jdbc.dataLocation}"/>
</jdbc:initialize-database>

然后我就一直在这个班上胡闹

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` (除其他外):

相关问题