大數據學習5:網站訪問日志分析

1.數據處理

1.1 環境準備

進入cd /opt/server/hadoop-3.1.0/sbin文件夾,停止hdfs服務

cd /opt/server/hadoop-3.1.0/sbin
./stop-dfs.sh

進入/opt/server/hadoop-3.1.0/etc/hadoop文件夾,編輯yarn-site.xml文件

/opt/server/hadoop-3.1.0/etc/hadoop
vim yarn-site.xml

yarn.nodemanager.vmem-pmem-ratio參數代表虛擬內存與物理內存的比率,如果超過默認2.1倍的限制,進程會被關閉,可以調高yarn.nodemanager.vmem-pmem-ratio值,或將yarn.nodemanager.vmem-check-enabled=false,關閉虛擬內存檢查 ?

<configuration><!-- Site specific YARN configuration properties --><property><name>yarn.nodemanager.aux-services</name><value>mapreduce_shuffle</value></property><property><name>yarn.nodemanager.vmem-pmem-ratio</name><value>4</value></property>
</configuration>

進入cd /opt/server/hadoop-3.1.0/sbin文件夾,啟動hdfs服務

cd /opt/server/hadoop-3.1.0/sbin
./start-dfs.sh

?啟動hive,創建一個新數據庫

cd /opt/server/apache-hive-3.1.2-bin/bin
hive
create database web_log;
use web_log;

創建原始表:

CREATE TABLE access_log_content (content STRING
);
CREATE TABLE ip_content (content STRING
);

上傳數據?

?導入數據

load data local inpath '/opt/data/web.log' into table access_log_content;
load data local inpath '/opt/data/ip.txt' into table ip_content;select * from access_log_content limit 5;
select * from ip_content limit 5;

1.2?日志文件屬性提取

?中間表

DROP TABLE IF EXISTS access_log_tmp1;
CREATE TABLE access_log_tmp1 (
id BIGINT,
ip STRING,
ip_num BIGINT,      -- ip對應的十進制數
ip_1 BIGINT,        -- ip首位數字
access_time STRING, -- 訪問時間
url STRING,         -- 訪問鏈接
status STRING,      -- http狀態碼
traffic STRING,     -- 流量
referer STRING,     -- 來源
c_info STRING       -- 客戶端信息
);
-- 使用正則表達式提取并向臨時表插入數據
INSERT OVERWRITE TABLE access_log_tmp1 
SELECT 
CAST(split(content, ' ')[0] AS BIGINT) AS id, 
split(content, ' ')[1] AS ip, 
cast(split(split(content, ' ')[1], '\\.')[0] as int)  * 256 * 256 * 256 + 
cast(split(split(content, ' ')[1], '\\.')[1] as int) * 256 * 256 + 
cast(split(split(content, ' ')[1], '\\.')[2] as int) * 256 + 
cast(split(split(content, ' ')[1], '\\.')[3] as int) AS ip_num,
cast(split(split(content, ' ')[1], '\\.')[0] as int) AS ip_1, 
regexp_extract(content,'(\\[.*\\])',1) AS access_time,
split(regexp_extract(content,'(".*?")',1)," ")[1] AS url, 
split(content,' ')[9] AS status, 
split(content,' ')[10] AS traffic, 
split(content,' ')[11]  AS referer, 
regexp_extract(content,'\" (".*?")$',1) AS c_info 
FROM access_log_content;
select * from access_log_tmp1 limit 5;

1.3 IP信息提取

