Tair (Redis OSS-compatible) インスタンスを使用して、eコマースストアアイテムの相関分析用のアプリケーションを構築できます。
背景情報
アイテム間の相関は、複数のアイテムが一緒にショッピングカートに追加されるシナリオを指す。 分析結果は、eコマース業界にとって非常に重要であり、ショッピング行動の分析に使用できます。 例:
特定のアイテムの詳細ページで、このページを参照しているユーザーに関連アイテムを推奨します。
ショッピングカートにアイテムを追加したばかりのユーザーに関連アイテムを推奨します。
相関の高いアイテムを一緒に表示します。
アイテムごとにソートされたセットを作成できます。 特定のアイテムのセットは、このアイテムとともにショッピングカートに追加されるアイテムで構成されます。 セットのメンバーは、その特定のアイテムと同じカートに表示される頻度に基づいてスコアリングされます。 アイテムAとアイテムBが同じショッピングカートに現れるたびに、アイテムAとアイテムBのそれぞれのソートされたセットが更新されます。
サンプルコード
この例では、Jedis 4.2.3が使用されます。 pom.xmlファイルに次の依存関係を追加できます。
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.2.3</version>
</dependency>
</dependencies>
サンプルコード:
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 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 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();
// Use Tair (Redis OSS-compatible) 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 the instance.
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.
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 the instance.
jedis.zincrby(products[5], 1, products[1]);
jedis.zincrby(products[1], 1, products[5]);
}
}
}
正しいエンドポイントとパスワードを入力してインスタンスに接続し、前述のJavaコードを実行すると、次の出力が表示されます。
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