cassandra java 例子,使用Java从Cassandra读取数据

My sample cassandra table looks like

id | article_read | last_hours | name

----+-----------------------------------

5 | [4, 5, 6] | 5 | shashank

10 | [12, 88, 32] | 1 | sam

8 | [4, 5, 6] | 8 | aman

7 | [5, 6] | 7 | ashif

6 | [4, 5, 6] | 6 | amit

9 | [4, 5, 6] | 9 | shekhar

My java code to read data from Cassandra table using cql queries,

Scanner sc = new Scanner(System.in);

System.out.println("enter name1 ");

String name1 = sc.nextLine();

System.out.println("enter name2");

String name2 = sc.nextLine();

Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();

Session session = cluster.connect("tp");

PreparedStatement queryStmt = session.prepare("select article_read from bat where name = ?");

ResultSet result1 = session.execute(queryStmt.bind(name1));

ResultSet result2 = session.execute(queryStmt.bind(name2));

System.out.println(result1.all());

System.out.println(result2.all());

if(result1.equals(result2))

{

System.out.println("100% sentiment ");

}

else

{

System.out.println("no sentiment ");

}

}

look at my code ,its running but when i am putting name1,name2 shashank and aman its giving 100 % but when giving shashank and ashif result is again 100% match..

解决方案

First prepared the query only once like below :

//Prepared only once.

PreparedStatement queryStmt = session.prepare("select * from bat where name = ?");

Then you can use it any number of time like below :

ResultSet result1 = session.execute(queryStmt.bind("shashank"));

ResultSet result2 = session.execute(queryStmt.bind("aman"));

Edited

try (Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build(); Session session = cluster.connect("test")) {

Scanner sc = new Scanner(System.in);

System.out.println("enter name1 ");

String name1 = sc.nextLine();

System.out.println("enter name2");

String name2 = sc.nextLine();

PreparedStatement queryStmt = session.prepare("select article_read from bat where name = ?");

ResultSet result1 = session.execute(queryStmt.bind(name1));

ResultSet result2 = session.execute(queryStmt.bind(name2));

if (result1.one().getList("article_read", Integer.class).equals(result2.one().getList("article_read", Integer.class))) {

System.out.println("100% sentiment ");

} else {

System.out.println("no sentiment ");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值