concept purchase in category kafka

appears as: purchases, purchase, purchases, purchase
Kafka Streams in Action: Real-time apps and microservices with the Kafka Streams API

This is an excerpt from Manning's book Kafka Streams in Action: Real-time apps and microservices with the Kafka Streams API.

The next child node in the process is the rewards accumulator (figure 1.8). ZMart has a customer rewards program that gives customers points for purchases made in the store. This node’s role is to extract the dollar amount spent and the client’s ID and create a new object containing those two fields.

Figure 1.8. The rewards node is responsible for consuming sales records from the masking node and converting them into records containing the total of the purchase and the customer ID.

The third processor in the topology is the customer rewards accumulator node shown in figure 3.8, which will let ZMart track purchases made by members of their preferred customer club. The rewards accumulator sends data to a topic consumed by applications at ZMart HQ to determine rewards when customers complete purchases.

The security department at ZMart has approached you. Apparently, in one of the stores, there’s a suspicion of fraud. There have been reports that a store manager is entering invalid discount codes for purchases. Security isn’t sure what’s going on, but they’re asking for your help.

Figure 3.15. To write purchases involving a given employee outside of the Kafka Streams application, you’ll first add a filter processor to extract purchases by employee ID, and then you’ll use a foreach operator to write each record to an external relational database.
Listing 4.12. ValueJoiner implementation
public class PurchaseJoiner
 implements ValueJoiner<Purchase, Purchase, CorrelatedPurchase> {

    @Override
    public CorrelatedPurchase apply(Purchase purchase, Purchase purchase2) {
        CorrelatedPurchase.Builder builder =
 CorrelatedPurchase.newBuilder();                                       #1

        Date purchaseDate =
 purchase != null ? purchase.getPurchaseDate() : null;

        Double price = purchase != null ? purchase.getPrice() : 0.0;
        String itemPurchased =
 purchase != null ? purchase.getItemPurchased() : null;                 #2

        Date otherPurchaseDate =
 otherPurchase != null ? otherPurchase.getPurchaseDate() : null;

        Double otherPrice =
 otherPurchase != null ? otherPurchase.getPrice() : 0.0;

        String otherItemPurchased =
 otherPurchase != null ? otherPurchase.getItemPurchased() : null;       #3

        List<String> purchasedItems = new ArrayList<>();

        if (itemPurchased != null) {
            purchasedItems.add(itemPurchased);
        }

        if (otherItemPurchased != null) {
            purchasedItems.add(otherItemPurchased);
        }

        String customerId =
 purchase != null ? purchase.getCustomerId() : null;

        String otherCustomerId =
 otherPurchase != null ? otherPurchase.getCustomerId() : null;

        builder.withCustomerId(customerId != null ? customerId : otherCustome
     rId)
                .withFirstPurchaseDate(purchaseDate)
                .withSecondPurchaseDate(otherPurchaseDate)
                .withItemsPurchased(purchasedItems)
                .withTotalAmount(price + otherPrice);

        return builder.build();                                           #4
    }
}