天天看點

如何使用GraphDB做商品實時推薦購買graphdb服務構造圖示資料模型資料模組化建立schema準備頂點&邊資料集,csv格式hgraph-loader執行導入使用gremlin-console做圖周遊執行

幾乎所有的企業都需要了解如何快速并且高效地影響客戶來購買他們的産品并且推薦其他相關商品給他們。這可能需要用到雲服務的推薦,個性化,網絡分析工具。圖非常适合這些類似的分析用例,如推薦産品,或基于使用者資料,過去行為,推薦個性化廣告。下面我們來看看怎麼使用圖做個性化推薦

購買graphdb服務

  • 登入www.aliyun.com 後進入 hbase産品控制台 ,選擇建立hbase叢集
    如何使用GraphDB做商品實時推薦購買graphdb服務構造圖示資料模型資料模組化建立schema準備頂點&邊資料集,csv格式hgraph-loader執行導入使用gremlin-console做圖周遊執行
  • 選擇Graphdb(圖)子産品,之後分别勾選付費方式,地域,可用區,網絡類型,vpc。跟選購其他産品一緻
    如何使用GraphDB做商品實時推薦購買graphdb服務構造圖示資料模型資料模組化建立schema準備頂點&邊資料集,csv格式hgraph-loader執行導入使用gremlin-console做圖周遊執行
  • 接來下選擇master及core的規則,購買core個數及磁盤容量,參考自身實際需求,這裡筆者這裡選擇4核8G,之後選擇立即購買
    如何使用GraphDB做商品實時推薦購買graphdb服務構造圖示資料模型資料模組化建立schema準備頂點&邊資料集,csv格式hgraph-loader執行導入使用gremlin-console做圖周遊執行
  • 之後同意協定完成支付,回到hbase控制台,目前狀态初始化,等待叢集建立完畢
    如何使用GraphDB做商品實時推薦購買graphdb服務構造圖示資料模型資料模組化建立schema準備頂點&邊資料集,csv格式hgraph-loader執行導入使用gremlin-console做圖周遊執行
  • 叢集建立完畢後,可以拿到如下圖庫位址,記住這個位址,替換下面指令行中的$host變量
    如何使用GraphDB做商品實時推薦購買graphdb服務構造圖示資料模型資料模組化建立schema準備頂點&邊資料集,csv格式hgraph-loader執行導入使用gremlin-console做圖周遊執行

如果需要使用graph-loader工具批量導入資料,請聯系釘釘

雲hbase答疑

我們幫您開通hbase服務端口

  • 修改通路控制,把用戶端ecs加入通路白名單
    如何使用GraphDB做商品實時推薦購買graphdb服務構造圖示資料模型資料模組化建立schema準備頂點&邊資料集,csv格式hgraph-loader執行導入使用gremlin-console做圖周遊執行
如何使用GraphDB做商品實時推薦購買graphdb服務構造圖示資料模型資料模組化建立schema準備頂點&邊資料集,csv格式hgraph-loader執行導入使用gremlin-console做圖周遊執行
  • 在用戶端ecs執行如下指令,有消息體傳回,說明用戶端可以正常通路graphdb了。

    如果出現異常先嘗試telnet $host 8180,如果不通則說明需要添加這台ECS IP至通路白名單

curl -XPOST -d '{"gremlin": "1+1" }'  http://$host:8180
{"requestId":"9db39735-9b69-4425-b58c-2b8c230d2fb9","status":{"message":"","code":200,"attributes":{}},"result":{"data":[2],"meta":{}}}           

接下來您可以使用練習下

graphdb快速入門

構造圖示資料模型

使用者在電商網站會産生訂單曆史記錄,訂單會包含一堆商品,讓我們構造一個如下圖資料模型

如何使用GraphDB做商品實時推薦購買graphdb服務構造圖示資料模型資料模組化建立schema準備頂點&邊資料集,csv格式hgraph-loader執行導入使用gremlin-console做圖周遊執行

資料模組化

我們重點考慮下面三類頂點,顧客customer, 訂單order, 産品product

  • 顧客下訂單行為用 ordered邊分别關聯customer,order頂點
  • 訂單會包含多個商品,定義contains邊,分别連接配接order頂點及product頂點。

    表示一個訂單會包含多個商品。

豐富頂點及邊的屬性

作為示例,我們假定

  • 顧客隻有customerid, name
  • order頂點隻要orderid, ordertime
  • product頂點隻有productid, name。
  • ordered/contains邊暫時不需要額外屬性字段,

建立schema

我們使用hgraphdb-loader導入圖schema及資料,hgraphdb-loader下載下傳位址:

http://public-hbase.oss-cn-hangzhou.aliyuncs.com/installpackage/hgraphdb-loader.tar.gz

建立schema.json檔案,編寫如下schema

