I'm trying to cache some data in a storm, but not sure if this is the right way to do this or not. In the underlying class, the employee ID and employer name are cached on the hash map. To do this, a database query was made in the Employee table to select all employees and fill in the hash map in the preparation method (is this the right place to initialize the map?).
After some logging (when using a storm topology), the topology makes several connections to the database and initializes the map several times. Of course, I want to avoid this, so I want to cache the result so that it does not go to the database every time. Please, help?
public class TestBolt extends BaseRichBolt { private static final long serialVersionUID = 2946379346389650348L; private OutputCollector collector; private Map<String, String> employeeIdToNameMap; private static final Logger LOG = Logger.getLogger(TestBolt.class); @Override public void execute(Tuple tuple) { String employeeId = tuple.getStringByField("employeeId"); String employeeName = employeeIdToNameMap.get(employeeId); collector.emit(tuple, new Values(employeeId, employeeName)); collector.ack(tuple); } @Override public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
SOLUTION I created a synchronized card and its work is excellent for me
private static Map<String, String> employeeIdToNameMap = Collections .synchronizedMap(new HashMap<String, String>());
source share