Scenario introduction

ApsaraDB for Redis, functions basically the same as Redis, can be used to create correlation analysis programs for items on e-commerce sites.

The correlation between items is the chances where the items are added to a user’s shopping cart. The analysis result plays an important role in analyzing users’ shopping behavior for the e-commerce industry. For example:

  • To recommend related items to a user who lingers on the details page of a certain item;

  • To recommend related items to a user who just adds an item to the shopping cart;

  • To place highly correlated items together on the shelves.

ApsaraDB for Redis can be used to create a sorted set for each type of item. The members of this set are scored based on how often they appear in the same users’ carts with the primary item. Each time A and B appear in the same user’s shopping cart, the respective sorted sets for items A and B in ApsaraDB for Redis are updated.

Sample Code

package shop.kvstore.aliyun.com;
import java.util.Set;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;
    public class AliyunShoppingMall {
        public static void main(String[] args) 
        
            //Information for connecting ApsaraDB for Redis, which can be obtained from the console
            String host = "xxxxxxxx.m.cnhza.kvstore.aliyuncs.com";
            int port = 6379;
            Jedis jedis = new Jedis(host, port);
            try {
                //ApsaraDB for Redis instance password
                String authString = jedis.auth("password");//password
                if (! authString.equals("OK"))
                
                    System.err.println("AUTH Failed: " + authString);
                    return;
                
                // Product list
                String key0="Alibaba Cloud:product:beer";
                String key1 = "Alibaba  Cloud:product:chocolate ";
                String key2 = "Alibaba Cloud:product:cola ";
                String key3 = "Alibaba Cloud:product:gum ";
                String key4 = "Alibaba Cloud:product:jerky ";
                String key5="Alibaba Cloud:product:chicken wing";
                final String[] aliyunProducts=new String[]{key0,key1,key2,key3,key4,key5};
                // Initialize to clear possible existing old data
                for (int i = 0; i < aliyunProducts.length; i++) {
                    jedis.del(aliyunProducts[i]);
                
                // Simulated user shopping behavior
                 for (int i = 0; i < 5; i++) {//Simulates users' buying items
                    customersShopping(aliyunProducts,i,jedis);
                
                System.out.println();
                // Generates correlation between items using ApsaraDB for Redis
                for (int i = 0; i < aliyunProducts.length; i++) {
                    System.out.println(">>>>>>>>>>与"+aliyunProducts[i]+"一起被购买的产品有<<<<<<<<<<<<<<<");
                    Set<Tuple> relatedList = jedis.zrevrangeWithScores(aliyunProducts[i], 0, -1);
                    for (Tuple item : relatedList) {  
                        System.out.println("Product name:"+item.getElement()+", bought together times:"+Double.valueOf(item.getScore()).intValue());
                      
                    System.out.println();
                
            } Catch (exception e ){
                e.printStackTrace();
            }finally{
                jedis.quit();
                jedis.close();
            
        
        private static void customersShopping(String[] products, int i, Jedis jedis) {
            // Simulation  three possibilities of shopping behavior at random.
            int bought=(int)(Math.random()*3);
            if(bought==1){
                 //Simulates the business logic: The user buys the following items
                 System.out.println("User"+i+"bought"+products[0]+","+products[2]+","+products[1]);
                //Adds the correlations between the items to SortSet in ApsaraDB for Redis
                Jedis. zencrby (products [0], 1, products [1]);
                Jedis. zencrby (products [0], 1, products [2]);
                Jedis. zencrby (products [1], 1, products [0]);
                Jedis. zencrby (products [1], 1, products [2]);
                Jedis. zencrby (products [2], 1, products [0]);
                Jedis. zencrby (products [2], 1, products [1]);
            } Else if (bought = 2 ){
                // Mock business logic: the user has purchased the following products
                System. out. println ("user" + I + "purchased" + products [4] + ", "+ products [2] +", "+ products [3]);
                // Record the association between the products in the sortset of apsaramba for redis
                Jedis. zencrby (products [4], 1, products [2]);
                Jedis. zencrby (products [4], 1, products [3]);
                Jedis. zencrby (products [3], 1, products [4]);
                Jedis. zencrby (products [3], 1, products [2]);
                Jedis. zencrby (products [2], 1, products [4]);
                Jedis. zencrby (products [2], 1, products [3]);
            } Else if (bought = 0 ){
                // Mock business logic: the user has purchased the following products
                System. out. println ("user" + I + "purchased" + products [1] + ", "+ products [5]);
                // Record the association between the products in the sortset of apsaramba for redis
                Jedis. zencrby (products [5], 1, products [1]);
                Jedis. zencrby (products [1], 1, products [5]);
            
        
    

Output

After you access the ApsaraDB for Redis instance with the correct address and password and run the above Java code, the following output is displayed:

User 0 buy Alibaba Cloud: product: Chocolate, Alibaba Cloud: product: Chicken Wings
User 1 bought Ali cloud: Product: beef jerky, Alibaba Cloud: Product: Cola, Alibaba Cloud: product: gum
User 2 purchased Ali cloud: Product: Beer, Alibaba Cloud: Product: Cola, Alibaba Cloud: product: chocolate
User 3 bought Ali cloud: Products: beef jerky, Alibaba Cloud: Products: Cola, Alibaba Cloud: products: gum
User 4 purchase Ali cloud: Product: Chocolate, Alibaba Cloud: product: Chicken Wings
> And Ali cloud: products: the products that were purchased together were <<
Product Name: Alibaba Cloud: product: Chocolate, co-purchase
Product Name: Alibaba Cloud: Product: Cola, co-purchase
> And Ali cloud: products: chocolate products purchased together include <<
Product: chicken wings, common purchases: 2 product name: Ali Yun
Product Name: Alibaba Cloud: product: Beer, co-purchase
Product Name: Alibaba Cloud: Product: Cola, co-purchase
>>>>>>>>>>与阿里云:产品:可乐一起被购买的产品有<<<<<<<<<<<<<<<
商品名称:阿里云:产品:牛肉干, 共同购买次数:2
商品名称:阿里云:产品:口香糖, 共同购买次数:2
商品名称:阿里云:产品:巧克力, 共同购买次数:1
商品名称:阿里云:产品:啤酒, 共同购买次数:1
>>>>>>>>>>与阿里云:产品:口香糖一起被购买的产品有<<<<<<<<<<<<<<<
商品名称:阿里云:产品:牛肉干, 共同购买次数:2
商品名称:阿里云:产品:可乐, 共同购买次数:2
>>>>>>>>>>与阿里云:产品:牛肉干一起被购买的产品有<<<<<<<<<<<<<<<
商品名称:阿里云:产品:可乐, 共同购买次数:2
商品名称:阿里云:产品:口香糖, 共同购买次数:2
>>>>>>>>>>与阿里云:产品:鸡翅一起被购买的产品有<<<<<<<<<<<<<<<
商品名称:阿里云:产品:巧克力, 共同购买次数:2