io.lettuce.core.codec.RedisCodec Java Examples

The following examples show how to use io.lettuce.core.codec.RedisCodec. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RedisSources.java    From hazelcast-jet-contrib with Apache License 2.0 5 votes vote down vote up
StreamRedisP(
        RedisURI uri,
        Map<K, String> streamOffsets,
        EventTimePolicy<? super T> eventTimePolicy,
        SupplierEx<RedisCodec<K, V>> codecFn,
        FunctionEx<? super StreamMessage<K, V>, ? extends T> projectionFn
) {

    this.uri = uri;
    this.streamOffsets = streamOffsets;
    this.codecFn = codecFn;
    this.projectionFn = projectionFn;

    this.eventTimeMapper = new EventTimeMapper<>(eventTimePolicy);
}
 
Example #2
Source File: LettuceSessionManager.java    From redis-session-manager with Apache License 2.0 5 votes vote down vote up
private GenericObjectPool<StatefulRedisConnection<String, Object>> createPool(List<String> nodes, RedisCodec<String, Object> codec) {
    GenericObjectPoolConfig<StatefulRedisConnection<String, Object>> cfg = new GenericObjectPoolConfig<>();
    cfg.setTestOnBorrow(true);
    cfg.setMinEvictableIdleTimeMillis(TimeUnit.MINUTES.toMillis(5));
    cfg.setMaxTotal(getMaxConnPoolSize());
    cfg.setMinIdle(getMinConnPoolSize());

    if (nodes.size() == 1) {
        return ConnectionPoolSupport.createGenericObjectPool(
            () -> client.connect(codec, RedisURI.create(nodes.get(0))),
            cfg
        );
    } else {
        List<RedisURI> uris = nodes.stream()
            .map(RedisURI::create)
            .collect(Collectors.toList());
        return ConnectionPoolSupport.createGenericObjectPool(
            () -> {
                StatefulRedisMasterReplicaConnection<String, Object> connection =
                    MasterReplica.connect(client, codec, uris);
                connection.setReadFrom(ReadFrom.MASTER_PREFERRED);
                return connection;
            },
            cfg
        );
    }
}
 
Example #3
Source File: RedisSources.java    From hazelcast-jet-contrib with Apache License 2.0 5 votes vote down vote up
ProcSupplier(
        RedisURI uri,
        Map<K, String> streamOffsets,
        EventTimePolicy<? super T> eventTimePolicy,
        SupplierEx<RedisCodec<K, V>> codecFn,
        FunctionEx<? super StreamMessage<K, V>, ? extends T> projectionFn
) {
    this.uri = uri;
    this.streamOffsets = streamOffsets;
    this.eventTimePolicy = eventTimePolicy;
    this.codecFn = codecFn;
    this.projectionFn = projectionFn;
}
 
Example #4
Source File: RedisSources.java    From hazelcast-jet-contrib with Apache License 2.0 5 votes vote down vote up
MetaSupplier(
        RedisURI uri,
        Map<K, String> streamOffsets,
        EventTimePolicy<? super T> eventTimePolicy,
        SupplierEx<RedisCodec<K, V>> codecFn,
        FunctionEx<? super StreamMessage<K, V>, ? extends T> projectionFn
) {
    this.uri = uri;
    this.streamOffsets = streamOffsets;
    this.eventTimePolicy = eventTimePolicy;
    this.codecFn = codecFn;
    this.projectionFn = projectionFn;
}
 
Example #5
Source File: RedisSources.java    From hazelcast-jet-contrib with Apache License 2.0 5 votes vote down vote up
static <K, V, T> ProcessorMetaSupplier streamRedisP(
        RedisURI uri,
        Map<K, String> streamOffsets,
        EventTimePolicy<? super T> eventTimePolicy,
        SupplierEx<RedisCodec<K, V>> codecFn,
        FunctionEx<? super StreamMessage<K, V>, ? extends T> projectionFn
) {
    return new MetaSupplier<>(uri, streamOffsets, eventTimePolicy, codecFn, projectionFn);
}
 