DROP TABLE IF EXISTS cz_ip;
CREATE TABLE cz_ip (
ip_start BIGINT,   -- 起始ip對應的十進制
ip_start_1 STRING, -- 起始ip首位
ip_end BIGINT,     -- 結束ip對應的十進制
city STRING,       -- 城市
isp STRING         -- 運營商
);
-- 使用正則表達式提取并向IP表插入數據
INSERT OVERWRITE TABLE cz_ip 
SELECT 
CAST(split(split(content, '\\s+')[0],'\\.')[0] AS BIGINT) * 256 * 256 * 256 + 
CAST(split(split(content, '\\s+')[0],'\\.')[1] AS BIGINT) * 256 * 256 + 
CAST(split(split(content, '\\s+')[0],'\\.')[2] AS BIGINT) * 256 + 
CAST(split(split(content, '\\s+')[0],'\\.')[3] AS BIGINT) AS ip_start, 
split(split(content, '\\s+')[0],'\\.')[0] AS ip_start_1, 
CAST(split(split(content, '\\s+')[1],'\\.')[0] AS BIGINT) * 256 * 256 * 256 + 
CAST(split(split(content, '\\s+')[1],'\\.')[1] AS BIGINT) * 256 * 256 + 
CAST(split(split(content, '\\s+')[1],'\\.')[2] AS BIGINT) * 256 + 
CAST(split(split(content, '\\s+')[1],'\\.')[3] AS BIGINT) AS ip_end, 
split(content, '\\s+')[2] AS city, 
split(content, '\\s+')[3] AS isp
FROM ip_content;
select * from cz_ip limit 5;

1.4 信息合并

-- 初始化臨時表
DROP TABLE IF EXISTS access_log_tmp2;
CREATE TABLE access_log_tmp2 (
id BIGINT,
ip STRING,
city STRING,
isp STRING,
access_time STRING,
url STRING,
status STRING,
traffic STRING,
referer STRING,
c_info STRING
);
-- 從ip表中查詢城市和運營商信息
INSERT OVERWRITE TABLE access_log_tmp2
SELECT a.id, a.ip, b.city, b.isp, a.access_time, a.url, a.status, a.traffic, a.referer, a.c_info
FROM access_log_tmp1 a JOIN 
cz_ip b ON a.ip_1 = b.ip_start_1 AND a.ip_num >= b.ip_start AND a.ip_num <= b.ip_end;
select * from access_log_tmp2 limit 5;

1.5 用戶所在省和訪問鏈接

-- 初始化臨時表
DROP TABLE IF EXISTS access_log_tmp3;
CREATE TABLE access_log_tmp3 (
id BIGINT,
ip STRING,
province STRING,
city STRING,
isp STRING,
access_time STRING,
url STRING,
status STRING,
traffic STRING,
referer STRING,
c_info STRING
);
-- 提取省信息使用正則表達式提取訪問鏈接的實際地址并向臨時表插入數據
INSERT OVERWRITE TABLE access_log_tmp3
SELECT id,ip,
CASE WHEN INSTR(city, '省') > 0 THEN SUBSTR(city,1,INSTR(city,'省')-1)
WHEN INSTR(city,'內蒙古') > 0 THEN '內蒙古'
WHEN INSTR(city,'西藏') > 0 THEN '西藏'
WHEN INSTR(city,'廣西') > 0 THEN '廣西'
WHEN INSTR(city,'寧夏') > 0 THEN '寧夏'
WHEN INSTR(city,'新疆') > 0 THEN '新疆'
WHEN INSTR(city,'北京') > 0 THEN '北京'
WHEN INSTR(city,'上海') > 0 THEN '上海'
WHEN INSTR(city,'天津') > 0 THEN '天津'
WHEN INSTR(city,'重慶') > 0 THEN '重慶'
WHEN INSTR(city,'香港') > 0 THEN '香港'
WHEN INSTR(city,'澳門') > 0 THEN '澳門'
ELSE city end 
AS province, 
city,
isp,
access_time,
split(url,"\\?")[0] AS url
,status, traffic, referer, c_info
FROM access_log_tmp2;
select * from access_log_tmp3 limit 5;

?1.6 訪問時間和referer

