Writing Data
Function Description
Follow instructions in Inserting Data to write data. Note that the data to be written must contain all fields of the index in Elasticsearch. If not all fields are contained, an Elasticsearch index data exception may occur. The user client needs to process ESDocIndexException based on business scenario requirements. The user can retry or ignore or perform other operations.
Sample Code
The following is sample code. Replace ***replace_with_Chinese_characters*** in code with Chinese characters.
public void putData() {
LOG.info("Entering testPut.");
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("rowkey001"));
put.addColumn(CF1, QUA_ARTICLE_CONTENT_CHINESE, Bytes.toBytes("***replace_with_Chinese_characters***"));
put.addColumn(CF2, QUA_ARTICLE_CONTENT_English, Bytes.toBytes("how many apples in the market"));
put.addColumn(CF1, QUA_ARTICLE_ID, Bytes.toBytes(1111l));
put.addColumn(CF1, QUA_CHARACTER_NUM, Bytes.toBytes(1));
short shortNum = 1;
put.addColumn(CF1, QUA_PAGE_NUM, Bytes.toBytes(shortNum));
char c = 'A';
put.addColumn(CF1, QUA_ARTICLE_LEVEL, new byte[]{(byte)c});
put.addColumn(CF1, QUA_RESEARCH_COST, Bytes.toBytes(111.11d));
put.addColumn(CF1, QUA_ARTICLE_SCORE, Bytes.toBytes(80.5f));
put.addColumn(CF1, QUA_AUTHOR_MALE, Bytes.toBytes(true));
put.addColumn(CF1, QUA_WHATEVER, Bytes.toBytes("happy life and sweet love"));
puts.add(put);
put = new Put(Bytes.toBytes("rowkey002"));
put.addColumn(CF1, QUA_ARTICLE_CONTENT_CHINESE, Bytes.toBytes("***replace_with_Chinese_characters***"));
put.addColumn(CF2, QUA_ARTICLE_CONTENT_English, Bytes.toBytes("how many people in the swimming pool"));
put.addColumn(CF1, QUA_ARTICLE_ID, Bytes.toBytes(2222l));
put.addColumn(CF1, QUA_CHARACTER_NUM, Bytes.toBytes(2));
shortNum = 2;
put.addColumn(CF1, QUA_PAGE_NUM, Bytes.toBytes(shortNum));
c = 'B';
put.addColumn(CF1, QUA_ARTICLE_LEVEL, new byte[]{(byte)c});
put.addColumn(CF1, QUA_RESEARCH_COST, Bytes.toBytes(222.22d));
put.addColumn(CF1, QUA_ARTICLE_SCORE, Bytes.toBytes(170.5f));
put.addColumn(CF1, QUA_AUTHOR_MALE, Bytes.toBytes(true));
put.addColumn(CF1, QUA_WHATEVER, Bytes.toBytes("forever young"));
puts.add(put);
put = new Put(Bytes.toBytes("rowkey003"));
put.addColumn(CF1, QUA_ARTICLE_CONTENT_CHINESE, Bytes.toBytes("***replace_with_Chinese_characters***"));
put.addColumn(CF2, QUA_ARTICLE_CONTENT_English, Bytes.toBytes("we play video game in night"));
put.addColumn(CF1, QUA_ARTICLE_ID, Bytes.toBytes(3333l));
put.addColumn(CF1, QUA_CHARACTER_NUM, Bytes.toBytes(3));
shortNum = 3;
put.addColumn(CF1, QUA_PAGE_NUM, Bytes.toBytes(shortNum));
c = 'C';
put.addColumn(CF1, QUA_ARTICLE_LEVEL, new byte[]{(byte)c});
put.addColumn(CF1, QUA_RESEARCH_COST, Bytes.toBytes(333.33d));
put.addColumn(CF1, QUA_ARTICLE_SCORE, Bytes.toBytes(180.5f));
put.addColumn(CF1, QUA_AUTHOR_MALE, Bytes.toBytes(false));
put.addColumn(CF1, QUA_WHATEVER, Bytes.toBytes("wishes always with you"));
puts.add(put);
// Submit a put request.
try {
table.put(puts);
}
// if your put operation does not contain the all field in your schema, you will get ESDocIndexException.
// ESDocIndexException means data/document indexing to ES failed, but remember data put in HBase success.
// so here you handle this exception depends on your business( you can retry or ignore).
catch (ESDocIndexException e) {
//TODO
}
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.");
} Last Article: Enabling an Index in Elasticsearch When Creating a Table
Next Article: Querying Data
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.