E-commerce store item correlation analysis

Last Updated: Nov 09, 2017

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 items 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

  1. package shop.kvstore.aliyun.com;
  2. import java.util.Set;
  3. import redis.clients.jedis.Jedis;
  4. import redis.clients.jedis.Tuple;
  5. public class AliyunShoppingMall {
  6. public static void main(String[] args)
  7. {
  8. //Information for connecting ApsaraDB for Redis, which can be obtained from the console
  9. String host = "xxxxxxxx.m.cnhza.kvstore.aliyuncs.com";
  10. int port = 6379;
  11. Jedis jedis = new Jedis(host, port);
  12. try {
  13. //ApsaraDB for Redis instance password
  14. String authString = jedis.auth("password");//password
  15. if (!authString.equals("OK"))
  16. {
  17. System.err.println("AUTH Failed: " + authString);
  18. return;
  19. }
  20. //Product list
  21. String key0="Alibaba Cloud:product:beer";
  22. String key1="Alibaba Cloud:product:chocolate";
  23. String key2="Alibaba Cloud:product:cola";
  24. String key3="Alibaba Cloud:product:gum";
  25. String key4="Alibaba Cloud:product:jerky";
  26. String key5="Alibaba Cloud:product:chicken wing";
  27. final String[] aliyunProducts=new String[]{key0,key1,key2,key3,key4,key5};
  28. //Initialization, clearing any existing data
  29. for (int i = 0; i < aliyunProducts.length; i++) {
  30. jedis.del(aliyunProducts[i]);
  31. }
  32. //Simulates user shopping behavior
  33. for (int i = 0; i < 5; i++) {//Simulates users' buying items
  34. customersShopping(aliyunProducts,i,jedis);
  35. }
  36. System.out.println();
  37. //Generates correlation between items using ApsaraDB for Redis
  38. for (int i = 0; i < aliyunProducts.length; i++) {
  39. System.out.println(">>>>>>>>>>and"+aliyunProducts[i]+"products bought together<<<<<<<<<<<<<<<");
  40. Set<Tuple> relatedList = jedis.zrevrangeWithScores(aliyunProducts[i], 0, -1);
  41. for (Tuple item : relatedList) {
  42. System.out.println("Product name:"+item.getElement()+", bought together times:"+Double.valueOf(item.getScore()).intValue());
  43. }
  44. System.out.println();
  45. }
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. }finally{
  49. jedis.quit();
  50. jedis.close();
  51. }
  52. }
  53. private static void customersShopping(String[] products, int i, Jedis jedis) {
  54. //Simulates three possibilities of shopping behavior at random
  55. int bought=(int)(Math.random()*3);
  56. if(bought==1){
  57. //Simulates the business logic: The user buys the following items
  58. System.out.println("User"+i+"bought"+products[0]+","+products[2]+","+products[1]);
  59. //Adds the correlations between the items to SortSet in ApsaraDB for Redis
  60. jedis.zincrby(products[0], 1, products[1]);
  61. jedis.zincrby(products[0], 1, products[2]);
  62. jedis.zincrby(products[1], 1, products[0]);
  63. jedis.zincrby(products[1], 1, products[2]);
  64. jedis.zincrby(products[2], 1, products[0]);
  65. jedis.zincrby(products[2], 1, products[1]);
  66. }else if(bought==2){
  67. //Simulates the business logic: The user buys the following items
  68. System.out.println("User"+i+"bought"+products[4]+","+products[2]+","+products[3]);
  69. //Adds the correlations between the items to SortSet in ApsaraDB for Redis
  70. jedis.zincrby(products[4], 1, products[2]);
  71. jedis.zincrby(products[4], 1, products[3]);
  72. jedis.zincrby(products[3], 1, products[4]);
  73. jedis.zincrby(products[3], 1, products[2]);
  74. jedis.zincrby(products[2], 1, products[4]);
  75. jedis.zincrby(products[2], 1, products[3]);
  76. }else if(bought==0){
  77. //Simulates the business logic: The user buys the following items
  78. System.out.println("User"+i+"bought"+products[1]+","+products[5]);
  79. //Adds the correlations between the items to SortSet in ApsaraDB for Redis
  80. jedis.zincrby(products[5], 1, products[1]);
  81. jedis.zincrby(products[1], 1, products[5]);
  82. }
  83. }
  84. }

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:

  1. User 0 bought Alibaba Cloud:Product:Chocolate, Alibaba Cloud:Product:Chicken wings
  2. User 1 bought Alibaba Cloud:Product:Beef jerky, Alibaba Cloud:Product:Cola, Alibaba Cloud:Product:Chewing gum
  3. User 2 bought Alibaba Cloud:Product:Beer, Alibaba Cloud:Product:Cola, Alibaba Cloud:Product:Chocolate
  4. User 3 bought Alibaba Cloud:Product:Beef jerky, Alibaba Cloud:Product:Cola, Alibaba Cloud:Product:Chewing gum
  5. User 4 bought Alibaba Cloud:Product:Chocolate, Alibaba Cloud:Product:Chicken wings
  6. >>>>>>>>>>Items bought with Alibaba Cloud:Product:Beer<<<<<<<<<<<<<<<
  7. Item name: Alibaba Cloud:Product:Chocolate, Times: 1
  8. Item name: Alibaba Cloud:Product:Cola, Times: 1
  9. >>>>>>>>>>Products bought with Alibaba Cloud:Product:Chocolate<<<<<<<<<<<<<<<
  10. Item name: Alibaba Cloud:Product:Chicken wings, Times: 2
  11. Item name: Alibaba Cloud:Product:Beer, Times: 1
  12. Item name: Alibaba Cloud:Product:Cola, Times: 1
  13. >>>>>>>>>>Products bought with Alibaba Cloud:Product:Cola<<<<<<<<<<<<<<<
  14. Item name: Alibaba Cloud:Product:Beef jerky, Times: 2
  15. Item name: Alibaba Cloud:Product:Chewing gum, Times: 2
  16. Item name: Alibaba Cloud:Product:Chocolate, Times: 1
  17. Item name: Alibaba Cloud:Product:Beer, Times: 1
  18. >>>>>>>>>>Products bought with Alibaba Cloud:Product:Chewing gum<<<<<<<<<<<<<<<
  19. Item name: Alibaba Cloud:Product:Beef jerky, Times: 2
  20. Item name: Alibaba Cloud:Product:Cola, Times: 2
  21. >>>>>>>>>>Products bought with Alibaba Cloud:Product:Beef jerky<<<<<<<<<<<<<<<
  22. Item name: Alibaba Cloud:Product:Cola, Times: 2
  23. Item name: Alibaba Cloud:Product:Chewing gum, Times: 2
  24. >>>>>>>>>>Products bought with Alibaba Cloud:Product:Chicken wings<<<<<<<<<<<<<<<
  25. Item name: Alibaba Cloud:Product:Chocolate, Times: 2
Thank you! We've received your feedback.