-- 初始化臨時表
DROP TABLE IF EXISTS access_log_tmp4;
CREATE TABLE access_log_tmp4 (
id BIGINT,
ip STRING,
province STRING,
city STRING,
isp STRING,
access_time STRING,
access_hour STRING,
url STRING,
status STRING,
traffic STRING,
referer STRING,
ref_type STRING,
c_info STRING
);
-- 使用正則表達式提取訪問時間和來源分類并向臨時表插入數據
INSERT OVERWRITE TABLE access_log_tmp4 
SELECT id, ip, province, city, isp,  
regexp_extract(access_time,":(\\d.*) ",1) AS access_time,
regexp_extract(access_time,":(\\d+):",1) AS access_hour
, url, status, traffic, referer
, 
CASE WHEN   
INSTR(referer, 'www.pdd.com') > 0 OR LENGTH(referer) < 5 THEN 'self'
WHEN INSTR(referer, 'www.google.com') > 0 THEN 'google'
WHEN INSTR(referer, 'www.baidu.com') > 0 THEN 'baidu'
WHEN INSTR(referer, 'www.bing.com') > 0 THEN 'bing'
WHEN INSTR(referer, 'www.so.com') > 0 THEN '360'
ELSE 'other'
END AS ref_type, c_info
FROM access_log_tmp3;
select * from access_log_tmp4 limit 5;

1.7 用戶信息處理

-- 初始化訪問日志表
DROP TABLE IF EXISTS access_log;
CREATE TABLE access_log (
id BIGINT,
ip STRING,
province STRING,
city STRING,
isp STRING,
access_time STRING,
access_hour STRING,
url STRING,
status STRING,
traffic STRING,
referer STRING,
ref_type STRING,
c_info STRING,
client_type STRING,
client_browser STRING
);
-- 使用正則表達式提取客戶端信息中的操作系統和瀏覽器信息并向表插入數據
INSERT OVERWRITE TABLE access_log
SELECT id, ip, province, city, isp, access_time, access_hour, url, status, traffic, referer, ref_type,c_info
,CASE 
WHEN INSTR(c_info, 'iPhone;') > 0 THEN 'IOS'
WHEN INSTR(c_info, 'iPad;') > 0 THEN 'IOS'
WHEN INSTR(c_info, 'Mac OS X ') > 0 THEN 'OS X'
WHEN INSTR(c_info, 'X11;') > 0 THEN 'Linux'
WHEN INSTR(c_info, 'Android ') > 0 THEN 'Android'
WHEN INSTR(c_info, 'Windows NT ') > 0 THEN 'Windows'
ELSE 'other'
END AS client_type
,
CASE 
WHEN INSTR(c_info, ' QQBrowser') > 0 THEN 'QQBrowser'
WHEN INSTR(c_info, ' UCBrowser') > 0 THEN 'UCBrowser'
WHEN INSTR(c_info, ' Edge') > 0 THEN 'Edge'
WHEN INSTR(c_info, ' LBBROWSER') > 0 THEN 'LBBROWSER'
WHEN INSTR(c_info, ' Maxthon') > 0 THEN 'Maxthon'
WHEN INSTR(c_info, ' Firefox') > 0 THEN 'Firefox'
WHEN INSTR(c_info, ' Chrome') > 0 THEN 'Chrome'
WHEN INSTR(c_info, ' Mac OS X') > 0
AND INSTR(c_info, ' Safari') > 0 THEN 'Safari'
WHEN INSTR(c_info, ' MSIE') > 0 THEN 'IE'
ELSE 'other'
END AS client_browser
FROM access_log_tmp4;
select * from access_log limit 5;

1.8.TopN數據采集

-- 初始化訪問鏈接TopN表
DROP TABLE IF EXISTS access_log_url_top;
CREATE TABLE access_log_url_top (
url STRING,
times INT
);
INSERT OVERWRITE TABLE access_log_url_top 
SELECT top.url, top.times
FROM 
(SELECT url, COUNT(*) AS timesFROM access_logGROUP BY url
) top
ORDER BY top.times DESC LIMIT 10;
select * from access_log_url_top limit 5;

