天天看點

HDInsight-Hadoop實戰(一)網站日志分析HDInsight-Hadoop實戰(一)網站日志分析

在此示例中,你将使用分析網站日志檔案的 HDInsight 查詢來深入了解客戶使用網站的方式。借助此分析,你可檢視外部網站一天内對該網站的通路頻率以及使用者體驗的網站錯誤總結。

在此教程中,你将學習如何使用 HDInsight:

連接配接到包含網站日志檔案的 Azure Storage Blob

建立配置單元表以查詢這些日志

建立配置單元查詢以分析資料

使用 Microsoft Excel 連接配接到 HDInsight(使用 ODBC 連接配接)以檢索已分析的資料

已使用群集配置你完成腳本和查詢所需的所有内容。要将已分析的資料導出到 Microsoft Excel,你必須滿足以下要求:

必須安裝了 Microsoft Excel 2010 或 Microsoft     Excel 2013。

以下是此示例使用的網站日志資料位置。你可從此頁頂部的檔案浏覽器頁籤通路此資料。也可以在 [default storage account]/[defaultcontainer]/HdiSamples/WebsiteLogSampleData/SampleLog 路徑下通路此示例的資料。

網站日志資料

wasb://[email protected]/HdiSamples/WebsiteLogSampleData/SampleLog/

以下配置單元語句建立了一個外部表,允許配置單元查詢存儲在 Azure Blob 存儲中的資料。外部表以初始檔案格式保留資料,同時允許配置單元針對檔案内的資料執行查詢。

配置單元語句通過描述檔案内的字段、字段間的界定符以及  Azure Blob 存儲中檔案的位置建立了名為網絡日志的新表。在此教程的建立配置單元查詢以分析資料章節,你将針對存儲在此表中的資料執行查詢。

CreateExternal Table weblogs

DROP TABLE IFEXISTS weblogs;

--create tableweblogs on space-delimited website log data

CREATE EXTERNALTABLE IF NOT EXISTS weblogs(s_date date, s_time string, s_sitename string,cs_method string, cs_uristem string,

cs_uriquerystring, s_port int, cs_username string, c_ip string, cs_useragent string,

cs_cookiestring, cs_referer string, cs_host string, sc_status int, sc_substatus int,

sc_win32statusint, sc_bytes int, cs_bytes int, s_timetaken int )

ROW FORMATDELIMITED FIELDS TERMINATED BY ' '

STORED ASTEXTFILE LOCATION'wasb://[email protected]/HdiSamples/WebsiteLogSampleData/SampleLog/'

TBLPROPERTIES('skip.header.line.count'='2');

以下配置單元查詢基于網絡日志表上運作的查詢建立了兩個新表。新表名為 clienterrors 和 refersperday。

clienterrors 的查詢從介于 400 到 500 之間的 HTTP 狀态代碼的網絡日志表中提取資料,并且按遭遇這些錯誤的使用者以及錯誤代碼類型對其進行分組。狀态代碼的範圍介于 400 到 500 之間,通過網絡日志表中的 sc_status 清單示,對應通路網站時用戶端遭遇的錯誤。然後,提取的資料按每個錯誤代碼的發生次數進行排序并寫入 clienterrors 表。

refersperday 的查詢從引用此網站的所有外部網站的網絡日志表中提取資料。外部網站資訊從網絡日志表的 cs_referer 列中提取。為了確定引用連結不遭遇錯誤,表僅顯示傳回 200 到 300 之間的 HTTP 狀态代碼的頁面資料。然後,提取的資料将寫入 refersperday 表。

DROP TABLE IFEXISTS ClientErrors;

--create tableClientErrors for storing errors users experienced and their frequencies

CREATE EXTERNALTABLE ClientErrors(sc_status int, cs_referer string, cs_page string, cnt int)

ROW FORMATDELIMITED FIELDS TERMINATED BY ',';

--populate tableClientErrors with data from table weblogs

INSERT OVERWRITETABLE ClientErrors

SELECT sc_status,cs_referer,

                                concat(cs_uristem,'?',regexp_replace(cs_uriquery,'X-ARR-LOG-ID=[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}',''))cs_page,

                                count(distinctc_ip) as cnt

FROM weblogs

WHERE sc_status>=400 and sc_status < 500

GROUP BYsc_status, cs_referer, concat(cs_uristem,'?',regexp_replace(cs_uriquery,'X-ARR-LOG-ID=[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}',''))

ORDER BY cnt;

-------------------------------------------------------------------------------------------

DROP TABLE IFEXISTS RefersPerDay;

--create tableRefersPerDay for storing references from external websites

CREATE EXTERNALTABLE IF NOT EXISTS RefersPerDay(year int, month int, day int, cs_refererstring, cnt int)

