栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

共享单车之数据存储 第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);

       
       
        
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/632974.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号