更新时间:2023-08-02 GMT+08:00
插入数据
功能介绍
HBase是一个面向列的数据库,一行数据,可能对应多个列族,而一个列族又可以对应多个列。通常,写入数据的时候,我们需要指定要写入的列(含列族名称和列名称)。HBase通过HTable的put方法来Put数据,可以是一行数据也可以是数据集。
开启冷热分离特性表的写入逻辑和正常表写入逻辑一致。
代码样例
public void testPut() { LOG.info("Entering testPut."); // Specify the column family name. byte[] familyName = Bytes.toBytes("info"); // Specify the column name. byte[][] qualifiers = { Bytes.toBytes("temp"), Bytes.toBytes("hum") }; Table table = null; try { // Instantiate an HTable object. table = conn.getTable(tableName); // Instantiate a Put object. Every Hour insert one data. Put put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/1 00:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("28.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("54.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/1 01:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("27.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("53.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/1 02:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("27.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("52.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/1 03:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("27.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("51.0")); puts.add(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/1 04:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("27.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("50.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/1 05:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("27.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("49.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/1 06:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("27.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("48.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/1 07:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("27.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("46.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/1 08:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("29.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("46.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/1 09:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("29.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("46.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/1 10:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("30.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("48.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/1 11:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("32.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("48.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/1 12:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("32.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("49.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/1 13:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("33.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("49.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/1 14:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("33.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("50.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/1 15:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("32.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("50.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/1 16:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("31.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("51.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/1 17:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("30.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("51.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/1 18:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("30.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("51.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/1 19:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("29.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("51.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/1 20:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("29.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("52.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/1 21:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("29.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("53.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/1 22:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("28.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("54.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/1 23:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("28.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("54.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/2 00:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("28.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("54.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/2 01:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("27.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("53.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/2 02:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("27.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("52.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/2 03:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("27.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("51.0")); puts.add(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/2 04:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("27.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("50.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/2 05:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("27.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("49.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/2 06:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("27.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("48.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/2 07:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("27.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("46.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/2 08:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("29.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("46.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/2 09:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("29.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("46.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/2 10:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("30.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("48.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/2 11:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("32.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("48.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/2 12:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("32.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("49.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/2 13:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("33.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("49.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/2 14:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("33.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("50.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/2 15:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("32.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("50.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/2 16:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("31.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("51.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/2 17:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("30.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("51.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/2 18:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("30.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("51.0")); puts.clear(); puts.add(put); table.put(puts); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/2 19:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("29.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("51.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/2 20:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("29.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("52.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/2 21:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("29.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("53.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/2 22:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("28.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("54.0")); table.put(put); put = new Put(Bytes.toBytes("Shenzhen#Longgang#2017/7/2 23:00:00")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("28.0")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("54.0")); table.put(put); LOG.info("Put successfully."); } catch (IOException e) { LOG.error("Put failed " ,e); } finally { if (table != null) { try { // Close the HTable object. table.close(); } catch (IOException e) { LOG.error("Close table failed " ,e); } } } LOG.info("Exiting testPut."); }
父主题: 样例代码说明