localhost / 127.0.0.1:9042]无法连接

问题描述 投票:0回答:1

尝试通过下面的代码将Cassandra与Java连接并获得localhost / 127.0.0.1:9042]无法连接错误-

    public static void main(String[] args)
    {
        Cluster cluster;
        Session session;
        //cluster connects to the address of the node provided.One contact point is required.Good to have multiple
        cluster=Cluster.builder().addContactPoint("localhost").build();

        session=cluster.connect("ecommerce");
        session.execute("INSERT INTO products (pdt_id, cat_id, pdt_name, pdt_desc, price, shipping) VALUES (002,105, 'Candy 0.9 cu. ft. Washing Machine', 'Capacity of 1 cu. ft.10 different power levels', 64.00, 'Expedited')");
        session.execute("INSERT INTO products (pdt_id, cat_id, pdt_name, pdt_desc, price, shipping) VALUES (003,106, 'Prestige 0.9 cu.cm. Pressure Cooker', 'Capacity: 18 qt.', 70.00, 'Dispatched from warehouse')");

        String pdtid = null, pdtname = null, pdtdesc = null;
        float price = 0;
        ResultSet resultSet=session.execute("select * from products");
        for(Row row:resultSet)
        {
            pdtid = Integer.toString(row.getInt("pdt_id"));
            pdtname = row.getString("pdt_name");
        }
        cluster.close();
        }

    }
java cassandra connection
1个回答
0
投票

Java代码语法在我看来是正确的。

请确保您的计算机上正在运行Cassandra,并且打开了端口9042(检查防火墙)。您可以检查执行cqlsh并查看Cassandra是否正在响应。

我看到您使用的驱动程序已过时。您应该考虑升级到4.x。这是完整的文档:https://docs.datastax.com/en/developer/java-driver/4.4/manual/core/

© www.soinside.com 2019 - 2024. All rights reserved.