[英]Using Hector with CQL3 in java
我正在運行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中,但是由於這只是個人學習,因此我將對此進行一下介紹,並觀察其進展。
這是我做連接部分的方式(我使用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" />
然后我所有的時光般的wibbeley wobbeley shenanigans都參加了這堂課
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-帶有hector-api的復合名稱 )
(我可能一如既往地忘記了事情,請立即告訴我,我會補充丟失的內容)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.