Skip to content

Commit 0212f89

Browse files
author
wangtantan
committed
add TableStore-Grid and 2 Timestream Demos mail&wifi Management
1 parent f3e2704 commit 0212f89

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+3077
-2
lines changed

.gitignore

+14
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,17 @@
2121

2222
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2323
hs_err_pid*
24+
25+
# IDEA Files
26+
*.iml
27+
*.ipr
28+
*.iws
29+
.idea/
30+
31+
# Other Language Built Files
32+
target/
33+
node_modules/
34+
*.pyc
35+
36+
# Others
37+
*.DS_Store

README.md

+89-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,89 @@
1-
# tablestore-examples
2-
Example code for aliyun tablestore.
1+
# Aliyun TableStore Examples
2+
3+
[![License Status](https://img.shields.io/badge/license-apache2-brightgreen.svg)](https://travis-ci.org/aliyun/aliyun-tablestore-nodejs-sdk)
4+
5+
**目录**
6+
- [1、项目结构](#1%E9%A1%B9%E7%9B%AE%E7%BB%93%E6%9E%84)
7+
- [2、开通与配置](#2%E5%BC%80%E9%80%9A%E4%B8%8E%E9%85%8D%E7%BD%AE)
8+
- [开通服务、创建实例、获取AK](#%E5%BC%80%E9%80%9A%E6%9C%8D%E5%8A%A1%E5%88%9B%E5%BB%BA%E5%AE%9E%E4%BE%8B%E8%8E%B7%E5%8F%96ak)
9+
- [完成配置](#%E5%AE%8C%E6%88%90%E9%85%8D%E7%BD%AE)
10+
- [3、样例统计](#3%E6%A0%B7%E4%BE%8B%E7%BB%9F%E8%AE%A1)
11+
- [usages(SDK基础功能)](#usagessdk%E5%9F%BA%E7%A1%80%E5%8A%9F%E8%83%BD)
12+
- [demos(场景样例)](#demos%E5%9C%BA%E6%99%AF%E6%A0%B7%E4%BE%8B)
13+
- [tools(迁移、计算等工具)](#tools%E8%BF%81%E7%A7%BB%E8%AE%A1%E7%AE%97%E7%AD%89%E5%B7%A5%E5%85%B7)
14+
- [4、使用提醒](#4%E4%BD%BF%E7%94%A8%E6%8F%90%E9%86%92)
15+
- [资源释放](#%E8%B5%84%E6%BA%90%E9%87%8A%E6%94%BE)
16+
- [5、咨询/答疑/反馈](#5%E5%92%A8%E8%AF%A2%E7%AD%94%E7%96%91%E5%8F%8D%E9%A6%88)
17+
- [联系方式:](#%E8%81%94%E7%B3%BB%E6%96%B9%E5%BC%8F)
18+
19+
20+
# 1、项目结构
21+
- 根据常用语言,分为3个模块(场景样例/迁移、计算等工具/SDK基础功能),
22+
- 每个模块拆分语言目录:java/go/node/python...
23+
- 语言下每个项目名下对应独立的Example项目
24+
25+
```
26+
├── README.md
27+
├── demos (场景样例)
28+
│   ├── TableStore-Grid (气象格点数据解决方案)
29+
│   ├── MailManagement
30+
│   └── WifiMonitor
31+
32+
├── tools (工具/产品)
33+
34+
└── usages (SDK基础功能)
35+
```
36+
37+
# 2、开通与配置
38+
## 开通服务、创建实例、获取AK
39+
- [控制台](https://ots.console.aliyun.com): https://ots.console.aliyun.com
40+
- [开通服务](https://help.aliyun.com/document_detail/27287.html): https://help.aliyun.com/document_detail/27287.html
41+
- [创建实例](https://help.aliyun.com/document_detail/55211.html): https://help.aliyun.com/document_detail/55211.html
42+
- [获取AK](https://usercenter.console.aliyun.com/#/manage/ak): https://usercenter.console.aliyun.com/#/manage/ak
43+
44+
## 完成配置
45+
在home目录下创建tablestoreCong.json文件,填写相应参数,所有独立项目都会使用该配置
46+
```
47+
# mac 或 linux系统下:/home/userhome/tablestoreCong.json
48+
# windows系统下: C:\Documents and Settings\%用户名%\tablestoreCong.json
49+
{
50+
"endpoint": "http://instanceName.cn-hangzhou.ots.aliyuncs.com",
51+
"accessId": "***********",
52+
"accessKey": "***********************",
53+
"instanceName": "instanceName"
54+
}
55+
```
56+
- endpoint:实例的接入地址,控制台实例详情页获取;
57+
- accessId:AK的ID,获取AK链接提供;
58+
- accessKey:AK的密码,获取AK链接提供;
59+
- instanceName:使用的实例名;
60+
61+
# 3、项目统计
62+
63+
## [demos(场景样例)](/demos)
64+
样例 | 语言 | 项目名
65+
--- | --- | ---
66+
[气象格点数据解决方案](https://yq.aliyun.com/articles/698313) | java | [TableStore-Grid](/demos/TableStore-Grid)
67+
基于Timestream的Wifi监控系统 | java | [WifiMonitor](/demos/WifiMonitor)
68+
基于Timestream的快递轨迹管理 | java | [MailManagement](/demos/MailManagement)
69+
70+
## usages(SDK基础功能)
71+
72+
## tools(迁移、计算等工具)
73+
74+
75+
76+
# 4、使用提醒
77+
78+
## 资源释放
79+
- 删除无用索引、无用数据、无用表格等
80+
- 释放相应资源,避免持续收费
81+
82+
83+
# 5、咨询/答疑/反馈
84+
## 联系方式:
85+
- 钉钉群: 表格存储技术交流群
86+
- 群号: 11789671
87+
- 二维码:
88+
89+
![二维码](http://console-demo.oss-cn-beijing.aliyuncs.com/marker.png?x-oss-process=image/resize,h_400)

demos/MailManagement/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# MailManagement
2+
3+
Manage mail meta and data by Timestream of TableStore.

demos/MailManagement/pom.xml

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.aliyun.tablestore.examples</groupId>
8+
<artifactId>MailManagement</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<project-sourceEncoding>UTF-8</project-sourceEncoding>
13+
</properties>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>com.aliyun.openservices</groupId>
18+
<artifactId>tablestore</artifactId>
19+
<version>4.11.0</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>commons-io</groupId>
23+
<artifactId>commons-io</artifactId>
24+
<version>2.4</version>
25+
</dependency>
26+
27+
</dependencies>
28+
<build>
29+
<plugins>
30+
<!-- compiler plugin -->
31+
<plugin>
32+
<artifactId>maven-compiler-plugin</artifactId>
33+
<configuration>
34+
<source>1.8</source>
35+
<target>1.8</target>
36+
<encoding>${project-sourceEncoding}</encoding>
37+
</configuration>
38+
</plugin>
39+
</plugins>
40+
</build>
41+
42+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.aliyun.tablestore;
2+
3+
public class MailManagement {
4+
5+
public static void main(String[] args) {
6+
ManageService demo = new ManageService("timestream_mail_meta", "timestream_mail_data");
7+
/**
8+
* init timestream:
9+
* the timestream of mail: meta and track data of each mail;
10+
* */
11+
demo.init();
12+
13+
/**
14+
* create meta table and data table for mail:
15+
* 1. mailMetaTableName: which stores the mail meta such as mailing id, mail address and mailing destination;
16+
* 2. mailDataTableName: which stores the location data changing with time;
17+
* */
18+
demo.createMailTable();
19+
demo.writeMail();
20+
21+
22+
demo.searchMail();
23+
demo.scanMailTrace();
24+
25+
/**
26+
* delete all the dataTables
27+
* */
28+
demo.deleteMailTable();
29+
30+
/**
31+
* shut down the connections;
32+
* */
33+
demo.close();
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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

Comments
 (0)