简体   繁体   中英

How to prevent reordering of Cassandra Set when getting data with Java driver

Retrieval using collections describes collections order. In my case the feature of returning an ordered collection from the set <text> type is what I need. But collection deserilized to java Set and java Set does not provide any guarantee that the order of the elements will kept. Therefore, the query result may differ from the java object.

I try getSet() and it returns ordered collection. I don't undestand why. Perhaps this is due to deserialization of 'LinkedHashSet'?

How to guarantee getting a sorted collection with Java driver, as specified in the Cassandra documentation?

Guaranteed by SetCodec<T> :

package com.datastax.driver.core;

public abstract class TypeCodec<T> {

...

  /**
   * This codec maps a CQL {@link DataType#set(DataType) set type} to a Java {@link Set}.
   * Implementation note: this codec returns mutable, non thread-safe {@link LinkedHashSet}
   * instances.
   */
  private static class SetCodec<T> extends AbstractCollectionCodec<T, Set<T>> {

    private SetCodec(TypeCodec<T> eltCodec) {
      super(DataType.set(eltCodec.cqlType), TypeTokens.setOf(eltCodec.getJavaType()), eltCodec);
    }

    @Override
    protected Set<T> newInstance(int size) {
      return new LinkedHashSet<T>(size);
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM