// batch job (possibly real-time using streaming APIs) public static void createProductAssociativityGraphPerCategory() { Set<String> customers = purchasesCache.keySet(); customers.forEach( customer -> { List<String> customerPurchases = Lists.newArrayList(purchasesCache.get(customer)); for (int i = 0; i < customerPurchases.size(); i++) { for (int j = i + 1; j < customerPurchases.size(); j++) { Product product1 = productCache.get(customerPurchases.get(i)); Product product2 = productCache.get(customerPurchases.get(j)); if (product1.category.equals(product2.category)) { ProductAssociativityGraph graph = productAssociativityGraphMap.getOrDefault( product1.category, ProductAssociativityGraph.create()); graph.addAssociation(Vertex.create(product1.id), Vertex.create(product2.id), 1); productAssociativityGraphMap.putIfAbsent(product1.category, graph); } } } }); }