搭建Cassandra环境,参考:
https://blog.csdn.net/abckingaa/article/details/123440517
本文使用Java JDBC方式,操作Cassandra数据库(SUID:查改增删)
一、配置数据库连接方式
#bee.db.dbName=Cassandra
bee.db.url =jdbc:cassandra://127.0.0.1:9042
bee.db.username =
bee.db.password =
bee.db.schemaName=store
二、查询shopping_cart表的数据
NameTranslateHandle.setSchemaName("store"); //cassandra's keyspace
Suid suid=BF.getSuid(); //获取操作DB(Select,update,insert,delete)的对象
List list2=suid.select(new ShoppingCart()); //查询
Printer.printList(list2); //打印列表
三、日志打印
[INFO] [Bee] select SQL: select userid,item_count,last_update_timestamp from store.shopping_cart
[INFO] [Bee] | <-- select rows: 3
[INFO] ShoppingCart[userid=4567,itemCount=20,lastUpdateTimestamp=null]
[INFO] ShoppingCart[userid=1234,itemCount=5,lastUpdateTimestamp=2022-03-12 12:57:08.79]
[INFO] ShoppingCart[userid=9876,itemCount=2,lastUpdateTimestamp=2022-03-12 12:57:07.482]
完整实例
package org.teasoft.exam.cassandra;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.teasoft.bee.osql.PreparedSql;
import org.teasoft.bee.osql.Suid;
import org.teasoft.exam.comm.Printer;
import org.teasoft.honey.osql.core.Logger;
import org.teasoft.honey.osql.core.NameTranslateHandle;
import org.teasoft.honey.osql.shortcut.BF;
/**
* @author Kingstar
* @since 1.11
*/
public class CassandraTest {
public static void main(String[] args) {
PreparedSql pre = BF.getPreparedSql();
// String sql = "SELECT * FROM store.shopping_cart";
// String sql = "select * from store.shopping_cart where 1!=1";
String sql = "select * from store.cassandra_table limit 1";
// String sql = "select * from store.shopping_cart where userid='4567'";
List list=pre.select(sql);
Printer.print(list);
NameTranslateHandle.setSchemaName("store");
// NameTranslateHandle.setSchemaNameLocal("store2");
Suid suid=BF.getSuid();
List list2=suid.select(new ShoppingCart());
Printer.printList(list2);
ShoppingCart shoppingCart=new ShoppingCart();
shoppingCart.setUserid("4567");
List list3=suid.select(shoppingCart);
Printer.printList(list3);
CassandraTable t=new CassandraTable();
// t.setF0Id(11111L);
List f22List=new ArrayList<>();
f22List.add(11);
f22List.add(22);
t.setF22List(f22List);
Set f23Set=new HashSet();
f23Set.add("aa");
f23Set.add("bb");
t.setF23Set(f23Set);
Map f24Map=new HashMap<>();
f24Map.put("a", 123L);
f24Map.put("b", 126L);
t.setF24Map(f24Map);
int isertNum=suid.insert(t); //同一个id,多次插入,是在同一行更新.
System.out.println("isertNum:"+isertNum); //返回0, 行数不对
t.setId(1L);
t.setF20Varchar("test-update2");
int updateNume=suid.update(t);
System.out.println("updateNume:"+updateNume);//返回0, 行数不对
// String insertSql="insert into store.cassandra_table(id,f19_uuid) values (3,uuid())";
// String insertSql="insert into store.cassandra_table(id,f19_uuid) values (5,uuid('edab4b7b-4978-4249-a0da-ef6bd05accca'))";
String insertSql="insert into store.cassandra_table(id,f19_uuid) values (8,uuid())";
int a=pre.modify(insertSql);
Logger.info(a);
List list4=suid.select(new CassandraTable());
Printer.printList(list4);
}
}
源码:
https://gitee.com/automvc/bee-exam
Bee,互联网新时代的Java ORM工具,更快、更简单、更自动,开发速度快,运行快,更智能!
Bee让程序员/软件工程师,从手工编码中解放出来,Bee更适合智能软件制造时代!
十分钟即可入门!
立志做最懂用户的软件!