|
| 1 | +package examples; |
| 2 | + |
| 3 | +import com.aliyun.openservices.ots.ClientException; |
| 4 | +import com.aliyun.openservices.ots.OTSClient; |
| 5 | +import com.aliyun.openservices.ots.OTSErrorCode; |
| 6 | +import com.aliyun.openservices.ots.ServiceException; |
| 7 | +import com.aliyun.openservices.ots.model.*; |
| 8 | +import com.aliyun.openservices.ots.model.condition.ColumnCondition; |
| 9 | +import com.aliyun.openservices.ots.model.condition.CompositeCondition; |
| 10 | +import com.aliyun.openservices.ots.model.condition.RelationalCondition; |
| 11 | + |
| 12 | +import java.util.Iterator; |
| 13 | +import java.util.Random; |
| 14 | + |
| 15 | +/** |
| 16 | + * 该示例代码包含了如何使用OTS的filter功能 |
| 17 | + */ |
| 18 | +public class OTSFilterSample { |
| 19 | + |
| 20 | + private static final String COLUMN_GID_NAME = "gid"; |
| 21 | + private static final String COLUMN_UID_NAME = "uid"; |
| 22 | + private static final String COLUMN_NAME_NAME = "name"; |
| 23 | + private static final String COLUMN_ADDRESS_NAME = "address"; |
| 24 | + private static final String COLUMN_AGE_NAME = "age"; |
| 25 | + private static final String COLUMN_IS_STUDENT_NAME = "isstudent"; |
| 26 | + |
| 27 | + public static void main(String args[]) { |
| 28 | + final String endPoint = ""; |
| 29 | + final String accessId = ""; |
| 30 | + final String accessKey = ""; |
| 31 | + final String instanceName = ""; |
| 32 | + |
| 33 | + OTSClient client = new OTSClient(endPoint, accessId, accessKey, instanceName); |
| 34 | + final String tableName = "FilterSampleTable"; |
| 35 | + |
| 36 | + try{ |
| 37 | + // 创建表 |
| 38 | + createTable(client, tableName); |
| 39 | + |
| 40 | + // 注意:创建表只是提交请求,OTS创建表需要一段时间。 |
| 41 | + // 这里简单地等待5秒,请根据您的实际逻辑修改。 |
| 42 | + Thread.sleep(5 * 1000); |
| 43 | + |
| 44 | + // 构造一些数据供查询测试 |
| 45 | + generateData(client, tableName); |
| 46 | + |
| 47 | + getRowWithFilter(client, tableName); |
| 48 | + |
| 49 | + getRangeWithFilter(client, tableName); |
| 50 | + } catch(ServiceException e) { |
| 51 | + System.err.println("操作失败,详情:" + e.getMessage()); |
| 52 | + // 可以根据错误代码做出处理, OTS的ErrorCode定义在OTSErrorCode中。 |
| 53 | + if (OTSErrorCode.QUOTA_EXHAUSTED.equals(e.getErrorCode())){ |
| 54 | + System.err.println("超出存储配额。"); |
| 55 | + } |
| 56 | + // Request ID可以用于有问题时联系客服诊断异常。 |
| 57 | + System.err.println("Request ID:" + e.getRequestId()); |
| 58 | + } catch(ClientException e) { |
| 59 | + // 可能是网络不好或者是返回结果有问题 |
| 60 | + System.err.println("请求失败,详情:" + e.getMessage()); |
| 61 | + } catch (InterruptedException e) { |
| 62 | + System.err.println(e.getMessage()); |
| 63 | + } finally{ |
| 64 | + // 不留垃圾。 |
| 65 | + try { |
| 66 | + deleteTable(client, tableName); |
| 67 | + } catch (ServiceException e) { |
| 68 | + System.err.println("删除表格失败,原因:" + e.getMessage()); |
| 69 | + e.printStackTrace(); |
| 70 | + } catch (ClientException e) { |
| 71 | + System.err.println("删除表格请求失败,原因:" + e.getMessage()); |
| 72 | + e.printStackTrace(); |
| 73 | + } |
| 74 | + client.shutdown(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private static void createTable(OTSClient client, String tableName) |
| 79 | + throws ServiceException, ClientException{ |
| 80 | + TableMeta tableMeta = new TableMeta(tableName); |
| 81 | + tableMeta.addPrimaryKeyColumn(COLUMN_GID_NAME, PrimaryKeyType.INTEGER); |
| 82 | + tableMeta.addPrimaryKeyColumn(COLUMN_UID_NAME, PrimaryKeyType.INTEGER); |
| 83 | + // 将该表的读写CU都设置为0 |
| 84 | + CapacityUnit capacityUnit = new CapacityUnit(0, 0); |
| 85 | + |
| 86 | + CreateTableRequest request = new CreateTableRequest(); |
| 87 | + request.setTableMeta(tableMeta); |
| 88 | + request.setReservedThroughput(capacityUnit); |
| 89 | + client.createTable(request); |
| 90 | + |
| 91 | + System.out.println("表已创建"); |
| 92 | + } |
| 93 | + |
| 94 | + private static void generateData(OTSClient client, String tableName) |
| 95 | + throws ServiceException, ClientException{ |
| 96 | + for (int i = 0; i < 100; i++) { |
| 97 | + RowPutChange rowChange = new RowPutChange(tableName); |
| 98 | + RowPrimaryKey primaryKey = new RowPrimaryKey(); |
| 99 | + primaryKey.addPrimaryKeyColumn(COLUMN_GID_NAME, PrimaryKeyValue.fromLong(i)); |
| 100 | + primaryKey.addPrimaryKeyColumn(COLUMN_UID_NAME, PrimaryKeyValue.fromLong(i + 1)); |
| 101 | + rowChange.setPrimaryKey(primaryKey); |
| 102 | + rowChange.addAttributeColumn(COLUMN_NAME_NAME, ColumnValue.fromString("lilei")); |
| 103 | + rowChange.addAttributeColumn(COLUMN_ADDRESS_NAME, ColumnValue.fromString("somewhere")); |
| 104 | + rowChange.addAttributeColumn(COLUMN_AGE_NAME, ColumnValue.fromLong(new Random().nextInt(30) + 10)); |
| 105 | + rowChange.addAttributeColumn(COLUMN_IS_STUDENT_NAME, ColumnValue.fromBoolean(i % 2 == 0)); |
| 106 | + rowChange.setCondition(new Condition(RowExistenceExpectation.EXPECT_NOT_EXIST)); |
| 107 | + |
| 108 | + PutRowRequest request = new PutRowRequest(); |
| 109 | + request.setRowChange(rowChange); |
| 110 | + |
| 111 | + client.putRow(request); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private static void getRowWithFilter(OTSClient client, String tableName) |
| 116 | + throws ServiceException, ClientException{ |
| 117 | + |
| 118 | + SingleRowQueryCriteria criteria = new SingleRowQueryCriteria(tableName); |
| 119 | + RowPrimaryKey primaryKeys = new RowPrimaryKey(); |
| 120 | + primaryKeys.addPrimaryKeyColumn(COLUMN_GID_NAME, PrimaryKeyValue.fromLong(1)); |
| 121 | + primaryKeys.addPrimaryKeyColumn(COLUMN_UID_NAME, PrimaryKeyValue.fromLong(2)); |
| 122 | + criteria.setPrimaryKey(primaryKeys); |
| 123 | + |
| 124 | + // 增加一个查询条件,只有当满足条件 name == 'lilei' 时才返回数据。 |
| 125 | + RelationalCondition filter = new RelationalCondition(COLUMN_NAME_NAME, RelationalCondition.CompareOperator.EQUAL, ColumnValue.fromString("lilei")); |
| 126 | + criteria.setFilter(filter); |
| 127 | + |
| 128 | + GetRowRequest request = new GetRowRequest(); |
| 129 | + request.setRowQueryCriteria(criteria); |
| 130 | + GetRowResult result = client.getRow(request); |
| 131 | + Row row = result.getRow(); |
| 132 | + System.out.println("Row returned (name == 'lilei'): " + row.getColumns()); |
| 133 | + |
| 134 | + // 更改查询条件,只有当满足条件(name == 'lilei' and isstudent == true)时才返回数据。 |
| 135 | + RelationalCondition anotherFilter = new RelationalCondition(COLUMN_IS_STUDENT_NAME, RelationalCondition.CompareOperator.EQUAL, ColumnValue.fromBoolean(true)); |
| 136 | + CompositeCondition cc = new CompositeCondition(CompositeCondition.LogicOperator.AND); |
| 137 | + cc.addCondition(filter); |
| 138 | + cc.addCondition(anotherFilter); |
| 139 | + |
| 140 | + criteria.setFilter(cc); |
| 141 | + // 而目前表中的该行数据不满足该条件,所以不会返回。 |
| 142 | + result = client.getRow(request); |
| 143 | + row = result.getRow(); |
| 144 | + System.out.println("Row returned (name == 'lilei' and isstudent == true): " + row.getColumns()); |
| 145 | + } |
| 146 | + |
| 147 | + private static void getRangeWithFilter(OTSClient client, String tableName) |
| 148 | + throws ServiceException, ClientException { |
| 149 | + // 查询范围为: |
| 150 | + // start primary key = (gid=0, INF_MIN) |
| 151 | + // end primary key = (gid=100, INF_MAX) |
| 152 | + // 且满足条件为: |
| 153 | + // isstudent == true |
| 154 | + // 的所有行。 |
| 155 | + RangeIteratorParameter param = new RangeIteratorParameter(tableName); |
| 156 | + RowPrimaryKey startPk = new RowPrimaryKey(); |
| 157 | + startPk.addPrimaryKeyColumn(COLUMN_GID_NAME, PrimaryKeyValue.fromLong(0)); |
| 158 | + startPk.addPrimaryKeyColumn(COLUMN_UID_NAME, PrimaryKeyValue.INF_MIN); |
| 159 | + RowPrimaryKey endPk = new RowPrimaryKey(); |
| 160 | + endPk.addPrimaryKeyColumn(COLUMN_GID_NAME, PrimaryKeyValue.fromLong(100)); |
| 161 | + endPk.addPrimaryKeyColumn(COLUMN_UID_NAME, PrimaryKeyValue.INF_MAX); |
| 162 | + |
| 163 | + param.setInclusiveStartPrimaryKey(startPk); |
| 164 | + param.setExclusiveEndPrimaryKey(endPk); |
| 165 | + |
| 166 | + RelationalCondition filter = new RelationalCondition(COLUMN_IS_STUDENT_NAME, RelationalCondition.CompareOperator.EQUAL, ColumnValue.fromBoolean(true)); |
| 167 | + param.setFilter(filter); |
| 168 | + |
| 169 | + Iterator<Row> rowIter = client.createRangeIterator(param); |
| 170 | + int totalRows = 0; |
| 171 | + while (rowIter.hasNext()) { |
| 172 | + Row row = rowIter.next(); |
| 173 | + totalRows++; |
| 174 | + System.out.println(row); |
| 175 | + } |
| 176 | + |
| 177 | + System.out.println("Total rows read: " + totalRows); |
| 178 | + } |
| 179 | + |
| 180 | + private static void deleteTable(OTSClient client, String tableName) |
| 181 | + throws ServiceException, ClientException{ |
| 182 | + DeleteTableRequest request = new DeleteTableRequest(); |
| 183 | + request.setTableName(tableName); |
| 184 | + client.deleteTable(request); |
| 185 | + |
| 186 | + System.out.println("表已删除"); |
| 187 | + } |
| 188 | +} |
0 commit comments