Example #6
Source File: RedisSources.java    From hazelcast-jet-contrib with Apache License 2.0 5 votes vote down vote up
SortedSetContext(RedisURI uri, RedisCodec<K, V> codec, K key, long start, long stop) {
    client = RedisClient.create(uri);
    connection = client.connect(codec);

    RedisAsyncCommands<K, V> commands = connection.async();
    commandFuture = commands.zrangebyscoreWithScores(this, key, Range.create(start, stop));
}
 
Example #7
Source File: RedisSources.java    From hazelcast-jet-contrib with Apache License 2.0 5 votes vote down vote up
HashContext(
        RedisURI uri,
        K hash,
        SupplierEx<RedisCodec<K, V>> codecSupplier,
        FunctionEx<Map.Entry<K, V>, T> mapFn
) {
    this.client = RedisClient.create(uri);
    this.connection = client.connect(codecSupplier.get());

    this.mapFn = mapFn;

    RedisAsyncCommands<K, V> commands = connection.async();
    this.commandFuture = commands.hgetall(this, hash);
}
 
Example #8
Source File: RedisSinks.java    From hazelcast-jet-contrib with Apache License 2.0 5 votes vote down vote up
private StreamContext(
        RedisURI uri,
        K stream,
        SupplierEx<RedisCodec<K, V>> codecFn,
        FunctionEx<T, Map<K, V>> mapFn
) {
    this.stream = stream;
    this.mapFn = mapFn;

    redisClient = RedisClient.create(uri);
    connection = redisClient.connect(codecFn.get());
}
 
Example #9
Source File: RedisSinks.java    From hazelcast-jet-contrib with Apache License 2.0 5 votes vote down vote up
SortedSetContext(RedisURI uri, K sortedSet, RedisCodec<K, V> codec, FunctionEx<T, Double> scoreFn,
                 FunctionEx<T, V> valueFn) {
    this.client = RedisClient.create(uri);
    this.connection = client.connect(codec);
    this.commands = connection.async();
    this.sortedSet = sortedSet;
    this.scoreFn = scoreFn;
    this.valueFn = valueFn;
}
 
