MyBatis Cassandra结果集地图(MyBatis Cassandra Result Set Map)

编程入门 行业动态 更新时间:2024-10-23 04:57:49
MyBatis Cassandra结果集地图(MyBatis Cassandra Result Set Map)

我们使用MyBatis-Spring来管理DBMS事务(Cassandra)。 在Cassandra中,我们有一个用map列定义的表,它用作分区键(我们的键必须是一个复杂的类型,因此不能选择更改它)。

CREATE TABLE mykeyspace.mytable ( name text, level frozen<map<text,text>>, effective_ts timestamp ... PRIMARY KEY ((name, level), effective_ts))

如果我在csqlsh中执行对Cassandra的查询,如下所示,我得到一个结果(即预期的正确结果集)。

SELECT * FROM mykeyspace.mytable where name = 'somename' and level = {'mykey','myvalue'} ;

但是,当通过MyBatis执行此操作时,我得不到任何结果。 查询没有抛出任何异常,我可以清楚地看到MyBatis框架的日志中的查询与我在cqlsh提示符中使用的查询匹配。

01/11/2017 14:35:24:644 [DEBUG] getSetForName - ==> Preparing: SELECT * FROM mykeyspace.mytable WHERE NAME = 'somename' AND LEVEL = {'mykey':'myvalue'} 01/11/2017 14:35:24:777 [DEBUG] getValueSetForParameterSet - ==> Parameters: 01/11/2017 14:35:24:890 [DEBUG] getValueSetForParameterSet - <== Total: 0 01/11/2017 14:35:24:890 [DEBUG] SqlSessionUtils - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@22b1651]

另一个注意事项我试图使用DataStax驱动程序核心(即执行不带MyBatis的查询)并获得预期的结果集。 因此,我的问题必须与MyBatis有关,但此时我已经用尽了所有想法。 有什么想法或建议吗?

供参考...... Mapper配置......

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.mycompany.mapper.MyMapper"> <select id="getSetForName" resultType="string" parameterType="map"> SELECT * FROM MYKEYSPACE.MYTABLE WHERE NAME = '${name}' AND LEVEL = ${level} </select> </mapper>

供参考......使用DataStax核心API ...

private Cluster cluster; private Session session; public void doQuery(String name, String level) { String query = "select * from mykeyspace.mytable where name = '" + name + "' and level = " + level; Session s = get Session(); ResultSet results = s.execute(query); for(Row r: results) { ... } } private Session getSession() { cluster = connectCluster("myhost"); session = cluster.connect(); return session; } private Cluster connectCluster(String node) { return Cluster.builder().addContactPoint(node).build(); }

版本信息:mybatis-spring 1.3,mybatis v3.4.1,spring 4.3.4-RELEASE

We are using MyBatis-Spring to manage DBMS transactions (Cassandra). Within Cassandra we have a table defined with a map column, which serves as a Partition Key (our keys have to be a complex type, so changing this is not an option).

CREATE TABLE mykeyspace.mytable ( name text, level frozen<map<text,text>>, effective_ts timestamp ... PRIMARY KEY ((name, level), effective_ts))

If I execute a query against Cassandra in csqlsh as follows I get a result back (i.e. the correct result set expected).

SELECT * FROM mykeyspace.mytable where name = 'somename' and level = {'mykey','myvalue'} ;

However, when executing this through MyBatis, I get no results back. The query is not throwing any exceptions, and I can clearly see the query in the logs from the MyBatis framework matches that which I have used in the cqlsh prompt.

01/11/2017 14:35:24:644 [DEBUG] getSetForName - ==> Preparing: SELECT * FROM mykeyspace.mytable WHERE NAME = 'somename' AND LEVEL = {'mykey':'myvalue'} 01/11/2017 14:35:24:777 [DEBUG] getValueSetForParameterSet - ==> Parameters: 01/11/2017 14:35:24:890 [DEBUG] getValueSetForParameterSet - <== Total: 0 01/11/2017 14:35:24:890 [DEBUG] SqlSessionUtils - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@22b1651]

Another note I attempted to use the DataStax driver core (i.e. executing the query without MyBatis) and got the expected result set as well. Therefore, my issue has to be related to MyBatis, but I've exhausted all ideas at this point. Any thoughts or suggestions?

For reference... Mapper configuration...

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.mycompany.mapper.MyMapper"> <select id="getSetForName" resultType="string" parameterType="map"> SELECT * FROM MYKEYSPACE.MYTABLE WHERE NAME = '${name}' AND LEVEL = ${level} </select> </mapper>

For reference... Using the DataStax core API...

private Cluster cluster; private Session session; public void doQuery(String name, String level) { String query = "select * from mykeyspace.mytable where name = '" + name + "' and level = " + level; Session s = get Session(); ResultSet results = s.execute(query); for(Row r: results) { ... } } private Session getSession() { cluster = connectCluster("myhost"); session = cluster.connect(); return session; } private Cluster connectCluster(String node) { return Cluster.builder().addContactPoint(node).build(); }

Version Info: mybatis-spring 1.3, mybatis v3.4.1, spring 4.3.4-RELEASE

最满意答案

当执行带有嵌入式映射文字的cql字符串时,例如... where level = {'key2':'value2', 'key1':'value1'} ,键将按照降序顺序重新排序键类型。 在这种情况下,密钥类型是varchar,因此密钥将按字母顺序降序排列区分大小写。 插入{'zbc':'', 'abc':'', '1':'', '1abc':'', 'q12':'', 'abbc':'', 'Abbc':''}实际插入[1=, 1abc=, Abbc=, abbc=, abc=, q12=, zbc=] 。

使用绑定参数执行语句时,将保留绑定映射对象中键的顺序。 这意味着如果您使用的是像HashMap<K, V>这样的无序地图结构HashMap<K, V>则无法保证订单。

比较地图对象时,订单很重要。 映射条目顺序的差异导致比较失败,并且不返回任何结果。

您可以使用SortedMap<K,V>实现(例如TreeMap<K,V>此问题,默认情况下,按字母顺序对命令键区分大小写。

When executing a cql string with an embedded map literal, such as ... where level = {'key2':'value2', 'key1':'value1'}, the keys will be re-ordered in descending order based on the key type. In this case, the key type is varchar, so the keys will be ordered descending alphabetically case-sensitive. Inserting {'zbc':'', 'abc':'', '1':'', '1abc':'', 'q12':'', 'abbc':'', 'Abbc':''} results in [1=, 1abc=, Abbc=, abbc=, abc=, q12=, zbc=] actually being inserted.

When executing your statement using bound parameters, the ordering of the keys within the bound map object is preserved. This means that if you're using an unordered map structure like HashMap<K, V> you can't guarantee the order.

When comparing map objects, order matters. The discrepancy in the order of the map entries is causing the compare to fail and no results to be returned.

You can work around this by using a SortedMap<K,V> implementation such as TreeMap<K,V> which, by default, orders keys alphabetically case-sensitive.

更多推荐

本文发布于:2023-08-04 08:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1413082.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:地图   Cassandra   MyBatis   Map   Set

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!