Skip to content

Commit 4bf4af1

Browse files
author
伯箫
committed
feature: searchIndex&globalIndex&atomicIncrement
1 parent add84b4 commit 4bf4af1

File tree

106 files changed

+33360
-34710
lines changed

Some content is hidden

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

106 files changed

+33360
-34710
lines changed

CHANGELOG.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@
2020
- 连接池的默认连接个数从50调整到300
2121
- 新增conditional update功能
2222

23-
## 版本号 4.0.0 日期2018/09/07
23+
## 版本号 4.0.0 日期:2018/09/07
2424
### 变更内容
25-
- 数据传输改为ProtocolBuffer加PlainBuffer
25+
- 数据传输改为ProtocolBuffer加PlainBuffer  
2626
- 支持自增列
27-
- 支持多版本
27+
- 支持多版本
28+
29+
## 版本号 4.1.0 日期:2018/12/17
30+
### 变更内容
31+
- 支持多元索引(SearchIndex)
32+
- 支持全局二级索引(GlobalIndex)
33+
- 支持原子增

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
- 阿里云表格存储是构建在阿里云飞天分布式系统之上的NoSQL数据存储服务,提供海量结构化数据的存储和实时访问。
1010

1111
## 版本
12-
- 当前版本:4.0.0
12+
- 当前版本:4.1.0
1313

1414
## 运行环境
1515
### Windows

aliyun-tablestore-csharp-sdk.sln

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

2-
Microsoft Visual Studio Solution File, Format Version 11.00
3-
# Visual Studio 2010
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28010.2050
5+
MinimumVisualStudioVersion = 10.0.40219.1
46
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aliyun-tablestore-sdk", "sdk\aliyun-tablestore-sdk.csproj", "{AB5EFCA2-53A9-42B5-861E-3FD6F865036B}"
57
EndProject
68
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aliyun-tablestore-sdk-test", "test\aliyun-tablestore-sdk-test.csproj", "{F30E0874-399A-4FBD-B72B-74EE36B31AF6}"
@@ -29,4 +31,7 @@ Global
2931
GlobalSection(SolutionProperties) = preSolution
3032
HideSolutionNode = FALSE
3133
EndGlobalSection
34+
GlobalSection(ExtensibilityGlobals) = postSolution
35+
SolutionGuid = {9E3B6113-EAB1-4E91-9D47-ADB022C414DC}
36+
EndGlobalSection
3237
EndGlobal

