1. Giriş
Bu codelab'de, kurgusal bir perakende şirketi olan Cymbal Pets için 360 derece müşteri görünümü ve öneri motoru oluşturmak üzere BigQuery Graph'i nasıl kullanacağınızı öğreneceksiniz. Grafik verilerini doğrudan BigQuery'de oluşturmak, sorgulamak ve analiz etmek için SQL'in gücünden yararlanacak, gelişmiş ürün önerileri için vektör arama ile birleştireceksiniz.
BigQuery Graph, veri varlıklarınız (ör. müşteriler, ürünler ve siparişler) arasındaki ilişkileri grafik olarak modellemenize olanak tanır. Bu sayede, müşteri davranışı ve ürün yakınlıkları ile ilgili karmaşık soruları kolayca yanıtlayabilirsiniz.

Yapacaklarınız
- Cymbal Pets grafiği için BigQuery veri kümesi ve şema oluşturma
- Cloud Storage'dan örnek verileri (Müşteriler, Ürünler, Siparişler, Mağazalar) yükleme
- BigQuery'de bu öğeleri bağlayan bir Özellik Grafiği oluşturun.
- Grafik sorgularını kullanarak müşterilerin satın alma geçmişini görselleştirme
- Vector Search'ü kullanarak ürün önerisi sistemi oluşturma
- "Birlikte satın alınanlar" grafik ilişkilerini kullanarak önerileri geliştirme
İhtiyacınız olanlar
- Chrome gibi bir web tarayıcısı
- Faturalandırmanın etkin olduğu bir Google Cloud projesi
Bu codelab, başlangıç seviyesindekiler de dahil olmak üzere her seviyeden geliştiriciye yöneliktir.
2. Başlamadan önce
Google Cloud projesi oluşturma
- Google Cloud Console'da bir Google Cloud projesi seçin veya oluşturun.
- Cloud projeniz için faturalandırmanın etkinleştirildiğinden emin olun.
Cloud Shell'i Başlatma
- Google Cloud Console'un üst kısmından Cloud Shell'i etkinleştir'i tıklayın.
- Kimlik doğrulamayı doğrulayın:
gcloud auth list
- Projenizi onaylayın:
gcloud config get project
- Gerekirse ayarlayın:
export PROJECT_ID=<YOUR_PROJECT_ID>
gcloud config set project $PROJECT_ID
API'leri etkinleştir
Gerekli BigQuery API'yi etkinleştirmek için şu komutu çalıştırın:
gcloud services enable bigquery.googleapis.com
3. Şemayı tanımlama
İlk olarak, grafikle ilgili tablolarınızı depolamak için bir veri kümesi oluşturmanız ve düğümleriniz ile kenarlarınızın şemasını tanımlamanız gerekir.
- Bu codelab'de SQL komutlarını yürüteceğiz. Bu komutları BigQuery Studio > SQL Düzenleyici'de çalıştırabilir veya Cloud Shell'de
bq querykomutunu kullanabilirsiniz.
Çok satırlı oluşturma ifadeleriyle daha iyi bir deneyim için BigQuery SQL Düzenleyici'yi kullandığınızı varsayacağız. cymbal_pets_demoveri kümesini oluşturun:
CREATE SCHEMA IF NOT EXISTS cymbal_pets_demo;
order_items,products,orders,stores,customersveco_related_products_for_angelicaiçin tablolar oluşturun. Bu tablolar, grafiğimizin kaynak verileri olarak kullanılır.
CREATE TABLE IF NOT EXISTS cymbal_pets_demo.order_items
(
order_id INT64,
product_id INT64,
order_item_id INT64,
quantity INT64,
price FLOAT64,
PRIMARY KEY (order_id, product_id, order_item_id) NOT ENFORCED
)
CLUSTER BY order_item_id;
CREATE TABLE IF NOT EXISTS cymbal_pets_demo.products
(
product_id INT64,
product_name STRING,
brand STRING,
category STRING,
subcategory INT64,
animal_type INT64,
search_keywords INT64,
price FLOAT64,
description STRING,
inventory_level INT64,
supplier_id INT64,
average_rating FLOAT64,
uri STRING,
embedding ARRAY<FLOAT64>,
PRIMARY KEY (product_id) NOT ENFORCED
)
CLUSTER BY product_id;
CREATE TABLE IF NOT EXISTS cymbal_pets_demo.orders
(
customer_id INT64,
order_id INT64,
shipping_address_city STRING,
store_id INT64,
order_date DATE,
order_type STRING,
payment_method STRING,
PRIMARY KEY (order_id) NOT ENFORCED
)
PARTITION BY order_date
CLUSTER BY order_id;
CREATE TABLE IF NOT EXISTS cymbal_pets_demo.stores
(
store_id INT64,
store_name STRING,
address_state STRING,
address_city STRING,
latitude FLOAT64,
longitude FLOAT64,
opening_hours STRUCT<Monday STRING, Tuesday STRING, Wednesday STRING, Thursday STRING, Friday STRING, Saturday STRING, Sunday STRING>,
manager_id INT64,
PRIMARY KEY (store_id) NOT ENFORCED
)
CLUSTER BY store_id;
CREATE TABLE IF NOT EXISTS cymbal_pets_demo.customers
(
customer_id INT64,
first_name STRING,
last_name STRING,
email STRING,
gender STRING,
address_city STRING,
address_state STRING,
loyalty_member BOOL,
PRIMARY KEY (customer_id) NOT ENFORCED
)
CLUSTER BY customer_id;
CREATE TABLE IF NOT EXISTS cymbal_pets_demo.co_related_products_for_angelica
(
angelica_product_id INT64,
other_product_id INT64,
co_purchase_count INT64
);
Artık grafik verilerinizin yapısını tanımladınız.
4. Verileri Yükleme
Şimdi tabloları Cloud Storage'daki örnek verilerle doldurun.
BigQuery SQL düzenleyicisinde aşağıdaki LOAD DATA ifadelerini çalıştırın:
LOAD DATA INTO `cymbal_pets_demo.customers`
FROM FILES (
format = 'AVRO',
uris = ['gs://sample-data-and-media/cymbal-pets/tables/customers/*.avro']
);
LOAD DATA INTO `cymbal_pets_demo.order_items`
FROM FILES (
format = 'AVRO',
uris = ['gs://sample-data-and-media/cymbal-pets/tables/order_items/*.avro']
);
LOAD DATA INTO `cymbal_pets_demo.orders`
FROM FILES (
format = 'AVRO',
uris = ['gs://sample-data-and-media/cymbal-pets/tables/orders/*.avro']
);
LOAD DATA INTO `cymbal_pets_demo.products`
FROM FILES (
format = 'AVRO',
uris = ['gs://sample-data-and-media/cymbal-pets/tables/products/*.avro']
);
LOAD DATA INTO `cymbal_pets_demo.stores`
FROM FILES (
format = 'AVRO',
uris = ['gs://sample-data-and-media/cymbal-pets/tables/stores/*.avro']
);
Satırların her tabloya yüklendiğine dair bir onay mesajı görürsünüz.
5. Mülk grafiğini oluşturma
Veriler yüklendikten sonra özellik grafiğini tanımlayabilirsiniz. Bu, BigQuery'ye hangi tabloların düğümleri (Müşteriler, Ürünler gibi öğeler) ve hangi tabloların kenarları (Ziyaret etti, Yerleştirdi, Sahip gibi ilişkiler) temsil ettiğini söyler.