--populate tableRefersPerDay with data from the weblogs table

INSERT OVERWRITETABLE RefersPerDay

SELECTyear(s_date), month(s_date), day(s_date), cs_referer, count(distinct c_ip) ascnt

WHERE sc_status>=200 and sc_status <300

GROUP BY s_date,cs_referer

ORDER BY cntdesc;

單擊送出以運作先前章節中顯示的查詢。查詢執行以下任務:

從 HDInsight 群集關聯的 Azure     Blob 存儲中的原始網站日志資料建立網絡日志表。

建立并填充先前章節中描述的 clienterrors 和 refersperday 表。

運作查詢時,你可單擊檢視詳細資訊來擷取有關背景運作任務的更多資訊。在頁底所有作業都處于已完成狀态後,繼續執行 将資料加載到 Excel。

                                    cs_uriquerystring, s_port int, cs_username string, c_ip string, cs_useragent string,

                                    cs_cookiestring, cs_referer string, cs_host string, sc_status int, sc_substatus int,

                                    sc_win32statusint, sc_bytes int, cs_bytes int, s_timetaken int )

STORED ASTEXTFILE LOCATION 'wasb://[email protected]/HdiSamples/WebsiteLogSampleData/SampleLog/'

--populatetable ClientErrors with data from table weblogs

INSERTOVERWRITE TABLE ClientErrors

SELECTsc_status, cs_referer,

                        concat(cs_uristem,'?',regexp_replace(cs_uriquery,'X-ARR-LOG-ID=[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}',''))cs_page,

                        count(distinct c_ip) ascnt

GROUP BYsc_status, cs_referer, concat(cs_uristem,'?', regexp_replace(cs_uriquery,'X-ARR-LOG-ID=[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}',''))

--populatetable RefersPerDay with data from the weblogs table

INSERTOVERWRITE TABLE RefersPerDay

GROUP BYs_date, cs_referer

作業會話

查詢名稱

日期

ID

操作

狀态

表中無可用資料

1. 打開 Excel 并建立空白的工作表。

2. 從資料頁籤中,選擇來自其他源,然後選擇來自 Microsoft 查詢。

HDInsight-Hadoop實戰(一)網站日志分析HDInsight-Hadoop實戰(一)網站日志分析

3. 提示選擇資料源時,選擇示例 Microsoft 配置單元 DSN。

HDInsight-Hadoop實戰(一)網站日志分析HDInsight-Hadoop實戰(一)網站日志分析

4. 在 Microsoft 配置單元 ODBC 驅動器連接配接對話框中,輸入以下值,然後單擊“确定”。

主機 - HDInsight 群集的主機名。例如,mycluster.azurehdinsight.net

使用者名 - HDInsight 群集的管理者名稱

密碼 - 管理者密碼

所有其它字段均為預設值。

HDInsight-Hadoop實戰(一)網站日志分析HDInsight-Hadoop實戰(一)網站日志分析

5. 在查詢向導中,選擇 refersperday 表,然後選擇 > 按鈕。

HDInsight-Hadoop實戰(一)網站日志分析HDInsight-Hadoop實戰(一)網站日志分析

6. 單擊下一步繼續檢視向導,直到到達帶有完成按鈕的對話框。單擊完成。

7. 出現導入資料對話框後,單擊确定以接受預設值。完成查詢後,資料将顯示在 Excel 中。

HDInsight-Hadoop實戰(一)網站日志分析HDInsight-Hadoop實戰(一)網站日志分析

在本教程中,你了解了如何使用 Azure HDInsight 分析使用 Apache Hive 的網站日志資料。你浏覽了一個流程,了解原始資料如何先上載到  Azure 存儲空間 Blob 再加載到配置單元表以便執行查詢。最後,你了解了如何将配置單元查詢的結果導入到 Microsoft Excel。如果你具有本教程或其他示例方面的回報,請使用上面的幫助 + 回報連結。

使用以下連結繼續了解如何将配置單元和 Excel 與 HDInsight 一同使用。

<a target="_blank" href="http://azure.microsoft.com/en-us/documentation/articles/hdinsight-use-hive/">将配置單元和 HDInsight 中 Hadoop 一同使用</a>

<a target="_blank" href="http://azure.microsoft.com/en-us/documentation/articles/hdinsight-analyze-twitter-data/">使用 HDInsight 中 Hadoop 分析 Twitter 資料</a>

<a target="_blank" href="http://azure.microsoft.com/en-us/documentation/articles/hdinsight-connect-excel-hive-odbc-driver/">使用 Microsoft 配置單元 ODBC 驅動程式将 Excel 連接配接到 Hadoop</a>