当前位置: 首页 > news >正文

共享单车之数据存储

文章目录

  • 第1关:获取工作簿中的数据
  • 第2关:保存共享单车数据


第1关:获取工作簿中的数据

相关知识
获取工作簿中的信息,我们可以使用Java POI(POI是一个提供API给Java程序对Microsoft Office格式档案读和写的功能)提供的Workbook类来操作。

为了完成本关任务,你需要掌握:如何获取Wookbook的数据。

读取一个Wookbook中数据
读取工作簿中的内容大致分为下列几个步骤:

使用WorkbookFactory新建一个工作簿(Wookbook)
InputStream resourceAsStream = SaveData.class.getClassLoader().getResourceAsStream(“data.xls”);//通过类加载器获取本地文件
Workbook workbook = WorkbookFactory.create(resourceAsStream);
获取给定索引处的Sheet对象。
Sheet sheet = workbook.getSheetAt(0);//拿到Wookbook中的第一个Sheet
说明:一个Wookbook中可能存在多个Sheet,因此需要指定索引,如下图:

通过Sheet对象获取指定行和行内单元格。
Row row = sheet.getRow(1);//首行一般为说明,因此我们直接从第一行进行获取
Cell cell = row.getCell(0);//获取当前行第一个单元格
获取单元格中的值。

上图观察表结构数据trip_id为数字类型,时间为字符类型,在获取数据时我们必须遵循类型规则,对应获取。

