Inserting Data
Function Description
HBase is a column-based database. A row of data may have multiple column families, and a column family may contain multiple columns.
When writing data, you must specify the columns (including the column family names and column names) to which data is written.
In HBase, data (a row of data or data sets) is inserted using the put method of HTable.
Sample Code
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("name"), Bytes.toBytes("gender"), Bytes.toBytes("age"), Bytes.toBytes("address") }; Table table = null; try { // Instantiate an HTable object. table = conn.getTable(tableName); List<Put> puts = new ArrayList<Put>(); // Instantiate a Put object. Put put = new Put(Bytes.toBytes("012005000201")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("A")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("Male")); put.addColumn(familyName, qualifiers[2], Bytes.toBytes("19")); put.addColumn(familyName, qualifiers[3], Bytes.toBytes("Shenzhen, Guangdong")); puts.add(put); put = new Put(Bytes.toBytes("012005000202")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("B")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("Female")); put.addColumn(familyName, qualifiers[2], Bytes.toBytes("23")); put.addColumn(familyName, qualifiers[3], Bytes.toBytes("Shijiazhuang, Hebei")); puts.add(put); put = new Put(Bytes.toBytes("012005000203")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("C")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("Male")); put.addColumn(familyName, qualifiers[2], Bytes.toBytes("26")); put.addColumn(familyName, qualifiers[3], Bytes.toBytes("Ningbo, Zhejiang")); puts.add(put); put = new Put(Bytes.toBytes("012005000204")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("D")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("Male")); put.addColumn(familyName, qualifiers[2], Bytes.toBytes("18")); put.addColumn(familyName, qualifiers[3], Bytes.toBytes("Xiangyang, Hubei")); puts.add(put); put = new Put(Bytes.toBytes("012005000205")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("E")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("Female")); put.addColumn(familyName, qualifiers[2], Bytes.toBytes("21")); put.addColumn(familyName, qualifiers[3], Bytes.toBytes("Shangrao, Jiangxi")); puts.add(put); put = new Put(Bytes.toBytes("012005000206")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("F")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("Male")); put.addColumn(familyName, qualifiers[2], Bytes.toBytes("32")); put.addColumn(familyName, qualifiers[3], Bytes.toBytes("Zhuzhou, Hunan")); puts.add(put); put = new Put(Bytes.toBytes("012005000207")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("G")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("Female")); put.addColumn(familyName, qualifiers[2], Bytes.toBytes("29")); put.addColumn(familyName, qualifiers[3], Bytes.toBytes("Nanyang, Henan")); puts.add(put); put = new Put(Bytes.toBytes("012005000208")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("H")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("Female")); put.addColumn(familyName, qualifiers[2], Bytes.toBytes("30")); put.addColumn(familyName, qualifiers[3], Bytes.toBytes("Kaixian, Chongqing")); puts.add(put); put = new Put(Bytes.toBytes("012005000209")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("I")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("Male")); put.addColumn(familyName, qualifiers[2], Bytes.toBytes("26")); put.addColumn(familyName, qualifiers[3], Bytes.toBytes("Weinan, Shaanxi")); puts.add(put); put = new Put(Bytes.toBytes("012005000210")); put.addColumn(familyName, qualifiers[0], Bytes.toBytes("J")); put.addColumn(familyName, qualifiers[1], Bytes.toBytes("Male")); put.addColumn(familyName, qualifiers[2], Bytes.toBytes("25")); put.addColumn(familyName, qualifiers[3], Bytes.toBytes("Dalian, Liaoning")); puts.add(put); // Submit a put request. table.put(puts); 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."); }
Precautions
Multiple threads are not allowed to use the same HTable instance at the same time. HTable is a non-thread-safe class. If an HTable instance is used by multiple threads at the same time, exceptions will occur.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot