共享单车之数据存储 第1关:保存共享单车数据

共享单车之数据存储 第1关:保存共享单车数据,第1张

共享单车之数据存储 第1关:保存共享单车数据
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 {
        
       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 puts = new ArrayList();
       for (int n =1; n< rows; n++) {
           try {
               Row row = sheet.getRow(n);
               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);

       
       
        
    }

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5638845.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存