Skip to content

Commit 65cfcbe

Browse files
Jeevananthan-23chayimshacharPash
authored
pipeline examples (#75)
* added pipelineexamples and docs * adding pipelinewithAsync example * Adding a README to the nuget package (#76) * Adding a README to the nuget package * readme path * fixing readme path * adding pipelinewithasync doc feedback changes * feedback changes * fix examples * fix JsonWithSearchPipeline test * Add delay before search * Change to one consractor for Pipeline that get IDatabase --------- Co-authored-by: Chayim <[email protected]> Co-authored-by: shacharPash <[email protected]>
1 parent 787a308 commit 65cfcbe

File tree

10 files changed

+349
-32
lines changed

10 files changed

+349
-32
lines changed

.github/workflows/integration.yml

+2
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,5 @@ jobs:
4343
token: ${{secrets.CODECOV_TOKEN}}
4444
verbose: true
4545

46+
- name: Build
47+
run: dotnet pack -c Release --output .

Examples/AsyncExample.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33

44
## All methods have synchronous & asynchronous implementation. the asynchronous methods all end ...Async(...), and are fully await-able. here is an example of using the async methods:
55

6-
### Connect to the Redis server and get a reference to the database and for JSON commands:
6+
Connect to the Redis server and get a reference to the database and for JSON commands:
77

88
```csharp
9-
var redis = await ConnectionMultiplexer.ConnectAsync("localhost");
10-
var db = redis.GetDatabase();
11-
var json = db.JSON();
9+
var redis = await ConnectionMultiplexer.ConnectAsync("localhost");
10+
var db = redis.GetDatabase();
11+
var json = db.JSON();
1212
```
1313

14-
### call async version of JSON.SET/GET
14+
call async version of JSON.SET/GET
1515

1616
```csharp
17-
await json.SetAsync("key", "$", new { name = "John", age = 30, city = "New York" });
18-
var john = await json.GetAsync("key");
17+
await json.SetAsync("key", "$", new { name = "John", age = 30, city = "New York" });
18+
var john = await json.GetAsync("key");
1919
```
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Combination modules Pipeline
2+
## An example of pipelines mixing a pipeline with a combination of module commands with JSON & Search
3+
4+
Connect to the Redis server:
5+
```csharp
6+
var redis = ConnectionMultiplexer.Connect("localhost");
7+
```
8+
9+
Setup pipeline connection
10+
```csharp
11+
var db = redis.GetDatabase();
12+
var pipeline = new Pipeline(db);
13+
```
14+
15+
## JSON
16+
Add JsonSet to pipeline
17+
```csharp
18+
pipeline.Json.SetAsync("person:01", "$", new { name = "John", age = 30, city = "New York" });
19+
pipeline.Json.SetAsync("person:02", "$", new { name = "Joy", age = 25, city = "Los Angeles" });
20+
pipeline.Json.SetAsync("person:03", "$", new { name = "Mark", age = 21, city = "Chicago" });
21+
pipeline.Json.SetAsync("person:04", "$", new { name = "Steve", age = 24, city = "Phoenix" });
22+
pipeline.Json.SetAsync("person:05", "$", new { name = "Michael", age = 55, city = "San Antonio" });
23+
```
24+
25+
## Search
26+
Create the schema to index name as text field, age as a numeric field and city as tag field.
27+
```csharp
28+
var schema = new Schema().AddTextField("name").AddNumericField("age", true).AddTagField("city");
29+
```
30+
31+
Filter the index to only include Jsons with prefix of person:
32+
```csharp
33+
var parameters = FTCreateParams.CreateParams().On(IndexDataType.JSON).Prefix("person:");
34+
```
35+
36+
Create the index via pipeline
37+
```csharp
38+
pipeline.Ft.CreateAsync("person-idx", parameters, schema);
39+
```
40+
41+
Search for all indexed person records
42+
```csharp
43+
var getAllPersons = db.FT().SearchAsync("person-idx", new Query());
44+
```
45+
46+
Execute the pipeline
47+
```csharp
48+
pipeline.Execute();
49+
```

Examples/HsetAndSearch.md

+14-15
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
21
# HSET and Search
32
## An example of mixing Redis open source command (HSET) with Redis Stack Redis commands (FT.CREATE & FT.SEARCH)
43

5-
### Connect to the Redis server:
4+
Connect to the Redis server:
65
```csharp
76
var redis = ConnectionMultiplexer.Connect("localhost");
87
```
9-
### Get a reference to the database and for search commands:
8+
Get a reference to the database and for search commands:
109
```csharp
1110
var db = redis.GetDatabase();
1211
var ft = db.FT();
1312
```
14-
### Use HSET to add a field-value pair to a hash:
13+
Use HSET to add a field-value pair to a hash:
1514
```csharp
1615
db.HashSet("profesor:5555", new HashEntry[] { new("first", "Albert"), new("last", "Blue"), new("age", "55") });
1716
db.HashSet("student:1111", new HashEntry[] { new("first", "Joe"), new("last", "Dod"), new("age", "18") });
@@ -22,40 +21,40 @@ db.HashSet("student:5555", new HashEntry[] { new("first", "Joen"), new("last", "
2221
db.HashSet("teacher:6666", new HashEntry[] { new("first", "Pat"), new("last", "Rod"), new("age", "20") });
2322
```
2423

25-
### Create the schema to index first and last as text fields, and age as a numeric field:
24+
Create the schema to index first and last as text fields, and age as a numeric field:
2625
```csharp
2726
var schema = new Schema().AddTextField("first").AddTextField("last").AddNumericField("age");
2827
```
29-
### Filter the index to only include hashes with an age greater than 16, and prefix of 'student:' or 'pupil:'
28+
Filter the index to only include hashes with an age greater than 16, and prefix of 'student:' or 'pupil:'
3029
```csharp
3130
var parameters = FTCreateParams.CreateParams().Filter("@age>16").Prefix("student:", "pupil:");
3231
```
33-
### Create the index:
32+
Create the index:
3433
```csharp
3534
ft.Create("example_index", parameters, schema);
3635
```
3736
## Search Examples:
3837

39-
### Search all hashes in the index:
38+
Search all hashes in the index:
4039
```csharp
4140
var noFilters = ft.Search("example_index", new Query());
4241
```
43-
### _noFilters_ now contains: _student:1111_, _student:5555_, _pupil:4444_, _student:3333_.<br /><br />
42+
_noFilters_ now contains: _student:1111_, _student:5555_, _pupil:4444_, _student:3333_.<br /><br />
4443

45-
### Search for hashes with a first name starting with Jo
44+
Search for hashes with a first name starting with Jo
4645
```csharp
4746
var startWithJo = ft.Search("example_index", new Query("@first:Jo*"));
4847
```
49-
### _startWithJo_ now contains: _student:1111_ (Joe), _student:5555_ (Joen).<br /><br />
48+
_startWithJo_ now contains: _student:1111_ (Joe), _student:5555_ (Joen).<br /><br />
5049

51-
### Search for hashes with first name of Pat
50+
Search for hashes with first name of Pat
5251
```csharp
5352
var namedPat = ft.Search("example_index", new Query("@first:Pat"));
5453
```
55-
### _namedPat_ now contains _pupil:4444_ (Pat). _teacher:6666_ (Pat) is not included because it does not have a prefix of 'student:' or 'pupil:'<br /><br />
54+
_namedPat_ now contains _pupil:4444_ (Pat). _teacher:6666_ (Pat) is not included because it does not have a prefix of 'student:' or 'pupil:'<br /><br />
5655

57-
### Search for hashes with last name of Rod
56+
Search for hashes with last name of Rod
5857
```csharp
5958
var lastNameRod = ft.Search("example_index", new Query("@last:Rod"));
6059
```
61-
### _lastNameRod_ is empty because there are no hashes with a last name of Rod that match the index definition.
60+
_lastNameRod_ is empty because there are no hashes with a last name of Rod that match the index definition.

Examples/PipelineExample.md

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Pipeline
2+
## An example of pipelines Redis Stack Redis commands (JSON.SET & JSON.CLEAR & JSON.GET)
3+
4+
Connect to the Redis server and Setup new Pipeline
5+
```csharp
6+
IDatabase db = redisFixture.Redis.GetDatabase();
7+
var pipeline = new Pipeline(db);
8+
```
9+
10+
11+
Add JsonSet to pipeline
12+
```csharp
13+
pipeline.Json.SetAsync("person", "$", new { name = "John", age = 30, city = "New York", nicknames = new[] { "John", "Johny", "Jo" } });
14+
```
15+
16+
Increase age by 2
17+
```csharp
18+
pipeline.Json.NumIncrbyAsync("person", "$.age", 2);
19+
```
20+
21+
Clear the nicknames from the Json
22+
```csharp
23+
pipeline.Json.ClearAsync("person", "$.nicknames");
24+
```
25+
26+
Delete the nicknames
27+
```csharp
28+
pipeline.Json.DelAsync("person", "$.nicknames");
29+
```
30+
31+
Get the Json response
32+
```csharp
33+
var getResponse = pipeline.Json.GetAsync("person");
34+
```
35+
36+
Execute pipeline
37+
```csharp
38+
pipeline.Execute();
39+
```
40+
41+
Get the result of getResponse
42+
```csharp
43+
var result = getResponse.Result;
44+
```
45+
now result is:
46+
```json
47+
{
48+
"name": "John",
49+
"age": 32,
50+
"city": "New York"
51+
}
52+
```

Examples/PipelineWithAsync.md

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Pipeline With Async
2+
## An example of pipelines Redis Stack Redis commands (JSON.SET & JSON.CLEAR & JSON.GET)
3+
4+
Connect to the Redis server
5+
```csharp
6+
var redis = ConnectionMultiplexer.Connect("localhost");
7+
```
8+
9+
Get a reference to the database
10+
```csharp
11+
var db = redis.GetDatabase();
12+
```
13+
14+
Setup pipeline connection
15+
```csharp
16+
var pipeline = new Pipeline(db);
17+
```
18+
19+
Create metedata lables for time-series.
20+
```csharp
21+
TimeSeriesLabel label1 = new TimeSeriesLabel("temp", "TLV");
22+
TimeSeriesLabel label2 = new TimeSeriesLabel("temp", "JLM");
23+
var labels1 = new List<TimeSeriesLabel> { label1 };
24+
var labels2 = new List<TimeSeriesLabel> { label2 };
25+
```
26+
27+
Create a new time-series.
28+
```csharp
29+
pipeline.Ts.CreateAsync("temp:TLV", labels: labels1);
30+
pipeline.Ts.CreateAsync("temp:JLM", labels: labels2);
31+
```
32+
33+
Adding multiple sequenece of time-series data.
34+
```csharp
35+
List<(string, TimeStamp, double)> sequence1 = new List<(string, TimeStamp, double)>()
36+
{
37+
("temp:TLV",1000,30),
38+
("temp:TLV", 1010 ,35),
39+
("temp:TLV", 1020, 9999),
40+
("temp:TLV", 1030, 40)
41+
};
42+
List<(string, TimeStamp, double)> sequence2 = new List<(string, TimeStamp, double)>()
43+
{
44+
("temp:JLM",1005,30),
45+
("temp:JLM", 1015 ,35),
46+
("temp:JLM", 1025, 9999),
47+
("temp:JLM", 1035, 40)
48+
};
49+
```
50+
Adding mutiple samples to mutiple series.
51+
```csharp
52+
pipeline.Ts.MAddAsync(sequence1);
53+
pipeline.Ts.MAddAsync(sequence2);
54+
```
55+
56+
Execute the pipeline
57+
```csharp
58+
pipeline.Execute();
59+
```
60+
61+
Get a reference to the database and for time-series commands
62+
```csharp
63+
var ts = db.TS();
64+
```
65+
66+
Get only the location label for each last sample, use SELECTED_LABELS.
67+
```csharp
68+
var respons = await ts.MGetAsync(new List<string> { "temp=JLM" }, selectedLabels: new List<string> { "location" });
69+
```

src/NRedisStack/NRedisStack.csproj

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
<Authors>Redis Open Source</Authors>
88
<Owners>Redis OSS</Owners>
99
<Description>.Net Client for Redis Stack</Description>
10+
<PackageReadmeFile>README.md</PackageReadmeFile>
1011
<Version>0.5.0</Version>
1112
<ReleaseVersion>0.5.0</ReleaseVersion>
1213
<PackageVersion>0.5.0</PackageVersion>
1314
</PropertyGroup>
1415

1516
<ItemGroup>
16-
<PackageReference Include="StackExchange.Redis" Version="2.6.45" />
17+
<PackageReference Include="StackExchange.Redis" Version="2.6.45" />
18+
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
1719
</ItemGroup>
1820

1921
</Project>

src/NRedisStack/Pipeline.cs

-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ namespace NRedisStack;
44

55
public class Pipeline
66
{
7-
public Pipeline(IConnectionMultiplexer muxer)
8-
{
9-
_batch = muxer.GetDatabase().CreateBatch();
10-
}
11-
127
public Pipeline(IDatabase db)
138
{
149
_batch = db.CreateBatch();

0 commit comments

Comments
 (0)