獲取每個訪客的第一條訪問日志,腳本如下: UV 獨立訪客 PV:頁面瀏覽量 ?

-- 初始化每個訪客的第一個訪問日志
DROP TABLE IF EXISTS access_log_first;
CREATE TABLE access_log_first (
id BIGINT,
ip STRING,
province STRING,
city STRING,
isp STRING,
access_time STRING,
access_hour STRING,
url STRING,
status STRING,
traffic STRING,
referer STRING,
ref_type STRING,
c_info STRING,
client_type STRING,
client_browser STRING
);
INSERT OVERWRITE TABLE access_log_first 
SELECT a.id, a.ip, a.province, a.city, a.isp, a.access_time, a.access_hour, a.url, a.status, a.traffic, a.referer, a.ref_type, a.c_info, a.client_type, a.client_browser
FROM access_log a
JOIN 
(
SELECT c.ip, MIN(c.id) AS id
FROM access_log c
GROUP BY c.ip,c.c_info
) b 
ON a.ip = b.ip AND a.id = b.id;
select * from access_log_first limit 5;

1.9?IP黑名單處理

-- 初始化訪問IP黑名單表
DROP TABLE IF EXISTS access_log_ip_black;
CREATE TABLE access_log_ip_black (
ip STRING,
times INT
);
INSERT OVERWRITE TABLE access_log_ip_black 
SELECT ip, COUNT(1) AS times
FROM access_log WHERE status = '404'
GROUP BY ip
HAVING COUNT(*) > 10;
select * from access_log_ip_black limit 5;

2 數據分析

2.1 總訪問人次(PV):

select max(id) from access_log;

2.2 訪問人數(UV):

select count(id) from access_log_first;

2.3 獨立IP:

select count(distinct ip) from access_log_first;

2.4 訪問鏈接top10?

select * from access_log_url_top;

2.5 各個時間訪問人次:

select access_hour,count(id) from access_log group by access_hour;

2.6 訪問來源占比:

select rt as name,count(rt) as value 
from (select case ref_type when 'other' then '其他鏈接' when 'self' then '直接訪問' else '搜索引擎' end as rt from access_log_tmp4 
) t group by rt;

2.7 IP黑名單:

select * from access_log_ip_black;

2.8 地域分布:

select province,count(id) from access_log group by province;

2.9 操作系統占比:

select client_type,count(id) from access_log group by client_type;

2.10 瀏覽器占比:

select client_browser,count(id) from access_log group by client_browser;

2.11 網絡運營商占比:

select t.isp, count(t.isp)from
(
select 
case when
INSTR(isp,"移動") > 0 then "移動"
when INSTR(isp,"聯通") > 0 then "聯通"
when INSTR(isp,"電信") > 0 then "電信"
else "其他"
end as isp
from access_log_tmp4 
) t group by t.isp;

2.12 搜索引擎

select ref_type as name,count(ref_type) as value
from access_log_tmp4
group by ref_type
having ref_type != 'self' and ref_type != 'other';

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/88265.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/88265.shtml
英文地址,請注明出處:http://en.pswp.cn/web/88265.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

力扣1310. 子數組異或查詢

這一題很明顯的就是用前綴和異或來解決&#xff0c;只要清楚異或的性質&#xff0c;這一題就十分容易。 對異或的性質的講解如下&#xff1a; 異或運算解析 具體代碼如下&#xff1a; class Solution { public:int sum[30005]; vector<int> xorQueries(vector<int>…

React Native 狀態管理方案全面對比

React Native 狀態管理方案全面對比 在 React Native 開發中&#xff0c;狀態管理是構建復雜應用的核心問題。以下是主流狀態管理方案的深度對比分析&#xff1a; 一、基礎方案&#xff1a;useState/useReducer 適用場景 簡單的組件級狀態中等復雜度的局部狀態管理不需要跨組件…

基于Python的程序員數據分析與可視化系統的設計與實現

文章目錄有需要本項目的代碼或文檔以及全部資源&#xff0c;或者部署調試可以私信博主項目介紹背景意義項目展示總結每文一語有需要本項目的代碼或文檔以及全部資源&#xff0c;或者部署調試可以私信博主 項目介紹 互聯網技術飛速發展&#xff0c;數據分析與可視化在程序員工…

Java 枚舉詳解:從基礎到實戰,掌握類型安全與優雅設計

作為一名Java開發工程師&#xff0c;在日常開發中你一定經常使用枚舉&#xff08;enum&#xff09;。自Java 5引入以來&#xff0c;枚舉已經成為定義固定集合常量的首選方式&#xff0c;它比傳統的 public static final 常量更加類型安全、可讀性強&#xff0c;并且具備面向對象…

海外盲盒系統:技術如何重構“信任經濟”?

盲盒的“非透明性”易引發信任危機&#xff0c;而海外盲盒系統通過技術手段構建了“可感知的公平”&#xff1a;1. 區塊鏈存證&#xff1a;概率透明化 隱藏款概率、抽盒記錄上鏈存證&#xff0c;用戶可隨時查詢歷史數據。某歐美用戶通過區塊鏈瀏覽器驗證&#xff0c;確認系統隱…

PyTorch Tensor 操作入門:轉換、運算、維度變換

目錄 1. Tensor 與 NumPy 數組的轉換 1.1 Tensor 轉換為 NumPy 數組 1.2 NumPy 數組轉換為 Tensor 1.3 獲取單個元素的值 2. Tensor 的基本運算 2.1 生成新 Tensor 的運算 2.2 覆蓋原 Tensor 的運算 2.3 阿達瑪積&#xff08;逐元素乘法&#xff09; 2.4 矩陣乘法 3.…

CompletableFuture使用詳解(Super Detailed)

一、 CompletableFuture介紹 多線程開發一般使用Runnable&#xff0c;Callable&#xff0c;Thread&#xff0c;FutureTask&#xff0c;ThreadPoolExecutor&#xff0c;但也有不近如意的地方 Thread Runnable&#xff1a;執行異步任務&#xff0c;沒有返回結果。 Thread Calla…

開源 Arkts 鴻蒙應用 開發(六)數據持久--文件和首選項存儲

文章的目的為了記錄使用Arkts 進行Harmony app 開發學習的經歷。本職為嵌入式軟件開發&#xff0c;公司安排開發app&#xff0c;臨時學習&#xff0c;完成app的開發。開發流程和要點有些記憶模糊&#xff0c;趕緊記錄&#xff0c;防止忘記。 相關鏈接&#xff1a; 開源 Arkts …

【Bluedroid】藍牙協議棧控制器能力解析與核心功能配置機制(decode_controller_support)

本文圍繞Bluedroid藍牙協議棧中控制器能力解析與核心功能配置的關鍵代碼展開&#xff0c;詳細闡述藍牙協議棧如何通過解析控制器硬件能力&#xff0c;構建 SCO/eSCO、ACL 數據包類型支持掩碼&#xff0c;配置鏈路策略、安全服務、查詢與掃描模式等核心功能。這些機制確保協議棧…

小架構step系列07:查找日志配置文件

1 概述 日志這里采用logback&#xff0c;其為springboot默認的日志工具。其整體已經被springboot封裝得比較好了&#xff0c;扔個配置文件到classpath里就能夠使用。 但在實際使用中&#xff0c;日志配置文件有可能需要進行改動&#xff0c;比如日志的打印級別&#xff0c;平…

一文講清楚React Hooks

文章目錄一文講清楚React Hooks一、什么是 React Hooks&#xff1f;二、常用基礎 Hooks2.1 useState&#xff1a;狀態管理基本用法特點2.2 useEffect&#xff1a;副作用處理基本用法依賴數組說明2.3 useContext&#xff1a;上下文共享基本用法特點三、其他常用 Hooks3.1 useRed…

Apache http 強制 https

