头歌:共享单车之数据存储

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

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 rowCount  = sheet.getPhysicalNumberOfRows();
        //4.对所有有效数据进行遍历并输出(期间无效数据通过异常捕获方式清除)
        for(int i =1; i <rowCount; i++){
            Row row = sheet.getRow(i);
            try{
                //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();
                //车辆的id
                int car_id = (int)row.getCell(3).getNumericCellValue();
                //为了方便后面的数据分析计算我们将需要将时间格式转为时间戳  
                long begintime = instance.parse(beginTimeValue).getTime();
                //3.获取第一行开始经度单元格的值  
                double start_longitude = row.getCell(9).getNumericCellValue();  
                DecimalFormat formatter2 = new DecimalFormat("###.######");//#表示一个数字,不包括0  
                String longitude = formatter2.format(start_longitude);
                System.out.println("骑行id:"+trip_id+",开始时间:"+begintime+",车辆id:"+car_id+",结束经度:"+longitude); 
            }catch(Exception e){

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

第2关 保存共享单车数据

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   ******* ***/
		//创建拥有一个列族的info的表t_shared_bicycle,一个列族可拥有任意数量的列
       	// HBaseUtil.createTable("t_shared_bicycle", "info");。  
		// InputStream resourceAsStream = SaveData.class.getClassLoader().getResourceAsStream("dataResources.xls");
		// Workbook workbook = WorkbookFactory.create(resourceAsStream);
        // //2.拿到工作簿中第一个Sheet
        // Sheet sheet = workbook.getSheetAt(0);
        // //3.获取当前Sheet中的行数
        // int rowCount  = sheet.getPhysicalNumberOfRows();
		// // 一个PUT代表一行数据,每个Put有唯一的ROWKEY  
		// List<Put> puts = new ArrayList<>();
		// for(int i = 0; i < rowCount; i++){
		// 	// Row row = sheet.getRow(i);
		// 	try{
        //         Row row = sheet.getRow(i);
        //         //1.获取第一行中trip_id列的第一个值(33404951)
		// 		double numericCellValue = row.getCell(0).getNumericCellValue();  
        //         DecimalFormat formatter = new DecimalFormat("########");//一个#表示一个数字  
        //         String trip_id =formatter.format(numericCellValue);//我们需要使用DecimalFormat将数据格式化
        //         Put put = new Put(Bytes.toBytes(trip_id)); //创建ROWKEY为trip_id的PUT
        //         byte[] family = Bytes.toBytes("info");
        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; 
					} 

                // //2.开始时间 
                // 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.valuesOf(pares.getTime())));
                // //3.结束时间
                // String sendTimeValue = row.getCell(2).getStringCellValue();
                // Date parse2 = instance.parse(sendTimeValue);
                // put.addcolumn(family,Bytes.toBytes("endTime"),Bytes.toBytes(String.valuesOf(pares2.getTime())));
                // //车辆的id
                // int bicycleId = (int)row.getCell(3).getNumericCellValue();
                // put.addcolumn(family,Bytes.toBytes("bicycleId"),Bytes.toBytes(String.valuesOf(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))); 
                // DecimalFormat formatter = new DecimalFormat("###.######");//#表示一个数字,不包括0    
                // double longitude = row.getCell(7).getNumericCellValue();  
                // String start_longitude = formatter2.format(longitude);
                // put.addColumn(family, Bytes.toBytes("start_longitude"),Bytes.toBytes(String.valueOf(start_longitude))); 
                // //结束经度
                // double longitude2 = row.getCell(8).getNumericCellValue();  
                // String 	stop_longitude = formatter2.format(longitude2);
                // put.addColumn(family, Bytes.toBytes("stop_longitude"),Bytes.toBytes(String.valueOf(stop_longitude))); 

                // //开始纬度
                // double latitude = row.getCell(9).getNumericCellValue();  
                // String 	start_latitude = formatter2.format(latitude);
                // put.addColumn(family, Bytes.toBytes("start_latitude"),Bytes.toBytes(String.valueOf(start_latitude)));
                // //结束纬度
                // double latitude2 = row.getCell(10).getNumericCellValue();  
                // String 	stop_latitude = formatter2.format(latitude2);
                // 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   ****** ****/
	}

}

 

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值