ç¬å¦ã¨ã³ã¸ãã¢ã®èª²é¡ã¨ãã¦ããã°è§£æã·ã¹ãã ãä½ã£ãã Step1. 製ä½ç©ã®ç¢ºèª
ãã®è¨äºã¯ã つながる勉強会 Advent Calendar 2022 - Adventar ãã® 6 æ¥ç®ã®è¨äºã§ãã
åæ¥ã¯ããããããï¼@yoshitaro_yoyoï¼ ããã§ããï¼
ããï¼ã¶æã§å¦ãã ãã¨ã¨ã®ãã¨ã§ããæ·±ããã¦ã¤ãããã§ããï¼ææ³ãæµ ãã¦ã¹ããã»ã³ï¼
ããã«ã¡ã¯ï¼
ã¹ãã¬ã¸ã»ããã¯ãã¡ã¼ã ã® Webã¨ã³ã¸ã㢠ãã¾ã¦ ã¨ç³ãã¾ãã
ã¯ããã«
ä»åãããç¬å¦ã¨ã³ã¸ãã¢ã®èª²é¡ã¨ãã¦ãä½ã£ãããã°è§£æã·ã¹ãã ã«ã¤ãã¦ãå®æã¾ã§ã®éã®ããæ稿ãã¾ãã
GitHub ã® Public ãªãã¸ããªã¨ãã¦ãé²è¦§ã§ãã¾ãã
ãç¬å¦ã¨ã³ã¸ãã¢ãã¨ã¯ãWebéçºï¼ä¸»ã«ãµã¼ãã¼ãµã¤ãï¼ã®åç»å¦ç¿ææã§ãã
ç®ç
ãã°è§£æã·ã¹ãã ãä½æããç®çã¯ãã½ããã¦ã§ã¢ã¨ã³ã¸ãã¢ãªã³ã°ã«ããã¦å®éã«ããç»å ´ããã¿ã¹ã¯ã®ä¸ã¤ã§ããããã°ãã¼ã¿ã解æãå®éã«ãã£ã¦ã¿ããã¨ã§ããã¼ã¿ãã¼ã¹ã¨SQLã¹ãã«ã®åä¸ãç®æããã¨ããç®çã§ãã
å ·ä½çã«ã¯ã以ä¸ã®ãã¼ã¿ãã¼ã¹ã¨SQLã¹ãã«ã®åä¸ãç®æãã¦ãã¾ãã
- ãã¼ã¿ãã¼ã¹ã¨ãã¼ãã«ãä½æãã
- ãã¼ãã«å ã«ãã¼ã¿ãä¿åãã
- SELECTæã§æ¤ç´¢ãã
- ããã°ã©ã ããSQLãæä½ãã
æ¦è¦
Wikipedia ã®ã¢ã¯ã»ã¹ãã°ã®æ å ±ã§ãã pageviews ãã¡ã¤ã«ãå©ç¨ãã¦ãã¢ã¯ã»ã¹ãã°ã解æããã·ã¹ãã ã§ãã
ãã¢ã¯ã»ã¹ãã°ã解æãããã¨ã¯ãã¢ã¯ã»ã¹ãã°ã®æ å ±ã DB ã«åãè¾¼ãã§ãã³ãã³ãã©ã¤ã³ããå®è¡ãã¦ãSQL ã®æä½ã§ããã決ã¾ã£ãæ¡ä»¶ã®ãã¼ã¿ãåå¾ãããã¨ããç°¡åãªãã®ã§ãã
ã¢ã¯ã»ã¹ãã°ã«ã¯ãèªè ãã¦ã§ããã¼ã¸ãèªã¿è¾¼ããã³ã«ã©ã®ãã¼ã¸ãèªã¿è¾¼ã¾ããããè¨é²ããã¦ãã¾ãããããã®æ å ±ã使ã£ã¦ããµã¤ãã®ã¦ã¼ã¶ã¼ã®æ´»åã«é¢ãã質åã«çããã·ã¹ãã ãä½æãã¾ãã
以ä¸ã®é ã«æ稿ãã¾ãã
- Step1. 製ä½ç©ã®ç¢ºèª
- Step2. DBã®ãã¼ãã«ä½æ
- Step3. ãã¼ã¿ã®ã¤ã³ãã¼ã
- Step4. SELECT æã§ã®æ¤ç´¢
- Step5. ããã°ã©ã ããã®SQLã®æä½â
- Step6. ããã°ã©ã ããã®SQLã®æä½â¡
ä»å㯠Step1 ã¨ãã¦ã 製ä½ç©ã®ç¢ºèª ããã¾ãã
å®æããææç©ã«ã¤ãã¦ãä½ããããã®ãã使ãæ¹ãªã©ãã¾ã¨ãã¦ãã©ã®ãããªãã®ãä½æãããã®ã¤ã¡ã¼ã¸ããä¼ããããã¨æãã¾ãã
ã¾ãã Step2 ã Step5 ã§ã¯ãä½ææé ã§ã®ãã¤ã³ããã¤ã¾ã¥ããç¹ãã¾ã¨ãã¾ãã
ç®æ¬¡
- ã¯ããã«
- 1. Wikipedia ã®ã¢ã¯ã»ã¹ãã°ã®ãã¼ã¿ã«ã¤ãã¦
- 2. Wikipedia ãã°è§£æã·ã¹ãã ã§ã§ãããã¨
- 3. ããã°ã©ã ã使ãæºå
- 4. ããã°ã©ã ã®ä½¿ãæ¹
- 5. åæ¢ã®æ¹æ³
- ãããã«
ç°å¢
ãã¼ã¸ã§ã³ | |
---|---|
macOS Big Sur | 11.6 |
Docker Desktop | 4.7.0 |
PHP | 8.1.7 |
MySQL | 8.0.28 |
1. Wikipedia ã®ã¢ã¯ã»ã¹ãã°ã®ãã¼ã¿ã«ã¤ãã¦
以ä¸ãWikipedia ã®ã¢ã¯ã»ã¹ãã°ã®ãã¼ã¿ã®ãã¦ã³ãã¼ãã¨ããã¼ã¿å½¢å¼ã«ã¤ãã¦ã§ãã
1-1. ãã¼ã¿ã®ãã¦ã³ãã¼ã
Wikipedia ã®ã¢ã¯ã»ã¹ãã°ã®ãã¼ã¿ã¯ä¸è¨ URL ãããã¦ã³ãã¼ããã¾ãã
ãã¼ã¿ã«ã¤ãã¦ã®å ¨ä½ã®è§£èª¬ã¯ãä¸è¨URLã«ã¦è¡ããã¦ããã®ã§ãåç §ãã ããã
ãã¦ã³ãã¼ããããã¼ã¿ã®ãã¼ãã«å®ç¾©ã¯ä¸è¨URLã§è§£èª¬ããã¦ããã®ã§ãåç §ãã ããã
1-2. ãã¼ã¿å½¢å¼
ãã¦ã³ãã¼ãã§ãããã¼ã¿ã®å½¢å¼ã¯ä»¥ä¸ã®ãããªå½¢å¼ã§ãã
domain_code | page_title | count_views | total_response_size |
---|---|---|---|
ãã¡ã¤ã³ã³ã¼ã | ãã¼ã¸ã¿ã¤ãã« | åæéã®ãã¼ã¸è¡¨ç¤ºåæ° | åè¨ã¬ã¹ãã³ã¹ãµã¤ãº |
aa | Main_Page | 4 | 0 |
aa | Wikipedia | 1 | 0 |
pageviews-20211201-000000.gz ããã¦ã³ãã¼ããã¦ãã¡ã¤ã«ãéãã¨ã以ä¸ã®ãããªãã¡ã¤ã«ã§ãã
aa Main_Page 4 0 aa Wikipedia 1 0 aa Wikipedia:Statistics 1 0 aa.b Main_Page 1 0 aa.d Main_Page 4 0 aa.m Main_Page 1 0 ab - 3 0 ab 1025 1 0 ab 1036 1 0 ab 1121 1 0 ab 1146 1 0 ab 1148 2 0 ab 1177 2 0 ab 1261 1 0 ab 1267 1 0 ab 1791 1 0 ab 1795 1 0 ab 1886 1 0 ab 1913 1 0 ab 1919 1 0 ab 1989 1 0 ab 338 1 0 ab 589 1 0 ab 984 1 0 ab Ðвикипедиа 1 0 ab Ðвикипедиа:ÐÑоÑÑм 12 0 ...
å¤ã®éã¯ã¹ãã¼ã¹åºåãã§ãã¹ã¯ãã¼ã«ãã¦ãä¸ã¾ã§è¾¿ãçãã®ã大å¤ãªãããã®è¡æ°ã®ã¢ã¯ã»ã¹ãã°ã«ã¤ãã¦ã®ãã¼ã¿ãæ¸ãã¦ããã¾ãã
2. Wikipedia ãã°è§£æã·ã¹ãã ã§ã§ãããã¨
ã§ãããã¨ã¯ä»¥ä¸ã®äºã¤ã§ãã
2-1. æããã¥ã¼æ°ã®å¤ããã¼ã¸ã®è¡¨ç¤º
æããã¥ã¼æ°ã®å¤ãè¨äºããæå®ããè¨äºæ°åã ããã¥ã¼æ°ãå¤ãé ã«ã½ã¼ããããã¡ã¤ã³ã³ã¼ãã¨ãã¼ã¸ã¿ã¤ãã«ããã¥ã¼æ°ã表示ãã
ï¼ä¾ï¼ã³ãã³ãã©ã¤ã³ä¸ã§2è¨äºã¨æå®ããå ´åãä¸è¨ã表示ãã
âenâ, âMain_Pageâ, 120 âenâ, âWikipedia:Umnyango_wamgwamandaâ, 112
2-2. ãã¡ã¤ã³ã³ã¼ãã®äººæ°é ã®è¡¨ç¤º
æå®ãããã¡ã¤ã³ã³ã¼ãã«å¯¾ãã¦ã人æ°é ã«ã½ã¼ããããã¡ã¤ã³ã³ã¼ãåã¨åè¨ãã¥ã¼æ°ã表示ãã
ï¼ä¾ï¼ã³ãã³ãã©ã¤ã³ä¸ã§ãen deãã¨æå®ããå ´åãä¸è¨ã表示ãã
âenâ, 10700 âdeâ, 5300
3. ããã°ã©ã ã使ãæºå
以ä¸ãããã°ã©ã ã使ãæºåã®æé ã§ãã
3-1. Docker ã³ã³ããã®æºå
Docker ã³ã³ãããçæã»èµ·åãã¾ãã
# Docker ã³ã³ããã®çæ docker compose build # Docker ã³ã³ããã®èµ·å docker compose up -d
3-2. DBã®ãã¼ãã«ä½æ
DBã®ãã¼ãã«ä½æãã¾ãã
docker compose exec app php databases/initialize_page_views_table.php
3-3. ãã¼ã¿ã®ãã¦ã³ãã¼ã
Wikipedia ã®ã¢ã¯ã»ã¹ãã°ã®ãã¼ã¿ã¯ä¸è¨URLãããã¦ã³ãã¼ããã¾ãã
ãã¦ã³ãã¼ããããã¡ã¤ã«ã¯è§£åããç¶æ ã§ã databases ãã£ã¬ã¯ããªã«ç§»åãã¾ãã
ãã¡ã¤ã«åã¯ã page_views ã«å¤æ´ãã¾ãã ï¼ã¤ã³ãã¼ãã®éã«ãã¼ãã«åã¨æãã¦ããå¿ è¦ãããã¾ãï¼
3-4. ãã¼ã¿ã®ã¤ã³ãã¼ã
Docker ã® db ã³ã³ããã® MySQL ã«ãroot
ã¦ã¼ã¶ã¼ã§ãã°ã¤ã³ãã¾ãã
docker compose exec db mysql -p
å®è¡ããã¨ãã¹ã¯ã¼ããèãããã®ã§ã root
ã¦ã¼ã¶ã¼ã®ãã¹ã¯ã¼ã pass
ãå
¥åãã¾ãã
ä¸è¨ SQL ãå®è¡ãã¦ã local-infile
 ã ON
 ã«è¨å®ãã¾ãã
