第1關:獲取工作簿中的數(shù)據(jù)
任務描述
本關任務:獲取data.xls
文件中的數(shù)據(jù)。
相關知識
獲取工作簿中的信息,我們可以使用Java POI
(POI
是一個提供API
給Java
程序?qū)?code>Microsoft Office格式檔案讀和寫的功能)提供的Workbook
類來操作。
為了完成本關任務,你需要掌握:如何獲取Wookbook
的數(shù)據(jù)。
編程要求
在右側(cè)編輯器 Begin-End
中補充代碼,獲取 data.xls
文件中的數(shù)據(jù),具體獲取以下數(shù)據(jù)并將結(jié)果打?。?trip_id
、開始時間、結(jié)束經(jīng)度、車輛 id
。
package com.educoder.savedata;
import java.io.InputStream;
import java.text.DecimalFormat;
import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class SaveWookbook {
public static void main(String[] args) throws Exception {
/********** Begin **********/
//1.通過類加載器獲取本地文件并新建一個工作簿
InputStream resourceAsStream = SaveData.class.getClassLoader().getResourceAsStream("data.xls");
Workbook workbook = WorkbookFactory.create(resourceAsStream);
//2.拿到工作簿中第一個Sheet
Sheet sheet = workbook.getSheetAt(0);
//3.獲取當前Sheet中的行數(shù)
int rows = sheet.getPhysicalNumberOfRows();
//4.對所有有效數(shù)據(jù)進行遍歷并輸出(期間無效數(shù)據(jù)通過異常捕獲方式清除)
for(int n=1;n<rows;n++)
{
Row row = sheet.getRow(n);
//通過異常方式清除格式不準確、數(shù)據(jù)不存在的無效行
try{
DecimalFormat formatter1 = new DecimalFormat("########");
String trip_id = formatter1.format(row.getCell(0).getNumericCellValue());
//開始時間
FastDateFormat instance = FastDateFormat.getInstance("MM/dd/yyyyHH:mm");
String beginTimeValue = row.getCell(1).getStringCellValue();
long begintime = instance.parse(beginTimeValue).getTime();
//車輛id
int car_id = (int)row.getCell(3).getNumericCellValue();
//結(jié)束經(jīng)度
double start_longitude = row.getCell(9).getNumericCellValue();
DecimalFormat formatter2 = new DecimalFormat("###.######");
String longitude = formatter2.format(start_longitude);
System.out.println("騎行id:"+trip_id+",開始時間:"+begintime+",車輛id:"+car_id+",結(jié)束經(jīng)度:"+longitude);
}catch(Exception e){
}
}
/******** ** End ******* ***/
}
}
第2關:保存共享單車數(shù)據(jù)
任務描述
本關任務:從dataResources.xls
文件中獲取共享單車數(shù)據(jù),保存到HBase
中。
相關知識
為了完成本關任務,你需要掌握:文章來源:http://www.zghlxwxcb.cn/news/detail-497940.html
- 如何創(chuàng)建
HBase
表; - 如何讀取文件;
- 了解共享單車數(shù)據(jù)表格式以及如何獲取數(shù)據(jù);
- 如何存儲到
HBase
。
編程要求
請補全右側(cè) Begin-End
之間的代碼,分別獲取攜程網(wǎng)所有帶有 href
的 link
標簽、第一次出現(xiàn) class
為 "pop_attention"
的 div
(可用 DOM
或 CSS
二種方式實現(xiàn))、以及所有 li
之后的 i
標簽。文章來源地址http://www.zghlxwxcb.cn/news/detail-497940.html
package com.educoder.savedata;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import com.educoder.util.HBaseUtil;
/*
* 讀取共享單車城市行車數(shù)據(jù)
*
*/
public class SaveData {
public static void SaveBicycleData() throws Exception {
/******** ** Begin ******* ***/
HBaseUtil.createTable("t_shared_bicycle", "info");
InputStream resourceAsStream = SaveData.class.getClassLoader().getResourceAsStream("dataResources.xls");
Workbook workbook = WorkbookFactory.create(resourceAsStream);
Sheet sheet = workbook.getSheetAt(0);
int rows = sheet.getPhysicalNumberOfRows();
List<Put> puts = new ArrayList<Put>();
for (int n = 1; n < rows; n++) {
// 通過異常方式清除格式不準確、數(shù)據(jù)不存在的無效行
try {
Row row = sheet.getRow(n);
// 唯一騎行id,當作行rowkey
DecimalFormat formatter1 = new DecimalFormat("########");
String trip_id = formatter1.format(row.getCell(0).getNumericCellValue());
Put put = new Put(Bytes.toBytes(trip_id));
byte[] family = Bytes.toBytes("info");
// 開始時間
FastDateFormat instance = FastDateFormat.getInstance("MM/dd/yyyy HH:mm");
String beginTimeValue = row.getCell(1).getStringCellValue();
Date parse = instance.parse(beginTimeValue);
put.addColumn(family, Bytes.toBytes("beginTime"),Bytes.toBytes(String.valueOf(parse.getTime())));
// 結(jié)束時間
String endTimeValue = row.getCell(2).getStringCellValue();
Date parse2 = instance.parse(endTimeValue);
put.addColumn(family, Bytes.toBytes("endTime"),Bytes.toBytes(String.valueOf(parse2.getTime())));
// 單車識別碼
int bicycleId = (int)row.getCell(3).getNumericCellValue();
put.addColumn(family, Bytes.toBytes("bicycleId"),
Bytes.toBytes(String.valueOf(bicycleId)));
// 出發(fā)地
String departure = row.getCell(4).getStringCellValue();
put.addColumn(family, Bytes.toBytes("departure"),
Bytes.toBytes(departure));
// 目的地
String destination = row.getCell(5).getStringCellValue();
put.addColumn(family, Bytes.toBytes("destination"),
Bytes.toBytes(destination));
// 所在城市
String city = row.getCell(6).getStringCellValue();
put.addColumn(family, Bytes.toBytes("city"), Bytes.toBytes(city));
// 清除目的地= 所在城市或者出發(fā)地= 目的地的無效數(shù)據(jù)
if (destination.equals(city)|| departure.equals(destination) ) {
continue;
}
//開始經(jīng)度
DecimalFormat formatter2 = new DecimalFormat("###.######");
String start_longitude = formatter2.format(row.getCell(7).getNumericCellValue());
put.addColumn(family, Bytes.toBytes("start_longitude"), Bytes.toBytes(String.valueOf(start_longitude)));
//開始緯度
String start_latitude = formatter2.format(row.getCell(8).getNumericCellValue());
put.addColumn(family, Bytes.toBytes("start_latitude"), Bytes.toBytes(String.valueOf(start_latitude)));
//結(jié)束經(jīng)度
String stop_longitude = formatter2.format(row.getCell(9).getNumericCellValue());
put.addColumn(family, Bytes.toBytes("stop_longitude"), Bytes.toBytes(String.valueOf(stop_longitude)));
//結(jié)束緯度
String stop_latitude = formatter2.format(row.getCell(10).getNumericCellValue());
put.addColumn(family, Bytes.toBytes("stop_latitude"),
Bytes.toBytes(String.valueOf(stop_latitude)));
puts.add(put);
} catch (Exception e) {
}
}
HBaseUtil.putByTable("t_shared_bicycle", puts);
/******* *** End ****** ****/
}
}
到了這里,關于【頭歌】共享單車之數(shù)據(jù)存儲的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!