@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