mysql> SET GLOBAL local_infile=ON;
GRANT
æ§æã§ãã¦ã¼ã¶ã¼æ¨©éã SUPER
ã«æå®ãã¾ãã
mysql> GRANT SUPER ON *.* To test_user@'%';
mysql> quit
ããã¹ããã¡ã¤ã«ãã MySQL ã®ãã¼ãã«ã¸ã®ãã¼ã¿ã¤ã³ãã¼ããã¾ãã
docker compose exec app mysqlimport -h db -u test_user -p -d --fields-terminated-by=' ' --local test_database databases/page_views
å®è¡ããã¨ãã¹ã¯ã¼ããèãããã®ã§ã test_user
ã®ãã¹ã¯ã¼ã pass
ãå
¥åãã¾ãã
4. ããã°ã©ã ã®ä½¿ãæ¹
ã2. Wikipedia ãã°è§£æã·ã¹ãã ã§ã§ãããã¨ãã®ã2-1. æããã¥ã¼æ°ã®å¤ããã¼ã¸ã®è¡¨ç¤ºãã2-2. ãã¡ã¤ã³ã³ã¼ãã®äººæ°é ã®è¡¨ç¤ºãã®ä½¿ãæ¹ã«ã¤ãã¦ã§ãã
4-1. æããã¥ã¼æ°ã®å¤ããã¼ã¸ã®è¡¨ç¤º
æããã¥ã¼æ°ã®å¤ãè¨äºããæå®ããè¨äºæ°åã ããã¥ã¼æ°ãå¤ãé ã«ã½ã¼ããããã¡ã¤ã³ã³ã¼ãã¨ãã¼ã¸ã¿ã¤ãã«ããã¥ã¼æ°ã表示ãã
ä¸è¨ã³ãã³ããå®è¡ãã¾ãã
docker compose exec app php most_viewed_pages.php 20
most_viewed_pages.php ã®å¾ã«åè§ã¹ãã¼ã¹ãéãã¦ãæå®ãããã¼ã¸æ°ãå ¥åãã¾ãã
# r_yamate @ mbp in ~/Documents/code/wikipedia-log-analysis-tool on git:feature/create-program x [7:25:16] $ docker compose exec app php most_viewed_pages.php 20 "en.m", "Main_Page", 122058 "en", "Main_Page", 69181 "en", "Special:Search", 26630 "de", "Wikipedia:Hauptseite", 20739 "en.m", "Special:Search", 19119 "ja", "ã¡ã¤ã³ãã¼ã¸", 18475 "es.m", "Wikipedia:Portada", 15335 "es", "Wikipedia:Portada", 15261 "fr", "Wikipédia:Accueil_principal", 14744 "thankyou", "Thank_You/en", 13449 "ru", "ÐаглавнаÑ_ÑÑÑаниÑа", 13336 "en", "Lotfi_A._Zadeh", 12864 "it", "Pagina_principale", 12731 "zh", "Wikipedia:é¦é¡µ", 10782 "pt", "Wikipédia:Página_principal", 10485 "de.m", "Wikipedia:Hauptseite", 10386 "ja.m", "ã¡ã¤ã³ãã¼ã¸", 9421 "en", "Bible", 9024 "fr.m", "Wikipédia:Accueil_principal", 8705 "en", "-", 7227
4-2. ãã¡ã¤ã³ã³ã¼ãã®äººæ°é ã®è¡¨ç¤º
æå®ãããã¡ã¤ã³ã³ã¼ãã«å¯¾ãã¦ã人æ°é ã«ã½ã¼ããããã¡ã¤ã³ã³ã¼ãåã¨åè¨ãã¥ã¼æ°ã表示ãã
ä¸è¨ã³ãã³ããå®è¡ãã¾ãã
docker compose exec app php popular_domain_codes.php en de ja
popular_domain_codes.php ã®å¾ã«åè§ã¹ãã¼ã¹ãéãã¦ãæå®ãããã¡ã¤ã³ã³ã¼ãã 2 ã¤ä»¥ä¸å ¥åãã¾ãã
# r_yamate @ mbp in ~/Documents/code/wikipedia-log-analysis-tool on git:feature/create-program x [7:35:12] C:16 $ docker compose exec app php popular_domain_codes.php en de ja "en", 3556081 "ja", 367924 "de", 284178
5. åæ¢ã®æ¹æ³
Docker ã³ã³ãããåæ¢ãã¾ãã
docker compose stop
ãããã«
ä»åã¯ãç¬å¦ã¨ã³ã¸ãã¢ã®èª²é¡ã¨ãã¦ããã°è§£æã·ã¹ãã ãä½ã£ãããã¨ããã¿ã¤ãã«ã§ã課é¡ã®è£½ä½ç©å®æã¾ã§ã®éã®ã Step1 ã¨ãã¦ã 製ä½ç©ã®ç¢ºèª ããã¾ããã
次åã¯ã Step2. DBã®ãã¼ãã«ä½æ ã«ã¤ãã¦æ稿ãã¾ãã
ãããã¨ããããã¾ããã
Qiitan ã®ã¬ãããã¿ãããããã«ãä»æ¥ããã®25æ¥éãæ¯æ¥1è¨äºæ稿ãã¾ããhttps://t.co/UDFWJmGm8K
— ãã¾ã¦ï½Webã¨ã³ã¸ãã¢2å¹´ç® (@r_yamate) 2022å¹´11æ30æ¥
ä¸äººã§ 25 è¨äºãæ稿ããã¢ããã³ãã«ã¬ã³ãã¼ããã£ã¦ããã¾ãã