[ 
https://issues.apache.org/jira/browse/NIFI-4061?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16054821#comment-16054821
 ] 

ASF GitHub Bot commented on NIFI-4061:
--------------------------------------

Github user bbende commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/1918#discussion_r122836798
  
    --- Diff: 
nifi-nar-bundles/nifi-redis-bundle/nifi-redis-extensions/src/main/java/org/apache/nifi/redis/state/RedisStateMapJsonSerDe.java
 ---
    @@ -0,0 +1,103 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.nifi.redis.state;
    +
    +import com.fasterxml.jackson.core.JsonFactory;
    +import com.fasterxml.jackson.core.JsonGenerator;
    +import com.fasterxml.jackson.core.JsonParser;
    +import com.fasterxml.jackson.core.JsonToken;
    +
    +import java.io.ByteArrayOutputStream;
    +import java.io.IOException;
    +import java.util.Map;
    +
    +/**
    + * A RedisStateMapSerDe that uses JSON as the underlying representation.
    + */
    +public class RedisStateMapJsonSerDe implements RedisStateMapSerDe {
    +
    +    public static final String FIELD_VERSION = "version";
    +    public static final String FIELD_ENCODING = "encodingVersion";
    +    public static final String FIELD_STATE_VALUES = "stateValues";
    +
    +    private final JsonFactory jsonFactory = new JsonFactory();
    +
    +    @Override
    +    public byte[] serialize(final RedisStateMap stateMap) throws 
IOException {
    +        if (stateMap == null) {
    +            return null;
    +        }
    +
    +        try (final ByteArrayOutputStream out = new 
ByteArrayOutputStream()) {
    +            final JsonGenerator jsonGenerator = 
jsonFactory.createGenerator(out);
    +            jsonGenerator.writeStartObject();
    +            jsonGenerator.writeNumberField(FIELD_VERSION, 
stateMap.getVersion());
    +            jsonGenerator.writeNumberField(FIELD_ENCODING, 
stateMap.getEncodingVersion());
    +
    +            jsonGenerator.writeObjectFieldStart(FIELD_STATE_VALUES);
    +            for (Map.Entry<String,String> entry : 
stateMap.toMap().entrySet()) {
    +                jsonGenerator.writeStringField(entry.getKey(), 
entry.getValue());
    +            }
    +            jsonGenerator.writeEndObject();
    +
    +            jsonGenerator.writeEndObject();
    +            jsonGenerator.flush();
    +
    +            return out.toByteArray();
    +        }
    +    }
    +
    +    @Override
    +    public RedisStateMap deserialize(final byte[] data) throws IOException 
{
    +        if (data == null || data.length == 0) {
    +            return null;
    +        }
    +
    +        final RedisStateMap.Builder builder = new RedisStateMap.Builder();
    +
    +        try (final JsonParser jsonParser = jsonFactory.createParser(data)) 
{
    +            while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
    --- End diff --
    
    I knew that code should have been simpler :) updated it to use the 
readValueAsTree


> Implement a StateProvider backed by Redis
> -----------------------------------------
>
>                 Key: NIFI-4061
>                 URL: https://issues.apache.org/jira/browse/NIFI-4061
>             Project: Apache NiFi
>          Issue Type: Improvement
>            Reporter: Bryan Bende
>            Assignee: Bryan Bende
>            Priority: Minor
>
> We currently have only one clustered state provider which is a ZooKeeper 
> implementation. Redis would make a good candidate to provide another option.



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to