Skip to content

pipeline examples #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Feb 5, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Examples/CombinationModulesPipeline.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Combination modules Pipeline
## An example of pipelines mixing a pipeline with a combination of module commands with JSON & Search

### Connect to the Redis server:
```csharp
var redis = ConnectionMultiplexer.Connect("localhost");
```

### Setup pipeline connection
```csharp
var pipeline = new Pipeline(ConnectionMultiplexer.Connect("localhost"));
```

### Add JsonSet to pipeline
```csharp
pipeline.Json.SetAsync("person:01", "$", new { name = "John", age = 30, city = "New York" });
pipeline.Json.SetAsync("person:02", "$", new { name = "Joy", age = 25, city = "Los Angeles" });
pipeline.Json.SetAsync("person:03", "$", new { name = "Mark", age = 21, city = "Chicago" });
pipeline.Json.SetAsync("person:04", "$", new { name = "Steve", age = 24, city = "Phoenix" });
pipeline.Json.SetAsync("person:05", "$", new { name = "Michael", age = 55, city = "San Antonio" });
```

### Create the schema to index first and age as a numeric field
```csharp
var schema = new Schema().AddTextField("name").AddNumericField("age", true).AddTagField("city");
```

### Filter the index to only include Jsons and prefix of person
```csharp
var parameters = FTCreateParams.CreateParams().On(Literals.Enums.IndexDataType.JSON).Prefix("person:");
```

### Create the index via pipeline
```csharp
pipeline.Ft.CreateAsync("person-idx", parameters, schema);
```

### Search for all indexed person records
```csharp
var getAllPersons = pipeline.Ft.SearchAsync("person-idx", new Query());
```

### Execute the pipeline
```csharp
pipeline.Execute();
```
42 changes: 42 additions & 0 deletions Examples/PipelineExample.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Pipeline
## An example of pipelines Redis Stack Redis commands (JSON.SET & JSON.CLEAR & JSON.GET)

### Connect to the Redis server:
```csharp
var redis = ConnectionMultiplexer.Connect("localhost");
```

### Setup pipeline connection
```csharp
var pipeline = new Pipeline(ConnectionMultiplexer.Connect("localhost"));
```

### Add JsonSet to pipeline
```csharp
pipeline.Json.SetAsync("person", "$", new { name = "John", age = 30, city = "New York", nicknames = new[] { "John", "Johny", "Jo" } });
```

### Inc age by 2
```csharp
pipeline.Json.NumIncrbyAsync("person", "$.age", 2);
```

### Clear the nicknames from the Json
```csharp
pipeline.Json.ClearAsync("person", "$.nicknames");
```

### Del the nicknames
```csharp
pipeline.Json.DelAsync("person", "$.nicknames");
```

### Get the Json response
```csharp
var getResponse = pipeline.Json.GetAsync("person");
```

### Execute the pipeline
```csharp
pipeline.Execute();
```
110 changes: 110 additions & 0 deletions tests/NRedisStack.Tests/Examples/ExamplesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Moq;
using NRedisStack.Search.FT.CREATE;
using NRedisStack.Search;
using NRedisStack.DataTypes;

namespace NRedisStack.Tests.Bloom;

Expand Down Expand Up @@ -73,4 +74,113 @@ public async Task AsyncExample()
await json.SetAsync("key", "$", new { name = "John", age = 30, city = "New York" });
var john = await json.GetAsync("key");
}

[Fact]
public void PipelineExample()
{
//Setup pipeline connection
var pipeline = new Pipeline(ConnectionMultiplexer.Connect("localhost"));

// Add JsonSet to pipeline
pipeline.Json.SetAsync("person", "$", new { name = "John", age = 30, city = "New York", nicknames = new[] { "John", "Johny", "Jo" } });

// Inc age by 2
pipeline.Json.NumIncrbyAsync("person", "$.age", 2);

// Clear the nicknames from the Json
pipeline.Json.ClearAsync("person", "$.nicknames");

// Del the nicknames
pipeline.Json.DelAsync("person", "$.nicknames");

// Get the Json response
var getResponse = pipeline.Json.GetAsync("person");

// Execute the pipeline
pipeline.Execute();
}

[Fact]
public void JsonWithSearchPipeline()
{
// Setup pipeline connection
var pipeline = new Pipeline(ConnectionMultiplexer.Connect("localhost"));

// Add JsonSet to pipeline
pipeline.Json.SetAsync("person:01", "$", new { name = "John", age = 30, city = "New York" });
pipeline.Json.SetAsync("person:02", "$", new { name = "Joy", age = 25, city = "Los Angeles" });
pipeline.Json.SetAsync("person:03", "$", new { name = "Mark", age = 21, city = "Chicago" });
pipeline.Json.SetAsync("person:04", "$", new { name = "Steve", age = 24, city = "Phoenix" });
pipeline.Json.SetAsync("person:05", "$", new { name = "Michael", age = 55, city = "San Antonio" });

// Create the schema to index first and age as a numeric field
var schema = new Schema().AddTextField("name").AddNumericField("age", true).AddTagField("city");

// Filter the index to only include Jsons and prefix of person
var parameters = FTCreateParams.CreateParams().On(Literals.Enums.IndexDataType.JSON).Prefix("person:");

// Create the index via pipeline
pipeline.Ft.CreateAsync("person-idx", parameters, schema);

// Search for all indexed person records
var getAllPersons = pipeline.Ft.SearchAsync("person-idx", new Query());

// execute the pipeline
pipeline.Execute();
}

[Fact]
public async Task PipelineWithAsync()
{
// Connect to the Redis server
var redis = ConnectionMultiplexer.Connect("localhost");

// Get a reference to the database
var db = redis.GetDatabase();
var ts = db.TS();

// Setup pipeline connection
var pipeline = new Pipeline(redis);

// create metedata lables for time-series.
TimeSeriesLabel label1 = new TimeSeriesLabel("temp", "TLV");
TimeSeriesLabel label2 = new TimeSeriesLabel("temp", "JLM");
var labels1 = new List<TimeSeriesLabel> { label1 };
var labels2 = new List<TimeSeriesLabel> { label2 };

//create a new time-series.
pipeline.Ts.CreateAsync("temp:TLV", labels: labels1);
pipeline.Ts.CreateAsync("temp:JLM", labels: labels2);

//adding multiple sequenece of time-series data.
List<(string, TimeStamp, double)> sequence1 = new List<(string, TimeStamp, double)>()
{
("temp:TLV",1000,30),
("temp:TLV", 1010 ,35),
("temp:TLV", 1020, 9999),
("temp:TLV", 1030, 40)
};
List<(string, TimeStamp, double)> sequence2 = new List<(string, TimeStamp, double)>()
{
("temp:JLM",1005,30),
("temp:JLM", 1015 ,35),
("temp:JLM", 1025, 9999),
("temp:JLM", 1035, 40)
};

// Adding mutiple samples to mutiple series.
pipeline.Ts.MAddAsync(sequence1);
pipeline.Ts.MAddAsync(sequence2);

pipeline.Execute();

// get only the location label for each last sample, use SELECTED_LABELS.
var respons = await ts.MGetAsync(new List<string> { "temp=JLM" }, selectedLabels: new List<string> { "location" });
}

[Fact]
public void TransactionExample()
{
// implementation for transaction
}
}