如何创建 HBase
表
com.util.HBaseUtil
类封装了对应的创建 Hbase
表方法 createTable
示例如下:
HBaseUtil.createTable("t_shared_bicycle", "info");//创建拥有一个列族的info的表t_shared_bicycle,一个列族可拥有任意数量的列。
获取本地文件
可以通过类加载器加载共享单车数据文件 dataResources.xls
:
InputStream resourceAsStream = SaveData.class.getClassLoader().getResourceAsStream("dataResources.xls");
如何存储到 HBase
com.util.HBaseUtil
类封装了对应的批量存储到 Hbase
表方法 putByTable
。示例如下:
List<Put> puts = new ArrayList<>();// 一个PUT代表一行数据,每个Put有唯一的ROWKEY Put put = new Put(Bytes.toBytes("33404951")); //创建ROWKEY为33404951的PUT byte[] family = Bytes.toBytes("info"); put.addColumn(family,Bytes.toBytes("bicycleId"), Bytes.toBytes(String.valueOf(5996)));//在列族info中,增加字段名称为bicycleId,值为5996的元素 put.addColumn(family,Bytes.toBytes("departure"), Bytes.toBytes("韩庄村北782米"));//在列族info中,增加字段名称为departure,值为韩庄村北782米的元素 puts.add(put); HBaseUtil.putByTable("t_shared_bicycle",puts);//批量保存数据到t_shared_bicycle
测试:
- 创建拥有列族
<span class="pln">info</span></code>的表 <code class="prettyprint prettyprinted"><span class="pln">t_shared_bicycle</span></code>;</li>
<li>将唯一骑行 <code class="prettyprint prettyprinted"><span class="pln">trip_id</span></code>设为表的 <code class="prettyprint prettyprinted"><span class="pln">ROWKEY</span></code>;</li>
<li>将 <code class="prettyprint prettyprinted"><span class="pun">出发地</span> <span class="pun">=</span> <span class="pun">目的地</span></code>或者 <code class="prettyprint prettyprinted"><span class="pun">目的地</span> <span class="pun">=</span> <span class="pun">所在城市</span></code>的无效数据清除;</li>
<li>把文件 <code class="prettyprint prettyprinted"><span class="pln">dataResources</span><span class="pun">.</span><span class="pln">xls</span></code>中相应的数据存到 <code class="prettyprint prettyprinted"><span class="typ">Hbase</span></code>表 <code class="prettyprint prettyprinted"><span class="pln">t_shared_bicycle</span></code>中。</li>
</ol><!-- wp:paragraph -->
<p><code>dataResources.xls</code>文件格式如下:</p>
<!-- /wp:paragraph --><!-- wp:table {"className":"is-style-stripes"} -->
<figure class="wp-block-table is-style-stripes"><table class=""><thead><tr><th>trip_id</th><th>开始时间</th><th>结束时间</th><th>车辆id</th><th>出发地</th><th>目的地</th><th>所在城市</th><th>开始经度</th><th>开始纬度</th><th>结束经度</th><th>结束纬度</th></tr></thead><tbody><tr><td>33404951</td><td>7/1/2017 0:09</td><td>7/1/2017 0:45</td><td>5996</td><td>韩庄村北782米</td><td>韩庄村北782米</td><td>河北省保定市雄县</td><td>39.043732</td><td>116.260139</td><td>39.043732</td><td>116.260139</td></tr><tr><td>33404950</td><td>7/1/2017 0:11</td><td>7/1/2017 0:45</td><td>5777</td><td>河北省保定市雄县G45(大广高速)</td><td>乡里乡情铁锅炖东499米</td><td>河北省保定市雄县</td><td>39.044159</td><td>116.251579</td><td>39.04652</td><td>116.237411</td></tr><tr><td>33404947</td><td>7/1/2017 1:59</td><td>7/1/2017 2:12</td><td>6342</td><td>韩庄村北782米</td><td>韩庄村北782米</td><td>河北省保定市雄县</td><td>39.043732</td><td>116.260139</td><td>39.043732</td><td>116.260139</td></tr></tbody></table></figure>
<!-- /wp:table --><!-- wp:paragraph -->
<p><code>t_shared_bicycle</code>表结构如下</p>
<!-- /wp:paragraph --><!-- wp:table {"className":"is-style-stripes"} -->
<figure class="wp-block-table is-style-stripes"><table class=""><thead><tr><th>列族名称</th><th>字段</th><th>对应的文件的描述</th><th>ROWKEY (格式为:骑行 <code>id</code>)</th></tr></thead><tbody><tr><td>info</td><td>beginTime</td><td>开始时间</td><td><code>trip_id</code></td></tr><tr><td>info</td><td>endTime</td><td>结束时间</td><td><code>trip_id</code></td></tr><tr><td>info</td><td>bicycleId</td><td>车辆 <code>id</code></td><td><code>trip_id</code></td></tr><tr><td>info</td><td>departure</td><td>出发地</td><td><code>trip_id</code></td></tr><tr><td>info</td><td>destination</td><td>目的地</td><td><code>trip_id</code></td></tr><tr><td>info</td><td>city</td><td>所在城市</td><td><code>trip_id</code></td></tr><tr><td>info</td><td>start_longitude</td><td>开始经度</td><td><code>trip_id</code></td></tr><tr><td>info</td><td>stop_longitude</td><td>结束经度</td><td><code>trip_id</code></td></tr><tr><td>info</td><td>start_latitude</td><td>开始纬度</td><td><code>trip_id</code></td></tr><tr><td>info</td><td>stop_latitude</td><td>结束纬度</td><td><code>trip_id</code></td></tr></tbody></table></figure>
<!-- /wp:table --><p>代码如下:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="java">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;/*
* 读取共享单车城市行车数据
*
*/
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<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);
}
}