77
88package examples ;
99
10+ import java .util .Iterator ;
1011import java .util .List ;
1112
1213import com .aliyun .openservices .ots .ClientException ;
1314import com .aliyun .openservices .ots .ServiceException ;
1415import com .aliyun .openservices .ots .OTSClient ;
1516import com .aliyun .openservices .ots .OTSErrorCode ;
1617import com .aliyun .openservices .ots .OTSException ;
17- import com .aliyun .openservices .ots .model .CapacityUnit ;
18- import com .aliyun .openservices .ots .model .ColumnValue ;
19- import com .aliyun .openservices .ots .model .Condition ;
20- import com .aliyun .openservices .ots .model .CreateTableRequest ;
21- import com .aliyun .openservices .ots .model .DeleteTableRequest ;
22- import com .aliyun .openservices .ots .model .GetRangeRequest ;
23- import com .aliyun .openservices .ots .model .GetRangeResult ;
24- import com .aliyun .openservices .ots .model .PrimaryKeyType ;
25- import com .aliyun .openservices .ots .model .PrimaryKeyValue ;
26- import com .aliyun .openservices .ots .model .PutRowRequest ;
27- import com .aliyun .openservices .ots .model .PutRowResult ;
28- import com .aliyun .openservices .ots .model .RangeRowQueryCriteria ;
29- import com .aliyun .openservices .ots .model .Row ;
30- import com .aliyun .openservices .ots .model .RowExistenceExpectation ;
31- import com .aliyun .openservices .ots .model .RowPrimaryKey ;
32- import com .aliyun .openservices .ots .model .RowPutChange ;
33- import com .aliyun .openservices .ots .model .TableMeta ;
18+ import com .aliyun .openservices .ots .model .*;
3419
3520public class OTSMultiDataSample {
3621 private static final String COLUMN_GID_NAME = "gid" ;
@@ -41,10 +26,10 @@ public class OTSMultiDataSample {
4126 private static final String COLUMN_MOBILE_NAME = "mobile" ;
4227
4328 public static void main (String args []) {
44- final String endPoint = "http://10.101.16.10" ;
45- final String accessId = "xxxx " ;
46- final String accessKey = "yyyy " ;
47- final String instanceName = "zzzz " ;
29+ final String endPoint = "" ;
30+ final String accessId = "" ;
31+ final String accessKey = "" ;
32+ final String instanceName = "" ;
4833
4934 OTSClient client = new OTSClient (endPoint , accessId , accessKey ,
5035 instanceName );
@@ -60,8 +45,12 @@ public static void main(String args[]) {
6045
6146 // 插入多行数据。
6247 putRows (client , tableName );
48+
6349 // 再取回来看看。
6450 getRange (client , tableName );
51+
52+ // 迭代读取
53+ getByIterator (client , tableName );
6554 } catch (ServiceException e ) {
6655 System .err .println ("操作失败,详情:" + e .getMessage ());
6756 // 可以根据错误代码做出处理, OTS的ErrorCode定义在OTSErrorCode中。
@@ -188,4 +177,41 @@ private static void getRange(OTSClient client, String tableName)
188177 .getReadCapacityUnit ();
189178 System .out .println ("本次读操作消耗的读CapacityUnit为:" + consumedReadCU );
190179 }
180+
181+ private static void getByIterator (OTSClient client , String tableName )
182+ throws OTSException , ClientException {
183+ // 构造迭代读取的起始位置
184+ RowPrimaryKey inclusiveStartKey = new RowPrimaryKey ();
185+ inclusiveStartKey .addPrimaryKeyColumn (COLUMN_GID_NAME ,
186+ PrimaryKeyValue .fromLong (1 ));
187+ inclusiveStartKey .addPrimaryKeyColumn (COLUMN_UID_NAME ,
188+ PrimaryKeyValue .INF_MIN );
189+
190+ // 构造迭代读取的结束位置
191+ RowPrimaryKey exclusiveEndKey = new RowPrimaryKey ();
192+ exclusiveEndKey .addPrimaryKeyColumn (COLUMN_GID_NAME ,
193+ PrimaryKeyValue .fromLong (4 ));
194+ exclusiveEndKey .addPrimaryKeyColumn (COLUMN_UID_NAME ,
195+ PrimaryKeyValue .INF_MAX );
196+
197+ // 构造迭代读取的参数对象,并设置起始和结束的位置,包括起始位置的行,不包括结束位置的行
198+ RangeIteratorParameter rangeIteratorParameter = new RangeIteratorParameter (tableName );
199+ rangeIteratorParameter .setInclusiveStartPrimaryKey (inclusiveStartKey );
200+ rangeIteratorParameter .setExclusiveEndPrimaryKey (exclusiveEndKey );
201+
202+ // 创建出迭代器,并迭代获取美一行数据
203+ try {
204+ Iterator <Row > iterator = client .createRangeIterator (rangeIteratorParameter );
205+ while (iterator .hasNext ()) {
206+ Row row = iterator .next ();
207+ System .out .println ("name:" + row .getColumns ().get (COLUMN_NAME_NAME ));
208+ System .out .println ("address" + row .getColumns ().get (COLUMN_ADDRESS_NAME ));
209+ }
210+ System .out .println ("Iterator get succeeded." );
211+ } catch (ClientException ex ) {
212+ System .out .println ("Iterator get failed." );
213+ } catch (OTSException ex ) {
214+ System .out .println ("Iterator get failed." );
215+ }
216+ }
191217}
0 commit comments