You can use Tair(Redis OSS-compatible) to build an application for correlation analysis on e-commerce store items.
Background information
The correlation between items refers to the scenario where multiple items are added to the shopping cart together. The analysis results are crucial for the e-commerce industry and can be used to analyze shopping behaviors. Example:
On the details page of a specific item, recommend related items to the user who is browsing this page.
Recommend related items to a user who just added an item to the shopping cart.
Display highly correlated items together.
You can use Tair to create a sorted set for each item. For a specific item, the set consists of items that are added along with this item to the shopping cart. Members of the set are scored based on how often they appear in the same cart with that specific item. Each time Item A and Item B appear in the same shopping cart, the respective sorted sets for Item A and Item B in Tair are updated.
Sample code
In this example, Jedis 4.2.3 is used. You can add the following dependencies to the pom.xml file:
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.2.3</version>
</dependency>
</dependencies>
Sample code:
import java.util.List;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.resps.Tuple;
public class AliyunShoppingMall {
public static void main(String[] args)
{
// Specify the connection information of the ApsaraDB for Redis instance. This information can be obtained from the console.
String host = "r-uf66rt5q0vsstw****.tairpena.rds.aliyuncs.com";
int port = 6379;
Jedis jedis = new Jedis(host, port);
try {
// Specify the password used to connect to the ApsaraDB for Redis instance.
String authString = jedis.auth("password");//password
if (!authString.equals("OK"))
{
System.err.println("AUTH Failed: " + authString);
return;
}
// List the products.
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: Beef Jerky";
String key5="Alibaba Cloud: Product: Chicken Wings";
final String[] aliyunProducts=new String[]{key0,key1,key2,key3,key4,key5};
// Initialize settings to clear the possible existing data.
for (int i = 0; i < aliyunProducts.length; i++) {
jedis.del(aliyunProducts[i]);
}
// Simulate shopping behaviors.
for (int i = 0; i < 5; i++) {// Simulate the shopping behaviors of multiple users.
customersShopping(aliyunProducts,i,jedis);
}
System.out.println();
// Uses ApsaraDB for Redis to generate the correlated relationship between items.
for (int i = 0; i < aliyunProducts.length; i++) {
System.out.println(">>>>>>>>>>"+aliyunProducts[i]+"was purchased together with <<<<<<<<<<<<<<<");
List<Tuple> relatedList = jedis.zrevrangeWithScores(aliyunProducts[i], 0, -1);
for (Tuple item : relatedList) {
System.out.println("Item name:"+item.getElement()+", Number of times it has been purchased together with other products:"+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) {
// Simulate three simple shopping behaviors and randomly select one as the behavior of the user.
int bought=(int)(Math.random()*3);
if(bought==1){
// Simulate business logic: The user has purchased the following products:
System.out.println("User"+i+"purchased"+products[0]+","+products[2]+","+products[1]);
// Store the correlations between the products in sorted sets of ApsaraDB for Redis.
jedis.zincrby(products[0], 1, products[1]);
jedis.zincrby(products[0], 1, products[2]);
jedis.zincrby(products[1], 1, products[0]);
jedis.zincrby(products[1], 1, products[2]);
jedis.zincrby(products[2], 1, products[0]);
jedis.zincrby(products[2], 1, products[1]);
}else if(bought==2){
// Simulate business logic: The user has purchased the following products:
System.out.println("User"+i+"purchased"+products[4]+","+products[2]+","+products[3]);
// Store the correlations between the products in sorted sets of ApsaraDB for Redis.
jedis.zincrby(products[4], 1, products[2]);
jedis.zincrby(products[4], 1, products[3]);
jedis.zincrby(products[3], 1, products[4]);
jedis.zincrby(products[3], 1, products[2]);
jedis.zincrby(products[2], 1, products[4]);
jedis.zincrby(products[2], 1, products[3]);
}else if(bought==0){
// Simulate business logic: The user has purchased the following products:
System.out.println("User"+i+"purchased"+products[1]+","+products[5]);
// Store the correlations between the products in sorted sets of ApsaraDB for Redis.
jedis.zincrby(products[5], 1, products[1]);
jedis.zincrby(products[1], 1, products[5]);
}
}
}
After you enter the correct endpoint and password to connect to the Tair instance and run the preceding Java code, the following output is displayed:
User 0 purchased Alibaba Cloud: Product: Chocolate, Alibaba Cloud: Product: Chicken Wings
User 1 purchased Alibaba Cloud: Product: Beef Jerky, Alibaba Cloud: Product: Cola, Alibaba Cloud: Product: Gum
User 2 purchased Alibaba Cloud: Product: Beer, Alibaba Cloud: Product: Cola, Alibaba Cloud: product: Chocolate
User 3 purchased Alibaba Cloud: Product: Beef Jerky, Alibaba Cloud: Product: Cola, Alibaba Cloud: Product: Gum
User 4 purchased Alibaba Cloud: Product: Chocolate, Alibaba Cloud: Product: Chicken Wings
>>>>>>>>>>Alibaba Cloud: Product: Beer was purchased together with<<<<<<<<<<<<<<<
Item name: Alibaba Cloud: Product: Chocolate, Number of times it has been purchased together with other products: 1
Item name: Alibaba Cloud: Product: Cola, Number of times it has been purchased together with other products: 1
>>>>>>>>>>Alibaba Cloud: Product: Chocolate was purchased together with<<<<<<<<<<<<<<<<<<<<
Item name: Alibaba Cloud: Product: Chicken Wings, Number of times it has been purchased together with other products: 2
Item name: Alibaba Cloud: Product: Beer, Number of times it has been purchased together with other products: 1
Item name: Alibaba Cloud: Product: Cola, Number of times it has been purchased together with other products: 1
>>>>>>>>>>Alibaba Cloud: Product: Cola was purchased together with<<<<<<<<<<<<<<<<<
Item name: Alibaba Cloud: Product: Beef Jerky, Number of times it has been purchased together with other products: 2
Item name: Alibaba Cloud: Product: Gum, Number of times it has been purchased together with other products: 2
Item name: Alibaba Cloud: Product: Chocolate, Number of times it has been purchased together with other products: 1
Item name: Alibaba Cloud: Product: Beer, Number of times it has been purchased together with other products: 1
>>>>>>>>>>Alibaba Cloud: Product: Gum was purchased together with<<<<<<<<<<<<<<<<<<<<
Item name: Alibaba Cloud: Product: Beef Jerky, Number of times it has been purchased together with other products: 2
Item name: Alibaba Cloud: Product: Cola, Number of times it has been purchased together with other products: 2
>>>>>>>>>>Alibaba Cloud: Product: Beef Jerky was purchased together with<<<<<<<<<<<<<<<
Item name: Alibaba Cloud: Product: Cola, Number of times it has been purchased together with other products: 2
Item name: Alibaba Cloud: Product: Gum, Number of times it has been purchased together with other products: 2
>>>>>>>>>>Alibaba Cloud: Product: Chicken Wings was purchased together with<<<<<<<<<<<<<<
Item name: Alibaba Cloud: Product: Chocolate, Number of times it has been purchased together with other products: 2