Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中传递值以查询cassnadra数据库_Java_Spring_Web Services_Rest_Cassandra - Fatal编程技术网

如何在java中传递值以查询cassnadra数据库

如何在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)

我正在云上运行cassandra数据库。我创建了一个RESTWeb服务来查询cassandra数据库。我想从将使用我的web服务的UI的用户那里获取输入

以下是我运行的代码:

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

另外还有几个细节:

  • 建议每个应用程序只初始化
    集群
    会话
    。这些是长寿命的物体,应该随身携带
  • 建议只编写一次报表
  • 为了保持代码简单,上面的代码中没有包括异常处理、初始化等内容
  • 如果您想了解有关准备语句的更多信息:

  • 与其他数据库一样,Cassandra也支持预先准备好的语句。你可以阅读更多关于这方面准备好的声明的优点
  • 用卡桑德拉语编写的声明在

  • 通常,当一个查询在应用程序的整个生命周期中要执行多次(有或没有不同的参数)时,准备好的语句是最佳解决方案

    基本上,您的代码将变成:

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

    另外还有几个细节:

  • 建议每个应用程序只初始化
    集群
    会话
    。这些是长寿命的物体,应该随身携带
  • 建议只编写一次报表
  • 为了保持代码简单,上面的代码中没有包括异常处理、初始化等内容
  • 如果您想了解有关准备语句的更多信息:

  • 与其他数据库一样,Cassandra也支持预先准备好的语句。你可以阅读更多关于这方面准备好的声明的优点
  • 用卡桑德拉语编写的声明在

  • 在此处输入格式化时可能键入的代码。很抱歉,在格式化时,此处输入的输入代码可能是错误的。对不起