sample/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
32
namespace Aliyun.OTS.Samples
43
{
54
class Program
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Aliyun.OTS.DataModel;
4+
using Aliyun.OTS.Request;
5+
using Aliyun.OTS.Response;
6+
7+
namespace Aliyun.OTS.Samples.Samples
8+
{
9+
/// <summary>
10+
/// 原子自增示例
11+
/// 可以指定某一列为原子自增列,每次按照指定的数值进行累加。
12+
/// </summary>
13+
public class AtomicIncrementSample
14+
{
15+
private static readonly string TableName = "AtomicIncrementSample";
16+
17+
private static readonly string Pk1 = "Pk1";
18+
private static readonly string Pk2 = "Pk2";
19+
private static readonly string IncrementCol = "IncrementCol";
20+
21+
22+
static void Main(string[] args)
23+
{
24+
Console.WriteLine("AtomicIncrementSample");
25+
26+
//准备表
27+
PrepareTable();
28+
29+
//新增一行,设置IncrementCol这个属性列的初始值为0
30+
PutRow();
31+
32+
//对IncrementCol执行原子自增,10次,每次加1
33+
for (int i = 0; i < 10; i++)
34+
{
35+
Increment(1);
36+
}
37+
38+
//获取自增后的列
39+
GetRow();
40+
41+
Console.ReadLine();
42+
43+
}
44+
45+
46+
private static void PrepareTable()
47+
{
48+
// 创建表
49+
OTSClient otsClient = Config.GetClient();
50+
51+
IList<string> tables = otsClient.ListTable(new ListTableRequest()).TableNames;
52+
if (tables.Contains(TableName))
53+
{
54+
return;
55+
}
56+
57+
PrimaryKeySchema primaryKeySchema = new PrimaryKeySchema
58+
{
59+
{ Pk1, ColumnValueType.Integer },
60+
{ Pk2, ColumnValueType.String }
61+
};
62+
TableMeta tableMeta = new TableMeta(TableName, primaryKeySchema);
63+
64+
CapacityUnit reservedThroughput = new CapacityUnit(0, 0);
65+
CreateTableRequest request = new CreateTableRequest(tableMeta, reservedThroughput);
66+
otsClient.CreateTable(request);
67+
68+
}
69+
70+
public static void PutRow()
71+
{
72+
Console.WriteLine("Start put row...");
73+
OTSClient otsClient = Config.GetClient();
74+
75+
// 定义行的主键,必须与创建表时的TableMeta中定义的一致
76+
PrimaryKey primaryKey = new PrimaryKey
77+
{
78+
{ Pk1, new ColumnValue(0) },
79+
{ Pk2, new ColumnValue("abc") }
80+
};
81+
82+
// 定义要写入该行的属性列
83+
AttributeColumns attribute = new AttributeColumns
84+
{
85+
{ IncrementCol, new ColumnValue(0) }
86+
};
87+
PutRowRequest request = new PutRowRequest(TableName, new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute);
88+
89+
otsClient.PutRow(request);
90+
Console.WriteLine("Put row succeed.");
91+
}
92+
93+
94+
/// <summary>
95+
/// 更新行的时候,指定某一列为原子自增列,并对这一列进行原子自增
96+
/// </summary>
97+
public static void Increment(int incrementValue)
98+
{
99+
Console.WriteLine("Start set increment column...");
100+
OTSClient otsClient = Config.GetClient();
101+
102+
// 定义行的主键,必须与创建表时的TableMeta中定义的一致
103+
PrimaryKey primaryKey = new PrimaryKey
104+
{
105+
{ Pk1, new ColumnValue(0) },
106+
{ Pk2, new ColumnValue("abc") }
107+
};
108+
RowUpdateChange rowUpdateChange = new RowUpdateChange(TableName, primaryKey);
109+
rowUpdateChange.ReturnType = ReturnType.RT_AFTER_MODIFY;
110+
rowUpdateChange.ReturnColumnNames = new List<string>() { IncrementCol};
111+
//设置一个原子自增列,这一列从0开始自增,每次增增加1。
112+
rowUpdateChange.Increment(new Column(IncrementCol, new ColumnValue(incrementValue)));
113+
114+
UpdateRowRequest updateRowRequest = new UpdateRowRequest(rowUpdateChange);
115+
116+
var response = otsClient.UpdateRow(updateRowRequest);
117+
Console.WriteLine("set Increment column succeed,Increment result:" + response.Row.GetColumns()[0].Value);
118+
}
119+
120+
121+
public static void GetRow()
122+
{
123+
Console.WriteLine("Start get row...");
124+
PrepareTable();
125+
OTSClient otsClient = Config.GetClient();
126+
127+
// 定义行的主键,必须与创建表时的TableMeta中定义的一致
128+
PrimaryKey primaryKey = new PrimaryKey
129+
{
130+
{ Pk1, new ColumnValue(0) },
131+
{ Pk2, new ColumnValue("abc") }
132+
};
133+
134+
GetRowRequest request = new GetRowRequest(TableName, primaryKey); // 未指定读哪列,默认读整行
135+
GetRowResponse response = otsClient.GetRow(request);
136+
PrimaryKey primaryKeyRead = response.PrimaryKey;
137+
AttributeColumns attributesRead = response.Attribute;
138+
139+
Console.WriteLine("Primary key read: ");
140+
foreach (KeyValuePair<string, ColumnValue> entry in primaryKeyRead)
141+
{
142+
Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value));
143+
}
144+
145+
Console.WriteLine("Attributes read: ");
146+
foreach (KeyValuePair<string, ColumnValue> entry in attributesRead)
147+
{
148+
Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value));
149+
}
150+
151+
Console.WriteLine("Get row succeed.");
152+
}
153+
154+
155+
156+
private static string PrintColumnValue(ColumnValue value)
157+
{
158+
switch (value.Type)
159+
{
160+
case ColumnValueType.String: return value.StringValue;
161+
case ColumnValueType.Integer: return value.IntegerValue.ToString();
162+
case ColumnValueType.Boolean: return value.BooleanValue.ToString();
163+
case ColumnValueType.Double: return value.DoubleValue.ToString();
164+
case ColumnValueType.Binary: return value.BinaryValue.ToString();
165+
}
166+
167+
throw new Exception("Unknow type.");
168+
}
169+
170+
171+
}
172+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Aliyun.OTS.DataModel;
4+
using Aliyun.OTS.Request;
5+
using Aliyun.OTS.Response;
6+
7+
namespace Aliyun.OTS.Samples.Samples
8+
{
9+
/// <summary>
10+
/// 主键列自增示例
11+
/// </summary>
12+
public class AutoIncrementSample
13+
{
14+
private static readonly string TableName = "AutoIncrementSample";
15+
16+
private static readonly string Pk1 = "Pk1";
17+
private static readonly string Pk2 = "Pk2_AutoIncrement";
18+
19+
20+
static void Main(string[] args)
21+
{
22+
Console.WriteLine("AutoIncrementSample");
23+
24+
//创建一个带自增列的表
25+
CreateTableWithAutoIncrementPk();
26+
27+
//写入10行,自增列Pk2将
28+
for (int i = 0; i < 10; i++)
29+
{
30+
PutRow(i.ToString());
31+
}
32+
33+
Console.ReadLine();
34+
35+
}
36+
37+
/// <summary>
38+
/// 创建一个带自增列的表
39+
/// </summary>
40+
private static void CreateTableWithAutoIncrementPk()
41+
{
42+
43+
OTSClient otsClient = Config.GetClient();
44+
45+
IList<string> tables = otsClient.ListTable(new ListTableRequest()).TableNames;
46+
if (tables.Contains(TableName))
47+
{
48+
return;
49+
}
50+
51+
PrimaryKeySchema primaryKeySchema = new PrimaryKeySchema
52+
{
53+
{ Pk1, ColumnValueType.String },
54+
//指定Pk2为自增列主键
55+
{ Pk2, ColumnValueType.Integer, PrimaryKeyOption.AUTO_INCREMENT}
56+
};
57+
TableMeta tableMeta = new TableMeta(TableName, primaryKeySchema);
58+
59+
CapacityUnit reservedThroughput = new CapacityUnit(0, 0);
60+
CreateTableRequest request = new CreateTableRequest(tableMeta, reservedThroughput);
61+
otsClient.CreateTable(request);
62+
63+
}
64+
65+
public static void PutRow(string pk1Value)
66+
{
67+
Console.WriteLine("Start put row...");
68+
OTSClient otsClient = Config.GetClient();
69+
70+
// 定义行的主键,必须与创建表时的TableMeta中定义的一致
71+
PrimaryKey primaryKey = new PrimaryKey
72+
{
73+
{ Pk1, new ColumnValue(pk1Value) },
74+
{ Pk2, ColumnValue.AUTO_INCREMENT }
75+
};
76+
77+
// 定义要写入该行的属性列
78+
AttributeColumns attribute = new AttributeColumns
79+
{
80+
{ "Col1", new ColumnValue(0) }
81+
};
82+
PutRowRequest request = new PutRowRequest(TableName, new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute);
83+
request.RowPutChange.ReturnType = ReturnType.RT_PK;
84+
85+
var response = otsClient.PutRow(request);
86+
Console.WriteLine("Put row succeed,autoIncrement Pk value:"+ response.Row.GetPrimaryKey()[Pk2].IntegerValue);
87+
}
88+
89+
}
90+
}

sample/Samples/ConditionUpdateSample.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ private static void PrepareTable()
2727
primaryKeySchema.Add("pk1", ColumnValueType.String);
2828
TableMeta tableMeta = new TableMeta(tableName, primaryKeySchema);
2929

30-
CapacityUnit reservedThroughput = new CapacityUnit(1, 1);
30+
CapacityUnit reservedThroughput = new CapacityUnit(0, 0);
3131
CreateTableRequest request = new CreateTableRequest(tableMeta, reservedThroughput);
3232
otsClient.CreateTable(request);
3333
}

0 commit comments

Comments
 (0)