如何在java中传递值以查询cassnadra数据库
我正在云上运行cassandra数据库。我创建了一个RESTWeb服务来查询cassandra数据库。我想从将使用我的web服务的UI的用户那里获取输入 以下是我运行的代码:如何在java中传递值以查询cassnadra数据库,java,spring,web-services,rest,cassandra,Java,Spring,Web Services,Rest,Cassandra,我正在云上运行cassandra数据库。我创建了一个RESTWeb服务来查询cassandra数据库。我想从将使用我的web服务的UI的用户那里获取输入 以下是我运行的代码: public String cql2(int psa) { Cluster.Builder clusterBuilder = Cluster.builder() .addContactPoints("52.36.24.246").withPort(9042)
public String cql2(int psa)
{
Cluster.Builder clusterBuilder = Cluster.builder()
.addContactPoints("52.36.24.246").withPort(9042)
.withQueryOptions(new QueryOptions().setFetchSize(2000))
.withCredentials("username", "password");
Session session = clusterBuilder.build().connect();
String cqlStatement = "SELECT * FROM godfather.crime WHERE psa='"+psa+"' ALLOW FILTERING";
for (Row row : session.execute(cqlStatement))
{
cql=(row.toString());
}
return cql;
}
}
通常,当一个查询在应用程序的整个生命周期中要执行多次(有或没有不同的参数)时,准备好的语句是最佳解决方案 基本上,您的代码将变成:
public class MyService {
private Cluster cluster;
private Session session;
private PreparedStatement query;
// exception handling not included to keep the code short
public void init() {
cluster = Cluster.builder()
.addContactPoints("52.36.24.246").withPort(9042)
.withQueryOptions(new QueryOptions().setFetchSize(2000))
.withCredentials("username", "password")
.build();
session = cluster.connect();
query = session.prepare("SELECT * FROM godfather.crime WHERE psa= ?");
}
public String execQuery(int psa) {
for(Row r : session.execute(prepared.bind(psa))) {
// your processing here
}
}
}
另外还有几个细节:
集群
和会话
。这些是长寿命的物体,应该随身携带通常,当一个查询在应用程序的整个生命周期中要执行多次(有或没有不同的参数)时,准备好的语句是最佳解决方案 基本上,您的代码将变成:
public class MyService {
private Cluster cluster;
private Session session;
private PreparedStatement query;
// exception handling not included to keep the code short
public void init() {
cluster = Cluster.builder()
.addContactPoints("52.36.24.246").withPort(9042)
.withQueryOptions(new QueryOptions().setFetchSize(2000))
.withCredentials("username", "password")
.build();
session = cluster.connect();
query = session.prepare("SELECT * FROM godfather.crime WHERE psa= ?");
}
public String execQuery(int psa) {
for(Row r : session.execute(prepared.bind(psa))) {
// your processing here
}
}
}
另外还有几个细节:
集群
和会话
。这些是长寿命的物体,应该随身携带在此处输入格式化时可能键入的代码。很抱歉,在格式化时,此处输入的输入代码可能是错误的。对不起