Example #10
Source File: RedisSources.java    From hazelcast-jet-contrib with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a {@link StreamSource} which reads all items from multiple Redis
 * Streams (starting from given indexes) and emits them as they arrive.
 * <p>
 * Here is an example which reads all elements from two different Redis
 * Streams, maps the objects received to a stream specific ID and drains
 * the results out to some generic sink.
 * <pre>{@code
 * Map<String, String> streamOffsets = new HashMap<>();
 * streamOffsets.put("streamA", "0");
 * streamOffsets.put("streamB", "0");
 *
 * RedisURI uri = RedisURI.create("redis://localhost/");
 *
 * Pipeline.create()
 *     .readFrom(RedisSources.stream("source", uri, streamOffsets,
 *                      StringCodec::new,
 *                      mes -> mes.getStream() + " - " + mes.getId()))
 *     .withoutTimestamps()
 *     .writeTo(sink);
 * }</pre>
 *
 * @param <K>           type of the stream identifier and of fields (keys of
 *                      the Redis Stream entry's body map)
 * @param <V>           type of the field values in the message body
 * @param <T>           type of data coming out of the stream, the result of
 *                      applying the projection function over {@link
 *                      StreamMessage} instances
 * @param name          name of the source being created
 * @param uri           URI of the Redis server
 * @param streamOffsets map keyed by stream identifiers, containing offset
 *                      values from which to start element retrieval of each
 *                      stream
 * @param codecFn       supplier of {@link RedisCodec} instances, used in
 *                      turn for serializing/deserializing keys and values
 * @param projectionFn  built in mapping function of the source which can be
 *                      used to map {@link StreamMessage} instances received
 *                      from Redis to an arbitrary type of output; this
 *                      could be done by an external mapping function in the
 *                      pipeline too, but it's included for convenience
 * @return source to use in {@link com.hazelcast.jet.pipeline.Pipeline#readFrom}
 */
@Nonnull
public static <K, V, T> StreamSource<T> stream(
        @Nonnull String name,
        @Nonnull RedisURI uri,
        @Nonnull Map<K, String> streamOffsets,
        @Nonnull SupplierEx<RedisCodec<K, V>> codecFn,
        @Nonnull FunctionEx<? super StreamMessage<K, V>, ? extends T> projectionFn
) {
    Objects.requireNonNull(name, "name");
    Objects.requireNonNull(uri, "uri");
    Objects.requireNonNull(streamOffsets, "streamOffsets");
    Objects.requireNonNull(codecFn, "codecFn");
    Objects.requireNonNull(projectionFn, "projectionFn");

    Util.checkSerializable(codecFn, "codecFn");
    Util.checkSerializable(projectionFn, "projectionFn");

    return streamFromProcessorWithWatermarks(name, false,
            w -> StreamRedisP.streamRedisP(uri, streamOffsets, w, codecFn, projectionFn));
}
 
Example #11
Source File: RedisSources.java    From hazelcast-jet-contrib with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a {@link BatchSource} which queries a Redis Sorted Set for a
 * range of elements having their scores between the two limit values
 * provided (from & to). The returned elements will be emitted as they
 * arrive, the batch ends when all elements have been received.
 * <p>
 * Here's an example which reads a range from a Sorted Set, maps the items
 * to strings and drains them to some sink.
 * <pre>{@code
 *      RedisURI uri = RedisURI.create("redis://localhost/");
 *      Pipeline.create()
 *          .readFrom(RedisSources.sortedSet("source", uri, "sortedSet",
 *                          StringCodec::new, 10d, 90d))
 *          .map(sv -> (int) sv.getScore() + ":" + sv.getValue())
 *          .writeTo(sink);
 * }</pre>
 *
 * @param name    name of the source being created
 * @param uri     URI of the Redis server
 * @param codecFn supplier of {@link RedisCodec} instances, used in turn for
 *                serializing/deserializing keys and values
 * @param key     identifier of the Redis Sorted Set
 * @param from    start of the score range we are interested in (INCLUSIVE)
 * @param to      end of the score range we are interested in (INCLUSIVE)
 * @param <K>     type of the sorted set identifier
 * @param <V>     type of the values stored in the sorted set
 * @return source to use in {@link com.hazelcast.jet.pipeline.Pipeline#readFrom}
 */
@Nonnull
public static <K, V> BatchSource<ScoredValue<V>> sortedSet(
        @Nonnull String name,
        @Nonnull RedisURI uri,
        @Nonnull SupplierEx<RedisCodec<K, V>> codecFn,
        @Nonnull K key,
        long from,
        long to
) {
    Objects.requireNonNull(name, "name");
    Objects.requireNonNull(uri, "uri");
    Objects.requireNonNull(codecFn, "codecFn");
    Objects.requireNonNull(key, "key");

    return SourceBuilder.batch(name, ctx -> new SortedSetContext<>(uri, codecFn.get(), key, from, to))
            .<ScoredValue<V>>fillBufferFn(SortedSetContext::fillBuffer)
            .destroyFn(SortedSetContext::close)
            .build();
}
 
Example #12
Source File: ConnectionProducerClusterImpl.java    From sherlock with GNU General Public License v3.0 4 votes vote down vote up
@Override
public <K> RedisConnection<K> produce(RedisCodec<K, K> codec) {
    return new RedisConnectionClusterImpl<>(Client.get().getRedisClusterClient().connect(codec));
}
 
Example #13
Source File: ConnectionProducerImpl.java    From sherlock with GNU General Public License v3.0 4 votes vote down vote up
@Override
public <K> RedisConnection<K> produce(RedisCodec<K, K> codec) {
    return new RedisConnectionImpl<>(Client.get().getRedisClient().connect(codec));
}
 
Example #14
Source File: LettuceSessionClient.java    From redis-session-manager with Apache License 2.0 4 votes vote down vote up
public LettuceSessionClient(GenericObjectPool<StatefulRedisConnection<String, Object>> pool, RedisCodec<String, Object> codec) {
    this.pool = pool;
    this.codec = codec;
}
 
Example #15
Source File: RedisSinks.java    From hazelcast-jet-contrib with Apache License 2.0 4 votes vote down vote up
HashContext(RedisURI uri, K hash, RedisCodec<K, V> codec, FunctionEx<T, K> keyFn,
            FunctionEx<T, V> valueFn) {
    this.client = RedisClient.create(uri);
    this.connection = client.connect(codec);
    this.commands = connection.async();
    this.hash = hash;
    this.keyFn = keyFn;
    this.valueFn = valueFn;
}
 
Example #16
Source File: RedisSinks.java    From hazelcast-jet-contrib with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a {@link Sink} which pushes data items into a specified Redis
 * Sorted Set.
 * <p>
 * Here is an example which reads out trades from a source and writes them
 * to a Redis Stream.
 * <pre>{@code
 * RedisURI uri = RedisURI.create("redis://localhost/");
 * Pipeline.create()
 *      .readFrom(source)
 *      .map(trade -> ScoredValue.fromNullable(trade.timestamp, trade))
 *      .writeTo(RedisSinks.sortedSet("sink", uri, "sortedSet",
 *                      StringCodec::new,
 *                      ScoredValue::getScore, ScoredValue::getValue));
 * }</pre>
 *
 * @param <K>     type of sorted set identifier
 * @param <V>     type of stored values
 * @param <T>     type of data items coming into the sink
 * @param name    name of the source being created
 * @param uri     URI of the Redis server
 * @param set     identifier of the Redis Sorted Set being used
 * @param codecFn supplier of {@link RedisCodec} instances, used in turn for
 *                serializing/deserializing keys and values
 * @param scoreFn function that specifies how to extract the scores from
 *                incoming data items
 * @param valueFn function that specifies how to extract stored values from
 *                incoming data items
 * @return sink to use in {@link com.hazelcast.jet.pipeline.Pipeline#writeTo}
 */
@Nonnull
public static <K, V, T> Sink<T> sortedSet(
        @Nonnull String name,
        @Nonnull RedisURI uri,
        @Nonnull K set,
        @Nonnull SupplierEx<RedisCodec<K, V>> codecFn,
        @Nonnull FunctionEx<T, Double> scoreFn,
        @Nonnull FunctionEx<T, V> valueFn
) {
    Objects.requireNonNull(name, "name");
    Objects.requireNonNull(uri, "uri");
    Objects.requireNonNull(codecFn, "codecFn");
    Objects.requireNonNull(set, "set");
    Objects.requireNonNull(scoreFn, "scoreFn");
    Objects.requireNonNull(valueFn, "valueFn");

    Util.checkSerializable(codecFn, "codecFn");
    Util.checkSerializable(scoreFn, "scoreFn");
    Util.checkSerializable(valueFn, "valueFn");

    return SinkBuilder.sinkBuilder(name, ctx -> new SortedSetContext<>(uri, set, codecFn.get(), scoreFn, valueFn))
            .<T>receiveFn(SortedSetContext::store)
            .flushFn(SortedSetContext::flush)
            .destroyFn(SortedSetContext::destroy)
            .build();
}
 
Example #17
Source File: ConnectionProducer.java    From sherlock with GNU General Public License v3.0 2 votes vote down vote up
/**
 * @param codec Redis codec to use
 * @param <K> codec primary type
 * @return a redis connection
 */
<K> RedisConnection<K> produce(RedisCodec<K, K> codec);