Search code examples
sqlscalacachingignite

Sorted table with a map in Apache Ignite


I initially want to accomplish something simple with Ignite. I have a type like this (simplified):

case class Product(version: Long, attributes: Map[String, String])

I have a key for each one to store it by (it's one of the attributes).

I'd like to store them such that I can retrieve a subset of them between two version numbers or, at the very least, WHERE version > n. The problem is that the cache API only seems to support either retrieval by key or table scan. On the other hand, SQL99 doesn't seem to have any kind of map type.

I was thinking I'd need to use a binary marshaler, but the docs say:

There is a set of 'platform' types that includes primitive types, String, UUID, Date, Timestamp, BigDecimal, Collections, Maps and arrays of thereof that will never be represented as a BinaryObject.

So... maps are supported?

Here's my test code. It fails with java.lang.IllegalArgumentException: Cache is not configured: ignite-sys-cache, though. Any help getting a simple test working would really aid my understanding of how this is supposed to work.

Oh, and also, do I need to configure the schema in the Ignite config file? Or are the field attributes a sufficient alternative to that?

case class Product(
    @(QuerySqlField @field)(index = true) version: Long,
    attributes: java.util.Map[String, String]
)

object Main {
    val TestProduct = Product(2L, Map("pid" -> "123", "foo" -> "bar", "baz" -> "quux").asJava)

    def main(args: Array[String]): Unit = {
        Ignition.setClientMode(true)
        val ignite = Ignition.start()
        val group = ignite.cluster.forServers

        val cacheConfig = new CacheConfiguration[String, Product]
        cacheConfig.setName("inventory1")
        cacheConfig.setIndexedTypes(classOf[String], classOf[Product])
        val cache = ignite.getOrCreateCache(cacheConfig)

        cache.put("P123", TestProduct)

        val query = new SqlQuery(classOf[Product], "select * from Product where version > 1")
        val resultSet = cache.query(query)
        println(resultSet)
    }
}

Solution

  • Ignite supports querying by indexed fields. Since version is a regular indexed field it should be feasible to do the described queries.

    I've checked your code and it works on my side.
    Please check that the Ignite version is consistent across all the nodes.

    If you provide the full logs I could take a look at it.