com.github.lontime.shaded.org.redisson.mapreduce.MapperTask Maven / Gradle / Ivy
The 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 com.github.lontime.shaded.org.redisson.mapreduce;
import java.util.Map.Entry;
import com.github.lontime.shaded.org.redisson.api.RMap;
import com.github.lontime.shaded.org.redisson.api.RMapCache;
import com.github.lontime.shaded.org.redisson.api.mapreduce.RCollector;
import com.github.lontime.shaded.org.redisson.api.mapreduce.RMapper;
import com.github.lontime.shaded.org.redisson.client.codec.Codec;
import com.github.lontime.shaded.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);
}
}
}
}