educoder项目——共享单车之数据存储

共享单车之数据存储
第一关 获取工作簿中的数据

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中的行数
        int rows = sheet.getPhysicalNumberOfRows();
        
        //4.对所有有效数据进行遍历并输出(期间无效数据通过异常捕获方式清除)
        for(int n=1;n<rows;n++)
        {
            Row row = sheet.getRow(n);
            //通过异常方式清除格式不准确、数据不存在的无效行
            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();
                //结束经度
                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+",结束经度:"+longitude);
            }catch(Exception e){

            }
        }
        
        
       /******** **    End    ******* ***/
	}
}

第二关 保存共享单车数据

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,当作行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   ****** ****/
	}

}

  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小小小小笼包

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值