All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.redisson.mapreduce.MapperTask Maven / Gradle / Ivy

There is a newer version: 3.36.0
Show newest version
/**
 * Copyright (c) 2013-2021 Nikita Koksharov
 *
 * Licensed 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.redisson.mapreduce;

import java.util.Map.Entry;

import org.redisson.api.RMap;
import org.redisson.api.RMapCache;
import org.redisson.api.mapreduce.RCollector;
import org.redisson.api.mapreduce.RMapper;
import org.redisson.client.codec.Codec;
import org.redisson.misc.Injector;

/**
 * 
 * @author Nikita Koksharov
 *
 * @param  input key type
 * @param  input key type
 * @param  output key type
 * @param  output key type
 */
public class MapperTask extends BaseMapperTask {

    private static final long serialVersionUID = 2441161019495880394L;
    
    protected RMapper mapper;
    
    public MapperTask() {
    }
    
    public MapperTask(RMapper mapper, Class objectClass, Class objectCodecClass) {
        super(objectClass, objectCodecClass);
        this.mapper = mapper;
    }

    @Override
    public void run() {
        Codec codec;
        try {
            codec = (Codec) objectCodecClass.getConstructor().newInstance();
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
        
        Injector.inject(mapper, redisson);
        RCollector collector = new Collector(codec, redisson, collectorMapName, workersAmount, timeout);

        for (String objectName : objectNames) {
            RMap map = null;
            if (RMapCache.class.isAssignableFrom(objectClass)) {
                map = redisson.getMapCache(objectName, codec);
            } else {
                map = redisson.getMap(objectName, codec);
            }
            
            for (Entry entry : map.entrySet()) {
                if (Thread.currentThread().isInterrupted()) {
                    return;
                }
                
                mapper.map(entry.getKey(), entry.getValue(), collector);
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy