連線到Adobe Experience Platform Query Service

Experience Platform上的JupyterLab可讓您在Python筆記本中使用SQL,透過查詢服務存取資料。 透過Query Service存取資料對於處理大型資料集很有用,因為其執行時間較長。 請注意,使用Query Service查詢資料的處理時間限製為10分鐘。

在JupyterLab中使用Query Service之前,請確定您瞭解Query Service SQL語法

若要在JupyterLab中利用查詢服務,您必須先建立正在運作的Python筆記本與查詢服務之間的連線。 這可以透過執行以下儲存格來達成。

qs_connect()

定義要探索的中值資料集

為了開始查詢和探索資料,必須提供中值資料集表格。 複製table_nametable_id值,並以您自己的資料表值取代。

target_table = "table_name"
target_table_id = "table_id"

完成後,此儲存格看起來應該類似下列範例:

target_table = "cross_industry_demo_midvalues"
target_table_id = "5f7c40ef488de5194ba0157a"

探索資料集以取得可用的日期

使用下方提供的儲存格,您可以檢視表格中涵蓋的日期範圍。 探索天數、第一個日期和最後一個日期的目的是協助選取日期範圍以供進一步分析。

%%read_sql -c QS_CONNECTION
SELECT distinct Year(timestamp) as Year, Month(timestamp) as Month, count(distinct DAY(timestamp)) as Count_days, min(DAY(timestamp)) as First_date, max(DAY(timestamp)) as Last_date, count(timestamp) as Count_hits
from {target_table}
group by Month(timestamp), Year(timestamp)
order by Year, Month;

執行儲存格會產生下列輸出:

查詢日期輸出

設定資料集探索的日期

在決定資料集探索的可用日期後,需要更新下列引數。 在此儲存格中設定的日期僅用於查詢形式的資料探索。 日期會再次更新為適合的範圍,以供本指南稍後章節的探索資料分析。

target_year = "2020" ## The target year
target_month = "02" ## The target month
target_day = "(01,02,03)" ## The target days

資料集探索

設定好所有引數、啟動Query Service並建立日期範圍後,您就可以開始讀取資料列了。 您應限制讀取的列數。

from platform_sdk.dataset_reader import DatasetReader
from datetime import date
dataset_reader = DatasetReader(PLATFORM_SDK_CLIENT_CONTEXT, dataset_id=target_table_id)
# If you do not see any data or would like to expand the default date range, change the following query
Table = dataset_reader.limit(5).read()

若要檢視資料集中可用的欄數,請使用下列儲存格:

print("\nNumber of columns:",len(Table.columns))

若要檢視資料集的列,請使用以下儲存格。 在此範例中,列數限製為五。

Table.head(5)

資料列輸出

一旦您瞭解資料集中包含哪些資料,進一步劃分資料集就十分實用。 在此範例中,會列出每個欄的欄名稱和資料型別,而輸出會用於檢查資料型別是否正確。

ColumnNames_Types = pd.DataFrame(Table.dtypes)
ColumnNames_Types = ColumnNames_Types.reset_index()
ColumnNames_Types.columns = ["Column_Name", "Data_Type"]
ColumnNames_Types

資料行名稱和資料型別清單

資料集趨勢探索

下節包含四個用於探索資料中趨勢和模式的查詢範例。 以下提供的範例並非詳盡無遺,但涵蓋一些較常檢視的功能。

指定日期的​ 小時活動計數

此查詢會分析一整天的動作和點按次數。 輸出會以表格的形式呈現,內含一天中每個小時活動計數的量度。

%%read_sql query_2_df -c QS_CONNECTION

SELECT Substring(timestamp, 12, 2)                        AS Hour,
       Count(enduserids._experience.aaid.id) AS Count
FROM   {target_table}
WHERE  Year(timestamp) = {target_year}
       AND Month(timestamp) = {target_month}
       AND Day(timestamp) in {target_day}
GROUP  BY Hour
ORDER  BY Hour;

查詢1輸出

確認查詢運作後,資料可以單變數繪圖長條圖呈現,以清楚顯示。

trace = go.Bar(
    x = query_2_df['Hour'],
    y = query_2_df['Count'],
    name = "Activity Count"
)

layout = go.Layout(
    title = 'Activity Count by Hour of Day',
    width = 1200,
    height = 600,
    xaxis = dict(title = 'Hour of Day'),
    yaxis = dict(title = 'Count')
)

fig = go.Figure(data = [trace], layout = layout)
iplot(fig)

查詢1 的長條圖輸出

指定日期前10個檢視頁面

此查詢會分析在指定日期中檢視次數最多的頁面。 輸出會以表格的形式呈現,內含頁面名稱和頁面檢視計數的量度。

%%read_sql query_4_df -c QS_CONNECTION

SELECT web.webpagedetails.name                 AS Page_Name,
       Sum(web.webpagedetails.pageviews.value) AS Page_Views
FROM   {target_table}
WHERE  Year(timestamp) = {target_year}
       AND Month(timestamp) = {target_month}
       AND Day(timestamp) in {target_day}
GROUP  BY web.webpagedetails.name
ORDER  BY page_views DESC
LIMIT  10;

確認查詢運作後,資料可以單變數繪圖長條圖呈現,以清楚顯示。

trace = go.Bar(
    x = query_4_df['Page_Name'],
    y = query_4_df['Page_Views'],
    name = "Page Views"
)

layout = go.Layout(
    title = 'Top Ten Viewed Pages For a Given Day',
    width = 1000,
    height = 600,
    xaxis = dict(title = 'Page_Name'),
    yaxis = dict(title = 'Page_Views')
)

fig = go.Figure(data = [trace], layout = layout)
iplot(fig)

前10個已檢視頁面

依使用者活動分組的前10個城市

此查詢會分析資料源自哪些城市。

%%read_sql query_6_df -c QS_CONNECTION

SELECT concat(placeContext.geo.stateProvince, ' - ', placeContext.geo.city) AS state_city,
       Count(timestamp)                                                     AS Count
FROM   {target_table}
WHERE  Year(timestamp) = {target_year}
       AND Month(timestamp) = {target_month}
       AND Day(timestamp) in {target_day}
GROUP  BY state_city
ORDER  BY Count DESC
LIMIT  10;

確認查詢運作後,資料可以單變數繪圖長條圖呈現,以清楚顯示。

trace = go.Bar(
    x = query_6_df['state_city'],
    y = query_6_df['Count'],
    name = "Activity by City"
)

layout = go.Layout(
    title = 'Top Ten Cities by User Activity',
    width = 1200,
    height = 600,
    xaxis = dict(title = 'City'),
    yaxis = dict(title = 'Count')
)

fig = go.Figure(data = [trace], layout = layout)
iplot(fig)

前十個城市

前10個檢視的產品

此查詢提供前10個檢視產品的清單。 在下列範例中,Explode()函式是用來將productlistitems物件中的每個產品傳回至其自己的資料列。 這可讓您執行巢狀查詢,以彙總不同SKU的產品檢視。

%%read_sql query_7_df -c QS_CONNECTION

SELECT Product_List_Items.sku AS Product_SKU,
       Sum(Product_Views) AS Total_Product_Views
FROM  (SELECT Explode(productlistitems) AS Product_List_Items,
              commerce.productviews.value   AS Product_Views
       FROM   {target_table}
       WHERE  Year(timestamp) = {target_year}
              AND Month(timestamp) = {target_month}
              AND Day(timestamp) in {target_day}
              AND commerce.productviews.value IS NOT NULL)
GROUP BY Product_SKU
ORDER BY Total_Product_Views DESC
LIMIT  10;

確認查詢運作後,資料可以單變數繪圖長條圖呈現,以清楚顯示。

trace = go.Bar(
    x = "SKU-" + query_7_df['Product_SKU'],
    y = query_7_df['Total_Product_Views'],
    name = "Product View"
)

layout = go.Layout(
    title = 'Top Ten Viewed Products',
    width = 1200,
    height = 600,
    xaxis = dict(title = 'SKU'),
    yaxis = dict(title = 'Product View Count')
)

fig = go.Figure(data = [trace], layout = layout)
iplot(fig)

前10個產品檢視

探索資料的趨勢和模式後,您應該清楚知道要為目標的預測建立哪些功能。 略過表格可以快速反白顯示每個資料屬性的形式、明顯的錯誤表示和值中的大型離群值,並開始建議要探索屬性之間的候選關係。