//1.获取第一行中trip_id列的第一个值(33404951)
double numericCellValue = row.getCell(0).getNumericCellValue();
DecimalFormat formatter = new DecimalFormat(“########”);//一个#表示一个数字
String trip_id =formatter.format(numericCellValue);//我们需要使用DecimalFormat将数据格式化
//2.获取第一行中开始时间单元格的值
FastDateFormat instance = FastDateFormat.getInstance(“MM/dd/yyyy HH:mm”);
String beginTimeValue = row.getCell(1).getStringCellValue();
//为了方便后面的数据分析计算我们将需要将时间格式转为时间戳
long begintime = instance.parse(beginTimeValue).getTime();
//3.获取第一行开始经度单元格的值
double start_longitude = row.getCell(7).getNumericCellValue();
DecimalFormat formatter = new DecimalFormat(“###.######”);//#表示一个数字,不包括0
String longitude = formatter.format(start_longitude);
获取当前sheet中的物理定义行数
//为了完整的将整个Sheet中的数据全部存储,我们需要知道整个Sheet中有多少条数据,然后对其遍历
int rows = sheet.getPhysicalNumberOfRows();
编程要求
在右侧编辑器Begin-End中补充代码,获取data.xls文件中的数据,具体获取以下数据并将结果打印:trip_id、开始时间、结束经度、车辆id。

文件数据格式如下:

trip_id 开始时间 结束时间 车辆id 出发地 目的地 所在城市 开始经度 开始纬度 结束经度 结束纬度
33404951 7/1/2017 0:09 7/1/2017 0:45 5996 韩庄村北782米 韩庄村北782米 河北省保定市雄县 39.043732 116.260139 39.043732 116.260139
33463211 7/1/2017 1:01 7/1/2017 11:13 6342 韩庄村北782米 39.043732 116.260139 NA NA
33415440 7/1/2017 1:59 7/1/2017 2:12 6273 擎天矿用材料有限公司北609米 河北省保定市雄县G45(大广高速) 河北省保定市雄县G45(大广高速) 39.041691 116.235352 39.044701 116.252441
注意:表中有非法数据,我们在获取时为了避免出错或者获取到空的数据,可以使用try-catch将其抛出。

测试说明
平台会对你编写的代码进行测试:

测试输入:无;
预期输出:
骑行id:33404951,开始时间:1498838940000,车辆id:5996,结束经度:39.043732
骑行id:33415440,开始时间:1498845540000,车辆id:6273,结束经度:39.044701

开始你的任务吧,祝你成功!
示例代码如下:

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 = SaveWookbook.class.getClassLoader().getResourceAsStream("data.xls");Workbook workbook = WorkbookFactory.create(resourceAsStream);//2.拿到工作簿中第一个SheetSheet sheet = workbook.getSheetAt(0);//3.获取当前Sheet中的行数int rows = sheet.getPhysicalNumberOfRows();//4.对所有有效数据进行遍历并输出(期间无效数据通过异常捕获方式清除)for (int n = 1; n < rows; n++) {Row row = sheet.getRow(n);//通过异常方式清除格式不准确、数据不存在的无效行try {//trip_idDecimalFormat formatter1 = new DecimalFormat("########");String trip_id = formatter1.format(row.getCell(0).getNumericCellValue());//开始时间FastDateFormat instance = FastDateFormat.getInstance("MM/dd/yyyy HH:mm");String beginTimeValue = row.getCell(1).getStringCellValue();long begintime = instance.parse(beginTimeValue).getTime();//车辆idint car_id = (int)row.getCell(3).getNumericCellValue();//结束经度double start_longitude = row.getCell(9).getNumericCellValue();DecimalFormat formatter2 = new DecimalFormat("###.######");//#表示一个数字,不包括0String longitude = formatter2.format(start_longitude);System.out.println("骑行id:"+trip_id+",开始时间:"+begintime+",车辆id:"+car_id+",结束经度:"+longitude);} catch (Exception e) {}}/******** **    End    ******* ***/}
}

在这里插入图片描述

第2关:保存共享单车数据

相关知识
为了完成本关任务,你需要掌握:

如何创建HBase表;
如何读取文件;
了解共享单车数据表格式以及如何获取数据;
如何存储到HBase。
如何创建HBase表
com.util.HBaseUtil类封装了对应的创建Hbase表方法createTable

示例如下:

HBaseUtil.createTable(“t_shared_bicycle”, “info”);//创建拥有一个列族的info的表t_shared_bicycle,一个列族可拥有任意数量的列。
获取本地文件
文件存放目录为src/main/resources,我们可以通过类加载器加载共享单车数据文件dataResources.xls:

InputStream resourceAsStream = SaveData.class.getClassLoader().getResourceAsStream(“dataResources.xls”);
共享单车数据结构和获取
dataResources.xls文件格式如下:

trip_id 开始时间 结束时间 车辆id 出发地 目的地 所在城市 开始经度 开始纬度 结束经度 结束纬度
33404951 7/1/2017 0:09 7/1/2017 0:45 5996 韩庄村北782米 韩庄村北782米 河北省保定市雄县 39.043732 116.260139 39.043732 116.260139
33404950 7/1/2017 0:11 7/1/2017 0:45 5777 河北省保定市雄县G45(大广高速) 乡里乡情铁锅炖东499米 河北省保定市雄县 39.044159 116.251579 39.04652 116.237411
33404947 7/1/2017 1:59 7/1/2017 2:12 6342 韩庄村北782米 韩庄村北782米 河北省保定市雄县 39.043732 116.260139 39.043732 116.260139
如何存储到HBase
com.util.HBaseUtil类封装了对应的批量存储到Hbase表方法putByTable。示例如下:

List puts = new ArrayList<>();// 一个PUT代表一行数据,每个Put有唯一的ROWKEY
Put put = new Put(Bytes.toBytes(“33404951”)); //创建ROWKEY为33404951的PUT
byte[] family = Bytes.toBytes(“info”);
put.addColumn(family,Bytes.toBytes(“bicycleId”), Bytes.toBytes(String.valueOf(5996)));//在列族info中,增加字段名称为bicycleId,值为5996的元素
put.addColumn(family,Bytes.toBytes(“departure”), Bytes.toBytes(“韩庄村北782米”));//在列族info中,增加字段名称为departure,值为韩庄村北782米的元素
puts.add(put);
HBaseUtil.putByTable(“t_shared_bicycle”,puts);//批量保存数据到t_shared_bicycle
编程要求
根据提示,在右侧编辑器Begin-End中补充savaBicycleData方法,完成如下操作:

创建拥有列族info的表t_shared_bicycle;
将唯一骑行trip_id设为表的ROWKEY;
将出发地 = 目的地或者目的地 = 所在城市的无效数据清除;
把文件dataResources.xls中相应的数据存到Hbase表t_shared_bicycle中。
t_shared_bicycle表结构如下

列族名称 字段 对应的文件的描述 ROWKEY (格式为:骑行id)
info beginTime 开始时间 trip_id
info endTime 结束时间 trip_id
info bicycleId 车辆id trip_id
info departure 出发地 trip_id
info destination 目的地 trip_id
info city 所在城市 trip_id
info start_longitude 开始经度 trip_id
info stop_longitude 结束经度 trip_id
info start_latitude 开始纬度 trip_id
info stop_latitude 结束纬度 trip_id
提示:注意使用try-catch将无效数据或非法数据进行抛出。

测试说明
平台会对你编写的代码进行测试,数据量较大,评测时间可能较长,请耐心等待:

测试输入:37785165
预期输出:
rowCount–>331850
info:beginTime 1501500120000
info:bicycleId 6280
info:city 河北省保定市雄县
info:departure 东方红家园西南121米
info:destination 沙辛庄村南940米
info:endTime 1501500840000
info:start_latitude 116.13826
info:start_longitude 39.144981
info:stop_latitude 116.13237
info:stop_longitude 39.13525

说明:由于数据过多,我们将输出ROWKEY为37785165的信息。

开始你的任务吧,祝你成功!
示例代码如下:

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;
/* 
* 读取共享单车城市行车数据
* 
*/
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++) {// 通过异常方式清除格式不准确、数据不存在的无效行try {Row row = sheet.getRow(n);// 唯一骑行id,当作行rowkeyDecimalFormat 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())));// 结束时间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)));// 出发地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));// 清除目的地 = 所在城市 或者 出发地 = 目的地 的无效数据if (destination.equals(city)|| departure.equals(destination) ) {continue;}//开始经度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)));//结束经度String stop_longitude = formatter2.format(row.getCell(9).getNumericCellValue());put.addColumn(family, Bytes.toBytes("stop_longitude"), Bytes.toBytes(String.valueOf(stop_longitude)));//结束纬度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   ****** ****/}
}

在这里插入图片描述


http://www.lryc.cn/news/269962.html

相关文章:

  • Flink(十一)【状态管理】
  • 【三维目标检测/自动驾驶】IA-BEV:基于结构先验和自增强学习的实例感知三维目标检测(AAAI 2024)
  • wefew
  • Springboot整合JSP-修订版本(Springboot3.1.6+IDEA2022版本)
  • Java核心知识点1-java和c++区别、隐式和显示类型转换
  • C++图论之强连通图
  • SadTalker数字人增加视频输出mp4质量精度
  • swing快速入门(三十二)消息对话框
  • 《Spring Cloud学习笔记:Nacos配置管理 OpenFeign LoadBalancer Getway》
  • 深入解析 Flink CDC 增量快照读取机制
  • 060:vue中markdown编辑器mavon-editor的应用示例
  • 使用SCP在Linux中安全复制文件:参数详解
  • 【动态规划精选题目】3、简单多状态模型
  • 软件测试/测试开发丨Python 虚拟环境及pip环境管理
  • Mybatis SQL构建器类 - SQL类
  • 海云安亮相2023北京国际金融安全论坛,助力金融企业数字化转型降本增效
  • nodeJS搭建免费代理IP池爬取贴吧图片实战
  • 基于图搜索的自动驾驶规划算法 - BFS,Dijstra,A*
  • Spring系列学习四、Spring数据访问
  • HBase 创建不分裂的表 ( 禁止 Table Split )
  • docker入门概念详解
  • C++程序设计实践报告【格式】
  • 浅谈数据仓库运营
  • 系列六、Consul
  • Java集合/泛型篇----第一篇
  • 集合使用注意事项
  • 什么是 JavaScript 中的 WeakMap
  • nodejs+vue+ElementUi农产品团购销售系统zto2c
  • nacos入门篇001-安装与启动
  • WordPress主题大前端DUX v8.3源码下载