使用场景:

    • 大数据量支持PB
    • Nosql

    多元索引功能类似es,但是个人用户不建议,因为有预留实例,每小时0.02元,因此,暂时放弃使用。
    二级索引类似mysql,但是没有mysql好用。

    官网控制台
    官方实例
    官方文档

    1. <dependency>
    2. <groupId>com.aliyun.fc.runtime</groupId>
    3. <artifactId>fc-java-core</artifactId>
    4. <version>1.3.0</version>
    5. </dependency>
    1. package com.alvin.service.impl;
    2. import com.alicloud.openservices.tablestore.ClientException;
    3. import com.alicloud.openservices.tablestore.SyncClient;
    4. import com.alicloud.openservices.tablestore.TableStoreException;
    5. import com.alicloud.openservices.tablestore.model.*;
    6. import com.alicloud.openservices.tablestore.model.search.SearchQuery;
    7. import com.alicloud.openservices.tablestore.model.search.SearchRequest;
    8. import com.alicloud.openservices.tablestore.model.search.SearchResponse;
    9. import com.alicloud.openservices.tablestore.model.search.agg.AggregationBuilders;
    10. import com.alicloud.openservices.tablestore.model.search.agg.CountAggregation;
    11. import com.alicloud.openservices.tablestore.model.search.query.QueryBuilder;
    12. import com.alicloud.openservices.tablestore.model.search.query.QueryBuilders;
    13. import com.alvin.entity.Picture;
    14. import com.alvin.entity.PictureVO;
    15. import com.alvin.service.RandomPicService;
    16. import org.apache.commons.io.FileUtils;
    17. import org.springframework.stereotype.Service;
    18. import java.io.*;
    19. import java.util.List;
    20. import java.util.Random;
    21. /**
    22. * @Description
    23. * @Author 田云
    24. * @Date 2020/3/19 21:20
    25. * @Version 1.0
    26. */
    27. @Service
    28. public class RandomPicServiceImpl implements RandomPicService {
    29. public static final String INDEX = "picture_index";
    30. //static String endPoint = "123";
    31. static String endPoint = "123";
    32. static String accessKeyId = "123";
    33. static String accessKeySecret = "123";
    34. static String instanceName = "randomPic";
    35. public static String TABLE_NAME = "picture";
    36. public static SyncClient client = new SyncClient(endPoint, accessKeyId, accessKeySecret, instanceName);
    37. /**
    38. * 随机url
    39. *
    40. * @param type
    41. * @return
    42. */
    43. @Override
    44. public String randomUrl(String type) {
    45. QueryBuilder builder;
    46. if (type == null) {
    47. builder = QueryBuilders.matchAll();
    48. } else {
    49. builder = QueryBuilders.term(Picture.Fields.type, type);
    50. }
    51. CountAggregation.Builder count1 = AggregationBuilders.count("count", Picture.Fields.type);
    52. /*
    53. 查询多少条数据
    54. */
    55. SearchRequest searchRequest = SearchRequest.newBuilder()
    56. .tableName(TABLE_NAME)
    57. .indexName(INDEX)
    58. .searchQuery(
    59. SearchQuery.newBuilder()
    60. .query(builder)
    61. .limit(0)
    62. .addAggregation(count1)
    63. .build())
    64. .build();
    65. System.out.println(searchRequest);
    66. SearchResponse resp = client.search(searchRequest);
    67. //获取统计聚合的结果
    68. long count = resp.getAggregationResults().getAsCountAggregationResult("count").getValue();
    69. /*
    70. 再次查询
    71. */
    72. int i = new Random().nextInt((int) count);
    73. SearchRequest searchRequest2 = SearchRequest.newBuilder()
    74. .tableName(TABLE_NAME)
    75. .indexName("picture_index")
    76. .addColumnsToGet(Picture.Fields.type)
    77. .searchQuery(
    78. SearchQuery.newBuilder()
    79. .query(builder)
    80. .limit(1)
    81. .offset(i)
    82. .addAggregation(count1)
    83. .build())
    84. .build();
    85. SearchResponse resp2 = client.search(searchRequest2);
    86. Row row = resp2.getRows().get(0);
    87. return String.valueOf(row.getPrimaryKey().getPrimaryKeyColumn("url").getValue());
    88. }
    89. private Picture getPicture(Row row) {
    90. return Picture.builder()
    91. .url(String.valueOf(row.getPrimaryKey().getPrimaryKeyColumn("url").getValue()))
    92. .type(String.valueOf(row.getColumns()[0].getValue()))
    93. .build();
    94. }
    95. @Override
    96. public void inserts(PictureVO pictureVO) {
    97. BatchWriteRowRequest batchWriteRowRequest = new BatchWriteRowRequest();
    98. for (String url : pictureVO.getUrls()) {
    99. //描述主键信息
    100. PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
    101. primaryKeyBuilder.addPrimaryKeyColumn(Picture.Fields.url, PrimaryKeyValue.fromString(url));
    102. PrimaryKey primaryKey = primaryKeyBuilder.build();
    103. RowPutChange rowPutChange = new RowPutChange(TABLE_NAME, primaryKey);
    104. //描述属性信息
    105. rowPutChange.addColumn(new Column(Picture.Fields.type, ColumnValue.fromString(pictureVO.getType())));
    106. batchWriteRowRequest.addRowChange(rowPutChange);
    107. }
    108. batchWrite(batchWriteRowRequest);
    109. }
    110. @Override
    111. public void deletes(PictureVO pictureVO) {
    112. BatchWriteRowRequest batchWriteRowRequest = new BatchWriteRowRequest();
    113. for (String url : pictureVO.getUrls()) {
    114. //描述主键信息
    115. PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
    116. primaryKeyBuilder.addPrimaryKeyColumn(Picture.Fields.url, PrimaryKeyValue.fromString(url));
    117. PrimaryKey primaryKey = primaryKeyBuilder.build();
    118. RowDeleteChange rowPutChange = new RowDeleteChange(TABLE_NAME, primaryKey);
    119. batchWriteRowRequest.addRowChange(rowPutChange);
    120. }
    121. batchWrite(batchWriteRowRequest);
    122. }
    123. @Override
    124. public Picture queryByUrl(String url) {
    125. // 构造主键
    126. PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
    127. primaryKeyBuilder.addPrimaryKeyColumn(Picture.Fields.url, PrimaryKeyValue.fromString(url));
    128. PrimaryKey primaryKey = primaryKeyBuilder.build();
    129. // 读一行
    130. SingleRowQueryCriteria criteria = new SingleRowQueryCriteria(TABLE_NAME, primaryKey);
    131. // 设置读取最新版本
    132. criteria.setMaxVersions(1);
    133. criteria.addColumnsToGet(Picture.Fields.type);
    134. GetRowResponse getRowResponse = client.getRow(new GetRowRequest(criteria));
    135. Row row = getRowResponse.getRow();
    136. System.out.println("读取完毕,结果为: ");
    137. System.out.println(row);
    138. return getPicture(row);
    139. }
    140. private void batchWrite(BatchWriteRowRequest batchWriteRowRequest) {
    141. BatchWriteRowResponse response = client.batchWriteRow(batchWriteRowRequest);
    142. System.out.println("是否全部成功:" + response.isAllSucceed());
    143. if (!response.isAllSucceed()) {
    144. for (BatchWriteRowResponse.RowResult rowResult : response.getFailedRows()) {
    145. System.out.println("失败的行:" + batchWriteRowRequest.getRowChange(rowResult.getTableName(), rowResult.getIndex()).getPrimaryKey());
    146. System.out.println("失败原因:" + rowResult.getError());
    147. }
    148. /**
    149. * 可以通过createRequestForRetry方法再构造一个请求对失败的行进行重试。这里只给出构造重试请求的部分。
    150. * 推荐的重试方法是使用SDK的自定义重试策略功能,支持对batch操作的部分行错误进行重试。设定重试策略后,调用接口处即不需要增加重试代码。
    151. */
    152. BatchWriteRowRequest retryRequest = batchWriteRowRequest.createRequestForRetry(response.getFailedRows());
    153. }
    154. }
    155. /**
    156. * 导入数据
    157. *
    158. * @param args
    159. */
    160. public static void main(String[] args) throws Exception {
    161. //ExportData("out.txt");
    162. //importData("out.txt");
    163. }
    164. public static void importData(String path) throws IOException {
    165. List<String> strings = FileUtils.readLines(new File(path), "utf-8");
    166. for (String string : strings) {
    167. String[] split = string.split("\t");
    168. if (split.length != 2) {
    169. continue;
    170. }
    171. //描述主键信息
    172. PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
    173. primaryKeyBuilder.addPrimaryKeyColumn(Picture.Fields.url, PrimaryKeyValue.fromString(split[0]));
    174. PrimaryKey primaryKey = primaryKeyBuilder.build();
    175. RowPutChange rowPutChange = new RowPutChange(TABLE_NAME, primaryKey);
    176. //描述属性信息
    177. rowPutChange.addColumn(new Column(Picture.Fields.type, ColumnValue.fromString(split[1])));
    178. //添加新数据到表格
    179. try {
    180. client.putRow(new PutRowRequest(rowPutChange));
    181. System.out.println("添加数据成功。");
    182. } catch (TableStoreException e) {
    183. System.err.println("操作失败,详情:" + e.getMessage());
    184. System.err.println("Request ID:" + e.getRequestId());
    185. } catch (ClientException e) {
    186. System.err.println("请求失败,详情:" + e.getMessage());
    187. }
    188. }
    189. System.out.println("ok");
    190. }
    191. public static void ExportData(String path) throws Exception{
    192. SearchRequest searchRequest2 = SearchRequest.newBuilder()
    193. .tableName(TABLE_NAME)
    194. .indexName("picture_index")
    195. .addColumnsToGet(Picture.Fields.type)
    196. .searchQuery(
    197. SearchQuery.newBuilder()
    198. .query(QueryBuilders.matchAll())
    199. .limit(100)
    200. .offset(0)
    201. .build())
    202. .build();
    203. SearchResponse resp2 = client.search(searchRequest2);
    204. List<Row> rows = resp2.getRows();
    205. BufferedWriter bw = null;
    206. bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path)));
    207. for (Row row : rows) {
    208. Picture picture = new RandomPicServiceImpl().getPicture(row);
    209. bw.write(picture.getUrl() + "\t" + picture.getType() + System.lineSeparator());
    210. }
    211. bw.close();
    212. System.out.println("ok");
    213. }
    214. }