共享单车之数据存储-保存共享单车数据

该文描述了一个编程任务,要求从dataResources.xls文件中读取共享单车数据,创建HBase表t_shared_bicycle并存储数据。数据包括trip_id、开始和结束时间、车辆id等信息。无效数据如出发地等于目的地会被清除。提供了创建表、读取文件及存储到HBase的代码示例。
摘要由CSDN通过智能技术生成

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


任务描述

本关任务:从dataResources.xls文件中获取共享单车数据,保存到HBase中。

相关知识

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

  1. 如何创建HBase表;
  2. 如何读取文件;
  3. 了解共享单车数据表格式以及如何获取数据;
  4. 如何存储到HBase

如何创建HBase

com.util.HBaseUtil类封装了对应的创建Hbase表方法createTable

示例如下:

 
  1. HBaseUtil.createTable("t_shared_bicycle", "info");//创建拥有一个列族的info的表t_shared_bicycle,一个列族可拥有任意数量的列。

获取本地文件

文件存放目录为src/main/resources,我们可以通过类加载器加载共享单车数据文件dataResources.xls

 
  1. 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。示例如下:

 
  1. List<Put> puts = new ArrayList<>();// 一个PUT代表一行数据,每个Put有唯一的ROWKEY
  2. Put put = new Put(Bytes.toBytes("33404951")); //创建ROWKEY为33404951的PUT
  3. byte[] family = Bytes.toBytes("info");
  4. put.addColumn(family,Bytes.toBytes("bicycleId"), Bytes.toBytes(String.valueOf(5996)));//在列族info中,增加字段名称为bicycleId,值为5996的元素
  5. put.addColumn(family,Bytes.toBytes("departure"), Bytes.toBytes("韩庄村北782米"));//在列族info中,增加字段名称为departure,值为韩庄村北782米的元素
  6. puts.add(put);
  7. HBaseUtil.putByTable("t_shared_bicycle",puts);//批量保存数据到t_shared_bicycle

编程要求

根据提示,在右侧编辑器Begin-End中补充savaBicycleData方法,完成如下操作:

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

t_shared_bicycle表结构如下

列族名称字段对应的文件的描述ROWKEY (格式为:骑行id
infobeginTime开始时间trip_id
infoendTime结束时间trip_id
infobicycleId车辆idtrip_id
infodeparture出发地trip_id
infodestination目的地trip_id
infocity所在城市trip_id
infostart_longitude开始经度trip_id
infostop_longitude结束经度trip_id
infostart_latitude开始纬度trip_id
infostop_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

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


开始你的任务吧,祝你成功!

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;ccccccccccccccc
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,当作行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()))); 
                // 结束时间
                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   ****** ****/
	}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值