1. 修改一下文件配置 sudo nano /etc/apache2/sites-enabled/000-default.conf<VirtualHost *:80>ServerName hongweizhu.comServerAlias www.hongweizhu.comServerAdmin webmasterlocalhostDocumentRoot /var/www/html# 強制重定向到HTTPSRewriteEngine OnRewriteCond …

【讀代碼】GLM-4.1V-Thinking:開源多模態推理模型的創新實踐

一、基本介紹 1.1 項目背景 GLM-4.1V-Thinking是清華大學KEG實驗室推出的新一代開源視覺語言模型,基于GLM-4-9B-0414基礎模型構建。該項目通過引入"思維范式"和強化學習課程采樣(RLCS)技術,顯著提升了模型在復雜任務中的推理能力。其創新點包括: 64k超長上下文…

從代碼生成到智能運維的革命性變革

AI大模型重塑軟件開發&#xff1a;從代碼生成到智能運維的革命性變革 希望對大家有一定的幫助&#xff0c;進行參考 目錄AI大模型重塑軟件開發&#xff1a;從代碼生成到智能運維的革命性變革 希望對大家有一定的幫助&#xff0c;進行參考一、范式轉移&#xff1a;軟件開發進入&…

豆包編寫Java程序小試

今天下載了一本第四版電氣工程師手冊&#xff0c;非常棒的一本書&#xff0c;在給PDF添加目錄的時候&#xff0c;由于目錄有將近60頁&#xff0c;使用老馬開發的PdgCntEditor有點卡頓&#xff0c;不過補充下&#xff0c;老馬這個PdgCntEditor還是非常好的。所以我決定用Java編一…

SpringBoot整合騰訊云新一代行為驗證碼

一 產品介紹 騰訊云官方介紹鏈接 騰訊云新一代行為驗證碼&#xff08;Captcha&#xff09;&#xff0c;基于十道安全防護策略&#xff0c;為網頁、App、小程序開發者打造立體、全面的人機驗證。在保護注冊登錄、活動秒殺、點贊發帖、數據保護等各大場景下業務安全的同時&…

SenseGlove新一代外骨骼力反饋手套Rembrand來襲!亞毫米級手部動捕+指尖觸覺力采集+5Dof主動力反饋多模態

在遠程機器人操作領域&#xff0c;精準的觸覺感知與靈活的動作控制始終是核心需求。SenseGlove 新推出的 Rembrandt 力反饋外骨骼數據手套&#xff0c;以先進技術為支撐&#xff0c;為遠程操控人形機器人手部提供了無縫解決方案&#xff0c;讓操作更精準、更高效。值得一提的是…

Linux 信號機制:操作系統的“緊急電話”系統

想象一下&#xff0c;你正在電腦前專心工作&#xff0c;突然手機響了——這是一個通知&#xff0c;要求你立即處理一件新事情&#xff08;比如接電話&#xff09;。 Linux 系統中的信號&#xff08;Signal&#xff09;?? 機制&#xff0c;本質上就是操作系統內核或進程之間用…

論文略讀:Prefix-Tuning: Optimizing Continuous Prompts for Generation

2021 ACL固定預訓練LM&#xff0c;為LM添加可訓練&#xff0c;任務特定的前綴這樣就可以為不同任務保存不同的前綴這種前綴可以看成連續可微的soft prompt&#xff0c;相比于離散的token&#xff0c;更好優化&#xff0c;效果更好訓練的時候只需要更新prefix部分的參數&#xf…

CSS基礎選擇器、文本屬性、引入方式及Chorme調試工具

CSS基礎 1.1 CSS簡介 CSS 是層疊樣式表 ( Cascading Style Sheets ) 的簡稱. 有時我們也會稱之為 CSS 樣式表或級聯樣式表。 CSS 是也是一種標記語言 CSS 主要用于設置 HTML 頁面中的文本內容&#xff08;字體、大小、對齊方式等&#xff09;、圖片的外形&#xff08;寬高、邊…