我用弹簧的火花流。我有一个存储库实现,我维护了一个静态缓存。此缓存填充在类的afterPropertiesSet()方法中。现在,当我尝试从驱动程序节点(即在rdd.forEach()中)访问此缓存时,我将获得填充的缓存。但是当我尝试从工作节点(即映射阶段)访问此缓存时,我得到一个空缓存。
那么,在将类实例发送到工作节点之后调用afterPropertiesSet()方法吗?如果是这样,那么我应该如何填充此缓存以便我也可以在工作节点中获取它?
我提供了以下代码,以便了解我所做的事情 -
这是存储库实现类:
@Service("hbaseRepository")
@Data
@Scope(SCOPE_SINGLETON)
public class HbaseRepositoryImpl implements HbaseRepository, InitializingBean {
private static Map<Class<?>, HBaseEntityMetaInfo> cache = new ConcurrentHashMap<>();
@Autowired
private IoTStreamProps props;
private static Tuple4<String, String, String, String> serializableProps;
@Autowired
private EntityAnnotationProcessor<HBaseEntityMetaInfo> annotationProcessor;
@Override
public void afterPropertiesSet() throws Exception {
Log.info("hbase repository meta cache is going populate");
EntityScan scan = Optional.ofNullable(HbaseConfig.class.getAnnotation(EntityScan.class)).orElseThrow(() -> new IoTException("EntityScan or packages attribute is missing"));
cache.putAll(annotationProcessor.process(scan.basePackage()));
serializableProps = new Tuple4<String, String, String, String>(props.getHbaseMasterUrl(), props.getZookeeperQuorum(), props.getHbaseZkPort(), props.getHbaseTableSanityCheck());
}
public static Map<Class<?>, HBaseEntityMetaInfo> getCache() {
return cache;
}
}
因此,当我从驱动程序节点调用HbaseRepositoryImpl.getCache()时,我将获得填充的缓存,但如果从工作节点调用相同的(在映射阶段),我将获得一个空缓存。