--- title: 南大_大數據應用(2021/09/01-2022/01/31) tags: 大數據分析,Hadoop description: 本門課的重點在輔導學生了解大數據的歷史由來及發展現狀,分享大數據零售行業、電信行業、互聯網行業相關企業的真實案例。 lang: zh --- # 大數據應用 ###### tags: 大數據分析,bigdata,opendata,hadoop ## 重要課程規定 ### 評分方式 1. 期中考:30% 2. 期末分組報告:40%(書面:20% 口頭:20%) 3. 平時成績(出席、課堂表現、作業、報告):30% 4. [額外全國競賽加分](https://gec2021.twisc.ncku.edu.tw/):<font color="red">依課程說明</font> ## 課程大綱 本門課將以資安議題為出發角度,教導學生如何利用現有的網路相關log發掘出潛在的資安問題,並由淺入深教導學生如何看懂相關資料,並做出對應的分析結果。 ## 相關使用軟體 1. Excel 2. Apache Hadoop Pig、Hive 3. Wireshark 封包分析 ## 課程重要行程 1. 期中考:110年11月05日。 2. 全國競賽初賽(線上競賽):<font color="red">(待大會公告)</font>。 3. 期末報告:111月01月 <font color="red">(暫定)</font>。 ## 課程相關線上教材 1. 電子書:[大數據(Big Data)分析與應用:使用Hadoop與Spark](https://nutn.ebook.hyread.com.tw/bookDetail.jsp?id=160971) 2. 線上影音教學: [Hadoop 線上課程](https://www.youtube.com/watch?v=I4esbC7ipbk&list=PLUuQL_ogRVuixcTdE9Q0XhvWz6l3p9xbC) 3. 課程雲端分享區: [連結1](https://drive.google.com/drive/folders/1Q-T60MNvFxRm_4_K9yUQ2GR1M_fi0sEA?usp=sharing) ## 初探大數據課程 userprofile.tsv (會員註冊資料) | 欄位名稱 | 描述 | 型態 | | -------- | -------- | -------- | | userid | 使用者 ID | chararray | | gender | 性別 | chararray | | age | 年齡 | int | | country | 國家 | chararray | | registered | 註冊日期 | chararray | userdemand.tsv (會員點播紀錄) | 欄位名稱 | 描述 | 型態 | | -------- | -------- | -------- | | userid | 使用者 ID | chararray | | time | 點聽時間 | chararray | | artname | 歌手名稱 | chararray | | traname | 歌曲名稱 | chararray | ### 2021/09/24 課堂練習題目 (基礎-Excel) #### 練習1 男性與女性的會員數分別是多少 :::spoiler 參考答案 m 182 f 137 ::: #### 練習2 來自加拿大(Canada)的會員數分別是多少? :::spoiler 參考答案 60 ::: #### 練習3 來自加拿大(Canada)的女性會員數分別是多少? :::spoiler 參考答案 31 ::: #### 練習4 來自加拿大(Canada)***或***女性會員數分別是多少? :::spoiler 參考答案 ::: ### 2021/10/01 課堂練習題目 (基礎-Excel) #### 練習1 男性會員或來自於德國的會員數是多少 :::spoiler 參考答案 191 ::: #### 練習2 來自於德國的男性會員數是多少 :::spoiler 參考答案 26 ::: #### 練習3 介於20~30歲(包含20與30)的會員人數有多少人? :::spoiler 參考答案 212 ::: #### 練習4 請查詢低於平均會員年齡的會員人數? :::spoiler 參考答案 202 ::: ### 2021/10/08 課堂練習題目 (基礎-Excel) #### 練習1 請根據資料集查詢出所有會員資料表中,會員數量最多的國家為哪個為家,請顯示國家名稱與會員數量 :::spoiler 參考答案 Canada 60 ::: #### 練習2 請根據資料集查詢出所有會員資料表中,會員數量最多的年齡是幾歲 :::spoiler 參考答案 21 35 ::: #### 練習3 請問在資料集中,哪個月份出生的會員人數最多,請顯示月份與會員人數 :::spoiler 參考答案 12 39 ::: #### 練習4 請問在資料集中,哪個國家的會員平均年齡最小,請顯示國家與平均會員年齡 :::spoiler Croatia 18 Venezuela 18 ::: #### 練習5 查詢會員人數第二多的國家是哪個國家? 並顯示出該國男性會員與女性會員分別是多少人 :::spoiler germany 26 9 ::: #### 練習6 哪個國家的男性會員人數最多,請顯示國家名稱與男性會員人數 :::spoiler Canada 29 ::: ### 2021/10/15 課堂練習 練習1 男性與女性的會員數分別是多少 練習2 來自於加拿大(Canada)的男性會員人數 練習3 來自於加拿大(Canada)或男性會員人數 練習4 年齡介於20~30歲的會員人數 練習5 請查詢高於平均會員年齡的會員人數 練習6 請查詢年齡最多的前名五分別是幾歲與人數為何 練習7 請根據資料集查詢出所有會員資料表中,那些國家的男性會員註冊人數是最多的,請顯示國家名稱與註冊人數 題目8 哪個國家的平均年齡最輕 ### 2021/10/22 課堂練習 #### 練習1 在點播紀錄中,哪一位歌手的點播紀錄最高 :::spoiler ::: #### 練習2 在點播紀錄中,哪一首歌曲的點播紀錄最高 :::spoiler ::: #### 練習3 哪一首歌曲被最多歌手所翻唱 :::spoiler Intro 102 ::: #### 練習4 查詢哪一位歌手所唱的歌曲點播次數最高(同一個使用者點播僅可計算一次) :::spoiler ::: ### 2021/10/29 課堂練習 #### 練習1 哪一個使用者點播次數最多 :::spoiler ::: #### 練習2 哪一個使用者點播次數最多,並查詢出他來自於哪個國家 :::spoiler ::: #### 練習3 哪一個國家累計的點播次數最高,請顯示排名前五名的國家與點播次數 :::spoiler ::: #### 練習4 哪一個國家的男性點播次數最高,請顯示國家名稱與次數 :::spoiler ::: ### 2021/12/17 C# 程式內容 #### 奇偶數判斷 ```csharp= class Program { static void Main(string[] args) { Console.Write("請輸入一個整數:"); String userIn = Console.ReadLine(); //將文字轉為整數 int userNumber = Int32.Parse(userIn); //判斷是否為偶數 if (userNumber % 2 == 0) { Console.WriteLine(String.Format("{0} 是一個偶數",userIn)); } else { Console.WriteLine(String.Format("{0} 是一個奇數", userIn)); } Console.ReadKey(); } } ``` #### 網路取得資料 ```csharp= static async void GetData() { //修正連線意外中斷問題 ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; //要查詢的網址 string Url = "https://datacenter.taichung.gov.tw/swagger/OpenData/537fca8f-4fc7-495f-b33f-114ba60e26f8"; //建立一個HttpClient的物件(類似瀏覽器) HttpClient client = new HttpClient(); //命令瀏覽器發出一個請求,取得某個網頁的內容 String webData = await client.GetStringAsync(Url); Console.WriteLine(webData); } static void Main(string[] args) { GetData(); Console.ReadKey(); } ``` #### 取得並整理資料 ```csharp= static async void GetData() { //修正連線意外中斷問題 ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; //要查詢的網址 string Url = "https://datacenter.taichung.gov.tw/swagger/OpenData/537fca8f-4fc7-495f-b33f-114ba60e26f8"; //建立一個HttpClient的物件(類似瀏覽器) HttpClient client = new HttpClient(); //命令瀏覽器發出一個請求,取得某個網頁的內容 String webData = await client.GetStringAsync(Url); //將原始資料轉換為逐筆資料並放入陣列中 String[] dataArray = webData.Split('\n'); for (int i =1; i < 10; i++) { //分割單筆資料並取得各個欄位 String[] data = dataArray[i].Split(','); string year = data[0]; string month = data[1]; string day = data[2]; string hour = data[3]; string min = data[4]; string death = data[7]; string injury = data[8]; Console.WriteLine(String.Format("{0},{1},{2} {3}:{4} 死亡人數:{5} 受傷人數:{6}", year,month,day,hour,min, death, injury)); Console.WriteLine("-------------"); } } ``` ### 期末報告範例 #### 需要引用的外掛套件 ![](https://i.imgur.com/GQPnBQy.png) #### 基礎範例程式碼 ```csharp= static async void ExecuteWebResult() { //修正連線意外中斷問題 ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; //要查詢的網址 string Url = " https://data.cip.gov.tw/API/v1/dump/datastore/A53000000A-110067-001"; //建立一個HttpClient的物件(類似瀏覽器) HttpClient client = new HttpClient(); //命令瀏覽器發出一個請求,取得某個網頁的內容 String webData = await client.GetStringAsync(Url); //將文字轉換為JSON格式 dynamic jsonObject = JsonConvert.DeserializeObject(webData); //取得真實資料的結果 dynamic results = jsonObject[0].result.records; //利用迴圈取得相關資料 foreach (dynamic result in results) { Console.WriteLine("認證日期:{0} 認證語言:{1} 報名人數:{2} 到考人數:{3} 通過人數:{4}", result.DateListed, result.族語方言別, result.報名人數, result.應試人數, result.通過人數 ); Console.WriteLine("-----------------------"); } } ``` ### 期末報告範例 ```csharp= static async void ExecuteWebResult() { //修正連線意外中斷問題 ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; //要查詢的網址 string Url = " https://data.cip.gov.tw/API/v1/dump/datastore/A53000000A-110067-001"; //建立一個HttpClient的物件(類似瀏覽器) HttpClient client = new HttpClient(); //命令瀏覽器發出一個請求,取得某個網頁的內容 String webData = await client.GetStringAsync(Url); //將文字轉換為JSON格式 dynamic jsonObject = JsonConvert.DeserializeObject(webData); //取得真實資料的結果 dynamic results = jsonObject[0].result.records; //題目一:查詢哪些場次的報名人數超過1000人 Console.WriteLine("查詢哪些場次的報名人數超過1000人"); int Q1Count = 0; foreach (dynamic result in results) { if ((int)(result.報名人數) > 1000) { Console.WriteLine("認證日期:{0} 認證語言:{1} 報名人數:{2} 到考人數:{3} 通過人數:{4}", result.DateListed, result.族語方言別, result.報名人數, result.應試人數, result.通過人數 ); Q1Count++; } } Console.WriteLine("總共有{0}場次的報名人數超過1000人", Q1Count); Console.WriteLine("-------------------"); //題目二:那些場次的到考率超過85% Console.WriteLine("查詢哪些場次的出席率低於70%"); int Q2Count = 0; foreach (dynamic result in results) { //計算到考率 double presenceRate = (double)result.應試人數 / (double)result.報名人數; if (presenceRate < 0.7) { Console.WriteLine("認證日期:{0} 認證語言:{1} 報名人數:{2} 到考人數:{3} 通過人數:{4} 到考率:{5}", result.DateListed, result.族語方言別, result.報名人數, result.應試人數, result.通過人數, Math.Round(presenceRate * 100,2) ); Q2Count++; } } Console.WriteLine("總共有{0}場次的出席率低於70%", Q2Count); Console.WriteLine("-------------------"); } ```