{
  "vertexLabels" : [ {
    "name" : "customer",
    "properties" : [ {
      "name" : "id",
      "type" : "String"
    }, {
      "name" : "name",
      "type" : "String"
    } ],
    "indexes" : [ {
      "propertykey" : "name",
      "unique" : false
    } ]
  }, {
    "name" : "order",
    "properties" : [ {
      "name" : "id",
      "type" : "String"
    }, {
      "name" : "ordertime",
      "type" : "Date"
    } ],
    "indexes" : [ ]
  }, {
    "name" : "product",
    "properties" : [ {
      "name" : "id",
      "type" : "String"
    }, {
      "name" : "name",
      "type" : "String"
    } ],
    "indexes" : [ ]
  } ],
  "edgeLabels" : [ {
    "name" : "ordered",
    "properties" : [ ],
    "indexes" : [ ],
    "connections" : [ {
      "outV" : "customer",
      "inV" : "order"
    } ]
  }, {
    "name" : "contains",
    "properties" : [ ],
    "indexes" : [ ],
    "connections" : [ {
      "outV" : "order",
      "inV" : "product"
    } ]
  } ]
}
           

準備頂點&邊資料集,csv格式

所有的頂點都放到一個vertex.csv檔案中

1,customer,oAECseuFIx
2,customer,UpOqBuMQSG
3,customer,WTlnfKULti
10001,order,2015-09-06
10002,order,1998-06-08
110001,product,ZDoVtEBlDq
110002,product,GXsssxOJSq

...           

所有的邊都放到一個edge.csv檔案中

9610,58824,ordered
2069,12200,ordered
85589,113864,contains
50591,110945,contains           

我們直接準備了一些測試資料,

點選此處

下載下傳

hgraph-loader執行導入

點選

工具

執行解壓,進入目錄,執行如下導入指令。

sh run.sh import emr-header-1,emr-header-2,emr-header-3 demo/schema.json demo/vertex.csv demo/edge.csv           

使用gremlin-console做圖周遊

下載下傳gremlin-console

用戶端,解壓,

remote.yaml 配置remote server位址, 購買graphdb後控制台可以看到這個位址。

hosts: [$host]
port: 8180           

打開gremlin-console

bin/gremlin.sh           

連接配接gremlin-server,并設定腳本自動送出至server

gremlin> :remote connect tinkerpop.server conf/remote.yaml session
==>Configured localhost/127.0.0.1:8180-[b1725987-7f5a-4a61-914a-d0ab39473105]
gremlin> :remote console
==>All scripts will now be sent to Gremlin Server - [localhost/127.0.0.1:8180]-[b1725987-7f5a-4a61-914a-d0ab39473105] - type ':remote console' to return to local mode           

執行

使用如下germlin語句做實時推薦,為id=2的顧客做商品推薦

注:無超級頂點的小圖場景可以這樣查詢,生産環境配合使用dedup()操作

gremlin> g.V("2").as("customer").out("ordered").out("contains").aggregate("products").in("contains").in("ordered").where(neq("customer")).out("ordered").out("contains").where(not(within("products"))).groupCount().by("name").order(local).by(values,decr).limit(local,5)
==>[SUKjnHCshw:13,pqiGHapYGW:12,ktkZSKsEdK:11,twLtDaYJmo:11,JAbhZdfhkO:10]           

如果使用關系型資料庫,sql如下:

select top (5) [t14].[productname]
    from (select count(*) as [value], [t13].[productname]
            from [customers] as [t0]
    cross apply (select [t9].[productname]
                    from [orders] as [t1]
                cross join [order details] as [t2]
                inner join [products] as [t3]
                        on [t3].[productid] = [t2].[productid]
                cross join [order details] as [t4]
                inner join [orders] as [t5]
                        on [t6].[customerid] = [t5].[customerid]
                cross join ([orders] as [t7]
                            cross join [order details] as [t8]
                            inner join [products] as [t9]
                                    on [t9].[productid] = [t8].[productid])
                    where not exists(select NULL as [empty]
                                        from [orders] as [t10]
                                cross join [order details] as [t11]
                                inner join [products] as [t12]
                                        on [t12].[productid] = [t11].[productid]
                                      where [t9].[productid] = [t12].[productid]
                                      and [t10].[customerid] = [t0].[customerid]
                                      and [t11].[orderid] = [t10].[orderid])
                    and [t6].[customerid] <> [t0].[customerid]
                    and [t1].[customerid] = [t0].[customerid]
                    and [t2].[orderid] = [t1].[orderid]
                    and [t4].[productid] = [t3].[productid]
                    and [t7].[customerid] = [t6].[customerid]
                    and [t8].[orderid] = [t7].[orderid]) as [t13]
                where [t0].[customerid] = N'1' //customerId
            group by [t13].[productname]) as [t14]
order by [t14].[value] desc                   

可見非常繁瑣,效率低下

繼續閱讀