Ali OTS table storage one-on-one with bulk multiline insert--javasdk

Source: Internet
Author: User
Tags exception handling
@Autowired

Private Syncclient syncclient;

----------------------------a single write-------------------------------

/**
* OTs Single Write
*/
Primarykeybuilder Primarykeybuilder = Primarykeybuilder.createprimarykeybuilder ();
Primarykeybuilder.addprimarykeycolumn (alitsconfigs.tb_pk_name_connector_id, primarykeyvalue.fromstring ( Info.getconnectorid ()));
Primarykeybuilder.addprimarykeycolumn (alitsconfigs.tb_pk_name_operator_id, primarykeyvalue.fromstring ( Operatorid));
Primarykeybuilder.addprimarykeycolumn (alitsconfigs.tb_pk_name_station_id, primarykeyvalue.fromstring (stationId) );
Primarykeybuilder.addprimarykeycolumn (Alitsconfigs.tb_pk_name_data_time, Primarykeyvalue.fromlong (dataTime));
PrimaryKey PrimaryKey = Primarykeybuilder.build ();
Rowputchange Rowputchange = new Rowputchange (Alitsconfigs.tb_name_connector_rt, PrimaryKey);
Rowputchange.addcolumn (New Column (Alitsconfigs.tb_column_status, Columnvalue.fromlong (Info.getstatus ()));
Rowputchange.addcolumn (New Column (Alitsconfigs.tb_column_parkstatus, Columnvalue.fromlong (Info.getParkStatus ())) );
Rowputchange.addcolumn (New Column (Alitsconfigs.tb_column_lockstatus, Columnvalue.fromlong (Info.getLockStatus ())) );
Putrowrequest request = new Putrowrequest ();
Rowputchange.setcondition (New Condition (rowexistenceexpectation.expect_not_exist));
Request.setrowchange (Rowputchange);
Syncclient.putrow (Request);


----------------------------Bulk Write-------------------------------

/**
* OTS Bulk Operation
*/
Batchwriterowrequest request = new Batchwriterowrequest ();
try {
for (int i = 0; i < list.size (); i++) {
Primarykeybuilder Primarykeybuilder = Primarykeybuilder.createprimarykeybuilder ();
Primarykeybuilder.addprimarykeycolumn (alitsconfigs.tb_pk_name_connector_id, primarykeyvalue.fromstring (list.get (i). Getconnectorid ());
Primarykeybuilder.addprimarykeycolumn (alitsconfigs.tb_pk_name_operator_id, primarykeyvalue.fromstring ( Operatorid));
Primarykeybuilder.addprimarykeycolumn (alitsconfigs.tb_pk_name_station_id, primarykeyvalue.fromstring (stationId) );
Primarykeybuilder.addprimarykeycolumn (Alitsconfigs.tb_pk_name_data_time, Primarykeyvalue.fromlong (dataTime));
PrimaryKey PrimaryKey = Primarykeybuilder.build ();

Rowputchange Rowputchange = new Rowputchange (Alitsconfigs.tb_name_connector_rt, PrimaryKey);
Rowputchange.addcolumn (New Column (Alitsconfigs.tb_column_status, Columnvalue.fromlong (List.get (i). GetStatus ())) ;
Rowputchange.addcolumn (New Column (Alitsconfigs.tb_column_parkstatus, Columnvalue.fromlong) (List.get (i). Getparkstatus ()));
Rowputchange.addcolumn (New Column (Alitsconfigs.tb_column_lockstatus, Columnvalue.fromlong) (List.get (i). Getlockstatus ()));
Constructs a request object that inserts data into the
Request.addrowchange (Rowputchange);
}

The Batchwriterow interface Returns a result set that contains the same number of results as the rows inserted, the result in the result set is not necessarily successful, and the user needs to retry the unsuccessful operation on its own.
Batchwriterowresponse response = Syncclient.batchwriterow (request);
Result exception Handling
if (!response.isallsucceed ()) {
For (Batchwriterowresponse.rowresult rowResult:response.getFailedRows ()) {
Log.error ("Failed row:" + Request.getrowchange (Rowresult.gettablename (), Rowresult.getindex ()). GetPrimaryKey ());
Log.error ("Reason for failure:" + rowresult.geterror (). toString ());
}}

The retry logic is similar to the logic in the Batchgetrow example above, where you omit

catch (Exception ex) {
Ex.printstacktrace ();
Log.error ("Batch write row failed.");
}

The picture looks more clear

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.