我正在运行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集群并创建一行。在CQL3的情况下,Hector的文档遍布整个地方或不完整。我似乎无法找到使用现有表连接到现有键空间并使用CQL3添加记录的完整示例。
更新
经过多次挖掘和诅咒后,我发现了这个示例项目using the cassandra driver core。我会玩它,但这更像是我的想法。目前它刚刚进入beta2,但由于这仅仅是为了个人学习,我会给它一个镜头,看看它是怎么回事。
答案 0 :(得分:0)
答案 1 :(得分:0)
这就是我连接部分(我使用spring / maven)的方式:
<{1>}中的:
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
然后用这个班级做了我所有的时间 - wimey wibbeley-wobbeley恶作剧
<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" />
我在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
查询部分,......我还在努力解决它:((见:https://stackoverflow.com/questions/26484525/in-cassandra-i-cant-seem-to-get-column-by-composite-name-with-hector-api)
(我可能一如既往地忘记了事情,不要犹豫告诉我,我会补充一些缺失的东西)