Aşağıdaki DDL ifadesini çalıştırın:
CREATE OR REPLACE PROPERTY GRAPH cymbal_pets_demo.PetsOrderGraph
NODE TABLES (
cymbal_pets_demo.customers KEY(customer_id) LABEL Customer,
cymbal_pets_demo.products KEY(product_id) LABEL Products,
cymbal_pets_demo.stores KEY(store_id) LABEL Stores,
cymbal_pets_demo.orders KEY(order_id) LABEL Orders
)
EDGE TABLES (
cymbal_pets_demo.orders as customer_to_store_edge
KEY (order_id)
SOURCE KEY (customer_id) references customers(customer_id)
DESTINATION KEY (store_id) references stores(store_id)
LABEL Visited
PROPERTIES ALL COLUMNS,
cymbal_pets_demo.order_items
KEY (order_item_id)
SOURCE KEY (order_id) references orders(order_id)
DESTINATION KEY (product_id) references products(product_id)
LABEL Has
PROPERTIES ALL COLUMNS,
cymbal_pets_demo.orders as customer_to_orders_edge
KEY (order_id)
SOURCE KEY (customer_id) references customers(customer_id)
DESTINATION KEY (order_id) references orders(order_id)
LABEL Placed
PROPERTIES ALL COLUMNS,
cymbal_pets_demo.co_related_products_for_angelica
KEY (angelica_product_id)
SOURCE KEY (angelica_product_id) references products(product_id)
DESTINATION KEY (other_product_id) references products(product_id)
LABEL BoughtTogether
PROPERTIES ALL COLUMNS
);
Bu işlem, GRAPH_TABLE operatörünü kullanarak grafik geçişleri yapmamıza olanak tanıyan PetsOrderGraph grafiğini oluşturur.
6. Tüm müşterilerin satın alma geçmişini görselleştirme
BigQuery Studio'da New Notebook'u (Yeni Not Defteri) açın.

Bu codelab'in görselleştirme ve öneri bölümlerinde BigQuery Studio'da Google Colab not defteri kullanacağız. Bu sayede grafik sonuçlarını kolayca görselleştirebiliriz.
BigQuery Graph Notebook, IPython Magics olarak uygulanır. %%bigquery sihirli komutunu TO_JSON işleviyle birlikte ekleyerek sonuçları aşağıdaki bölümlerde gösterildiği gibi görselleştirebilirsiniz.
Cymbal Pets'in belirli bir zaman aralığında tüm müşterilerin ve yaptıkları satın alma işlemlerinin 360 derece görselleştirmesini istediğini varsayalım.
Yeni bir hücrede aşağıdakileri çalıştırın:
%%bigquery --graph
GRAPH cymbal_pets_demo.PetsOrderGraph
# finds the customer node and then finds all
# the Orders nodes that are connected to that customer through the
# Placed relationship
MATCH (customer:Customer)-[placed:Placed]->(ordr:Orders)-[has:Has]->(product:Products)
# filters the Orders nodes to only include those where the
# order_date is within the last 3 months.
WHERE ordr.order_date >= date('2024-11-27')
# # This line finds all the Products nodes that are connected to the
# # filtered Orders nodes through the Has relationship.
MATCH p=(customer:Customer)-[placed:Placed]->(ordr:Orders)-[has:Has]->(product:Products)
LIMIT 40
RETURN
TO_JSON(p) as paths
Grafik sonucunun görsel bir temsilini görürsünüz.

7. Angelica'nın işlem geçmişini görselleştir
Cymbal Pets'in Angelica Russell adlı bir müşteriyi ayrıntılı olarak incelemek istediğini varsayalım. Angelica'nın son 3 ay içinde satın aldığı ürünleri ve müşterinin ziyaret ettiği mağazaları analiz etmek istiyor.
%%bigquery --graph
GRAPH cymbal_pets_demo.PetsOrderGraph
# finds the customer node with the name "Angelica Russell" and then finds all
# the Orders nodes that are connected to that customer through the
# Placed relationship and all the Products nodes that are connected to the
# filtered Orders nodes through the Has relationship.
MATCH p=(customer:Customer {first_name: 'Angelica', last_name: 'Russell'})-[placed:Placed]->(ordr:Orders)-[has:Has]->(product:Products)
# filters the Orders nodes to only include those where the
# order_date is within the last 3 months.
WHERE ordr.order_date >= date('2024-11-27')
# finds the Stores nodes where Angelica placed order from
MATCH p2=(customer)-[visited:Visited]->(store:Stores)
RETURN
TO_JSON(p) as path, TO_JSON(p2) as path2

8. Vektör araması kullanarak ürün önerisi
Cymbal Pets, Angelica'ya yakın zamanda satın aldığı ürünlere göre ürün önermek istiyor. Geçmiş satın alma işlemlerine benzer yerleştirmelere sahip ürünleri bulmak için vektör arama özelliğini kullanabiliriz.
Yeni bir Colab hücresinde aşağıdaki SQL komut dosyasını çalıştırın. Bu komut dosyası:
- Angelica'nın yakın zamanda satın aldığı ürünleri tanımlar.
VECTOR_SEARCHtablosundaki en benzer 4 ürünü bulmak içinproductskullanılır.
Not: Bu adımda, ürünler tablosunda bir yerleştirme sütunu oluşturmak için AI.GENERATE_EMBEDDINGS işlevini daha önce çalıştırdığınız varsayılır.
%%bigquery
DECLARE products_bought_by_angelica ARRAY<INT64>;
-- 1. Get IDs of products bought by Angelica
SET products_bought_by_angelica = (
SELECT ARRAY_AGG(product_id) FROM
GRAPH_TABLE(
cymbal_pets_demo.PetsOrderGraph
MATCH (c:Customer {first_name: 'Angelica', last_name: 'Russell'})-[placed:Placed]->(o:Orders)
WHERE o.order_date >= date('2024-11-27')
MATCH (o)-[has_edge:Has]->(p:Products)
RETURN DISTINCT p.product_id as product_id
));
-- 2. Find similar products using vector search
SELECT
query.product_name as AngelicaBought,
base.product_name as RecommendedProducts,
base.category
FROM
VECTOR_SEARCH(
TABLE cymbal_pets_demo.products,
'embedding',
(SELECT * FROM cymbal_pets_demo.products
WHERE product_id IN UNNEST(products_bought_by_angelica)),
'embedding',
top_k => 4)
WHERE query.product_name <> base.product_name;
Angelica'nın satın aldığı ürünlere anlamsal olarak benzeyen önerilen ürünlerin listesini görürsünüz.

9. "Birlikte satın alınan ürünler" ilişkilerini kullanan öneri
Bir diğer etkili öneri tekniği de "Ortak Filtreleme"dir. Bu teknikte, diğer kullanıcılar tarafından sıklıkla birlikte satın alınan ürünler önerilir. Bunu grafiğimizde BoughtTogether kenarı olarak modelledik.
Birlikte satın alınan ürünleri önermek için Cymbal Pets, Angelica'nın satın aldığı her ürün için önerilecek en iyi ürünleri bulmak üzere analitik bir çevrimdışı grafik sorgusu yürüttü.
%%bigquery
CREATE OR REPLACE TABLE cymbal_pets_demo.co_related_products_for_angelica AS
SELECT
angelica_product_id,
other_product_id,
co_purchase_count
FROM (
SELECT
angelicaProduct.product_id AS angelica_product_id,
otherProduct.product_id AS other_product_id,
count(otherProduct) AS co_purchase_count,
# ensures that the row numbering is done separately for each angelica_product_id
ROW_NUMBER() OVER (PARTITION BY angelicaProduct.product_id ORDER BY count(otherProduct) DESC) AS rn
FROM
GRAPH_TABLE (cymbal_pets_demo.PetsOrderGraph
MATCH (angelica:Customer {first_name: 'Angelica', last_name: 'Russell'})-[:Placed]->(o:Orders)-[:Has]->(angelicaProduct:Products)
WHERE o.order_date >= date('2024-11-27')
WITH angelica, angelicaProduct
MATCH (otherCustomer:Customer)-[:Placed]->(otherOrder:Orders)-[:Has]->(angelicaProduct) # Find orders where Angelica's products were bought
WHERE otherCustomer <> angelica # Exclude Angelica's own orders
WITH angelicaProduct, otherOrder
MATCH (otherOrder)-[:HAS]->(otherProduct:Products) # Find other products in those orders
WHERE angelicaProduct <> otherProduct # Exclude the original product.
RETURN angelicaProduct, otherProduct, otherOrder
)
GROUP BY
angelicaProduct.product_id, otherProduct.product_id
)
WHERE rn <= 3; # only keep top 3 co-related products

BoughtTogether kenarı aracılığıyla Angelica'nın satın alma işlemleriyle doğrudan bağlantılı ürünleri önermek için bu sorguyu çalıştırın:
%%bigquery
SELECT * FROM GRAPH_TABLE(
cymbal_pets_demo.PetsOrderGraph
MATCH (customer:Customer {first_name: 'Angelica', last_name: 'Russell'})-[placed:Placed]->(ordr:Orders)
WHERE ordr.order_date >= date('2024-11-27')
MATCH (ordr)-[has:Has]->(product:Products)
MATCH (product)-[bought_together:BoughtTogether]->(recommended_product:Products)
RETURN
product.product_name AS OriginalProduct,
recommended_product.product_name AS Recommended,
bought_together.co_purchase_count AS Strength
);
Bu sorgu, Müşteri -> Sipariş -> Ürün -> (BirlikteSatınAlınan) -> Önerilen Ürün şeklinde ilerleyerek toplu satın alma davranışına dayalı öneriler gösterir.

10. Temizleme
Google Cloud hesabınızın sürekli olarak ücretlendirilmesini önlemek için bu codelab sırasında oluşturulan kaynakları silin.
Veri kümesini ve tüm tabloları silme:
DROP SCHEMA IF EXISTS cymbal_pets_demo CASCADE;
Bu codelab için yeni bir proje oluşturduysanız projeyi de silebilirsiniz:
gcloud projects delete $PROJECT_ID
11. Tebrikler
Tebrikler! BigQuery Graph'i kullanarak başarıyla bir 360 derece müşteri görünümü ve öneri motoru oluşturmuş olmanız gerekir.
Öğrendikleriniz
- BigQuery'de özellik grafiği oluşturma
- Grafik düğümlerine ve kenarlarına veri yükleme
GRAPH_TABLEveMATCHkullanarak grafik desenlerini sorgulama- Karma öneriler için grafik sorgularını vektör aramasıyla birleştirme
Sonraki adımlar
- BigQuery Graph belgelerini inceleyin.
- BigQuery'de Vector Search hakkında daha fazla bilgi edinin.