|
| 1 | +/*********************************************************** |
| 2 | + * Author:潭潭 |
| 3 | + * 样例说明: |
| 4 | + * 基于表格存储的时序模型[Timestream]创建的快递轨迹管理系统 |
| 5 | + * 功能: |
| 6 | + * 1、快递的meta管理、查询; |
| 7 | + * 2、快递轨迹路线追踪; |
| 8 | + * 3、快递员meta管理、查询; |
| 9 | + * *********************************************************/ |
| 10 | +package com.aliyun.tablestore; |
| 11 | + |
| 12 | +import com.alicloud.openservices.tablestore.AsyncClient; |
| 13 | +import com.alicloud.openservices.tablestore.timestream.TimestreamDBClient; |
| 14 | +import com.alicloud.openservices.tablestore.timestream.TimestreamDBConfiguration; |
| 15 | +import com.alicloud.openservices.tablestore.timestream.TimestreamDataTable; |
| 16 | +import com.alicloud.openservices.tablestore.timestream.TimestreamMetaTable; |
| 17 | +import com.alicloud.openservices.tablestore.timestream.model.AttributeIndexSchema; |
| 18 | +import com.alicloud.openservices.tablestore.timestream.model.Point; |
| 19 | +import com.alicloud.openservices.tablestore.timestream.model.TimestreamIdentifier; |
| 20 | +import com.alicloud.openservices.tablestore.timestream.model.TimestreamMeta; |
| 21 | +import com.alicloud.openservices.tablestore.timestream.model.filter.*; |
| 22 | +import com.aliyun.tablestore.common.Conf; |
| 23 | + |
| 24 | +import java.util.*; |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | + |
| 27 | +import static com.aliyun.tablestore.common.Util.*; |
| 28 | + |
| 29 | + |
| 30 | +public class ManageService { |
| 31 | + private String mailMetaTableName = "defaultMetaTableName"; |
| 32 | + private String mailDataTableName = "defaultDataTableName"; |
| 33 | + private TimestreamDBClient mailDb; |
| 34 | + |
| 35 | + private AsyncClient asyncClient = null; |
| 36 | + private String seperator = "/"; |
| 37 | + private Random Rand = new Random(79); |
| 38 | + |
| 39 | + ManageService (String mailMetaTableName, String mailDataTableName) { |
| 40 | + this.mailMetaTableName = mailMetaTableName; |
| 41 | + this.mailDataTableName = mailDataTableName; |
| 42 | + } |
| 43 | + |
| 44 | + public void init() { |
| 45 | + String os = System.getProperty("os.name"); |
| 46 | + if (os.toLowerCase().startsWith("win")) { |
| 47 | + seperator = "\\"; |
| 48 | + } |
| 49 | + Conf conf = Conf.newInstance(System.getProperty("user.home") + seperator + "tablestoreConf.json"); |
| 50 | + asyncClient = new AsyncClient( |
| 51 | + conf.getEndpoint(), |
| 52 | + conf.getAccessId(), |
| 53 | + conf.getAccessKey(), |
| 54 | + conf.getInstanceName()); |
| 55 | + |
| 56 | + TimestreamDBConfiguration mailConf = new TimestreamDBConfiguration(mailMetaTableName); |
| 57 | + mailDb = new TimestreamDBClient(asyncClient, mailConf); |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + /**************************************** management of mails ****************************************/ |
| 62 | + public void createMailTable() { |
| 63 | + mailDb.createMetaTable(Arrays.asList( |
| 64 | + new AttributeIndexSchema("fromMobile", AttributeIndexSchema.Type.KEYWORD), |
| 65 | + new AttributeIndexSchema("fromName", AttributeIndexSchema.Type.KEYWORD), |
| 66 | + new AttributeIndexSchema("toMobile", AttributeIndexSchema.Type.KEYWORD), |
| 67 | + new AttributeIndexSchema("toName", AttributeIndexSchema.Type.KEYWORD) |
| 68 | + )); |
| 69 | + mailDb.createDataTable(mailDataTableName); |
| 70 | + } |
| 71 | + |
| 72 | + public void deleteMailTable() { |
| 73 | + mailDb.deleteDataTable(mailDataTableName); |
| 74 | + mailDb.deleteMetaTable(); |
| 75 | + } |
| 76 | + |
| 77 | + public void writeMail() { |
| 78 | + List<String> whos = Arrays.asList("张三", "李四", "王五", "赵六"); |
| 79 | + List<String> actions = Arrays.asList("取件", "派送", "中转" , "中转", "签收"); |
| 80 | + List<String> wheres = Arrays.asList( |
| 81 | + "杭州西湖区转塘", |
| 82 | + "吉林省长春市创业大街", |
| 83 | + "上海市闵行区东川路", |
| 84 | + "北京东站南广场西", |
| 85 | + "上海市浦东新区陆家嘴世纪金融广场" |
| 86 | + ); |
| 87 | + TimestreamDataTable dataWriter = mailDb.dataTable(mailDataTableName); |
| 88 | + TimestreamMetaTable metaWriter = mailDb.metaTable(); |
| 89 | + |
| 90 | + for (int no = 0; no < 100; no++) { |
| 91 | + String mId = formatMId(no); |
| 92 | + |
| 93 | + TimestreamIdentifier identifier = new TimestreamIdentifier.Builder(mId) |
| 94 | + .build(); |
| 95 | + |
| 96 | + TimestreamMeta meta = new TimestreamMeta(identifier) |
| 97 | + .addAttribute("fromName", whos.get(Rand.nextInt(whos.size()))) |
| 98 | + .addAttribute("fromMobile", "15812345678") |
| 99 | + .addAttribute("toName", whos.get(Rand.nextInt(whos.size()))) |
| 100 | + .addAttribute("toMobile", "15812345678"); |
| 101 | + |
| 102 | + for (int i = 0; i < 5; i++) { |
| 103 | + String addr = wheres.get(Rand.nextInt(wheres.size())); |
| 104 | + String location = getMailRandomLocation(); |
| 105 | + |
| 106 | + dataWriter.write( |
| 107 | + meta.getIdentifier(), |
| 108 | + new Point.Builder(i, TimeUnit.SECONDS) |
| 109 | + .addField("who", whos.get(i % whos.size())) |
| 110 | + .addField("do", actions.get(i % actions.size())) |
| 111 | + .addField("where", addr) |
| 112 | + .addField("location", location) |
| 113 | + .build() |
| 114 | + ); |
| 115 | + |
| 116 | + if (i == 0) { |
| 117 | + meta.addAttribute("fromAddr", addr); |
| 118 | + meta.addAttribute("fromLocation", location); |
| 119 | + } else if (i == 5 - 1) { |
| 120 | + meta.addAttribute("toAddr", addr); |
| 121 | + meta.addAttribute("toLocation", location); |
| 122 | + } |
| 123 | + } |
| 124 | + metaWriter.put(meta); |
| 125 | + } |
| 126 | + waitForSync(); |
| 127 | + } |
| 128 | + |
| 129 | + public void searchMail() { |
| 130 | + Filter filter = new AndFilter( |
| 131 | + Name.equal("m0000000001"), |
| 132 | + Attribute.equal("fromMobile", "15812345678") |
| 133 | + ); |
| 134 | + |
| 135 | + Iterator<TimestreamMeta> metaIterator = mailDb.metaTable() |
| 136 | + .filter(filter) |
| 137 | + .fetchAll(); |
| 138 | + |
| 139 | + while (metaIterator.hasNext()) { |
| 140 | + System.out.println(metaIterator.next().getIdentifier().getName()); |
| 141 | + } |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | + public void scanMailTrace() { |
| 146 | + Filter filter = Name.equal("m0000000001"); |
| 147 | + Iterator<TimestreamMeta> metaIterator = mailDb.metaTable() |
| 148 | + .filter(filter) |
| 149 | + .fetchAll(); |
| 150 | + |
| 151 | + //Cause mailId is unique in mail meta,searching by mailId gets none or only one meta; |
| 152 | + TimestreamMeta meta = metaIterator.next(); |
| 153 | + |
| 154 | + Iterator<Point> dataIterator = mailDb.dataTable(mailDataTableName) |
| 155 | + .get(meta.getIdentifier()) |
| 156 | + .fetchAll(); |
| 157 | + |
| 158 | + while (dataIterator.hasNext()) { |
| 159 | + System.out.println("\t" + dataIterator.next().getFields()); |
| 160 | + } |
| 161 | + |
| 162 | + } |
| 163 | + |
| 164 | + |
| 165 | + public void close() { |
| 166 | + mailDb.close(); |
| 167 | + } |
| 168 | + |
| 169 | + /**************************************** private tool functions ****************************************/ |
| 170 | + private void waitForSync() { |
| 171 | + waitForSync(10000); |
| 172 | + } |
| 173 | + |
| 174 | + private void waitForSync(long time) { |
| 175 | + try { |
| 176 | + Thread.sleep(time); |
| 177 | + } catch (InterruptedException e) { |
| 178 | + e.printStackTrace(); |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments