Skip to content

Commit

Permalink
Merge pull request #37 from RedisGears/added_mysql_example
Browse files Browse the repository at this point in the history
added mysql example
  • Loading branch information
MeirShpilraien authored Oct 18, 2020
2 parents 58e5f15 + 422490f commit c16a737
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 57 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ RGWriteBehind(GB, keysPrefix='car', mappings=carsMappings, connector=carsConnect
```

## Running the recipe
You can use [this utility](https://github.com/RedisGears/gears-cli) to send a RedisGears recipe for execution. For example, run this repository's [example.py recipe](example.py) and install its dependencies with the following command:
You can use [this utility](https://github.com/RedisGears/gears-cli) to send a RedisGears recipe for execution. For example, run this repository's [example.py recipe](examples/mysql/example.py) and install its dependencies with the following command:

```bash
gears-cli --host <host> --port <post> --password <password> run example.py REQUIREMENTS git+https://github.com/RedisGears/rgsync.git PyMySQL
gears-cli --host <host> --port <post> --password <password> run example.py REQUIREMENTS rgsync PyMySQL cryptography
```

## Overview of the recipe's operation
Expand Down
53 changes: 0 additions & 53 deletions example.py

This file was deleted.

46 changes: 46 additions & 0 deletions examples/mysql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Set up MySql DB

## Setup MySql docker
```bash
docker run -p 3306:3306 --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest
```

## Create Persons table
```bash
docker exec -it <mysql container id> /bin/bash
mysql -u root -p # set password my-secret-pw
CREATE DATABASE test;
CREATE TABLE test.persons (person_id VARCHAR(100) NOT NULL, first VARCHAR(100) NOT NULL, last VARCHAR(100) NOT NULL, age INT NOT NULL, PRIMARY KEY (person_id));
CREATE USER 'demouser'@'%' IDENTIFIED BY 'Password123!';
FLUSH PRIVILEGES;
GRANT ALL PRIVILEGES ON test.* to 'demouser'@'%';
FLUSH PRIVILEGES;
```

# Running the recipe
Assuming you have RedisGears up and running (see [Quick Start](https://oss.redislabs.com/redisgears/quickstart.html)). Please use <a href="https://github.com/RedisGears/gears-cli">gears-cli</a> to send a RedisGears Write-Behind and/or Write-Through recipe for execution. For example, run the sample [MySql](example.py) recipe (contains the mapping of MySql tables with Redis Hashes and RedisGears registrations) and install its dependencies with the following command:

```bash
gears-cli run --host <host> --port <port> --password <password> example.py --requirements requirements.txt
```

# Test
Using redis-cli perform:
```bash
redis-cli
> hset person:1 first_name foo last_name bar age 20
```

Make sure data reached MySql server:
```bash
docker exec -it <mysql container id> /bin/bash
mysql -u root -p # set password my-secret-pw
mysql> select * from test.persons;
+-----------+-------+------+-----+
| person_id | first | last | age |
+-----------+-------+------+-----+
| 1 | foo | bar | 20 |
+-----------+-------+------+-----+
1 row in set (0.00 sec)

```
22 changes: 22 additions & 0 deletions examples/mysql/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from rgsync import RGWriteBehind, RGWriteThrough
from rgsync.Connectors import MySqlConnector, MySqlConnection

'''
Create MySQL connection object
'''
connection = MySqlConnection('demouser', 'Password123!', 'localhost:3306/test')

'''
Create MySQL persons connector
'''
personsConnector = MySqlConnector(connection, 'persons', 'person_id')

personsMappings = {
'first_name':'first',
'last_name':'last',
'age':'age'
}

RGWriteBehind(GB, keysPrefix='person', mappings=personsMappings, connector=personsConnector, name='PersonsWriteBehind', version='99.99.99')

RGWriteThrough(GB, keysPrefix='__', mappings=personsMappings, connector=personsConnector, name='PersonsWriteThrough', version='99.99.99')
3 changes: 3 additions & 0 deletions examples/mysql/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
rgsync
PyMySQL
cryptography
29 changes: 29 additions & 0 deletions examples/redis/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Set up Redis Server
To setup a Redis server to replicate data to, start another Redis server on different port (assuming you have Redis installed on the machine)
```bash
redis-server --port 9001
```

## Running the recipe
Please use <a href="https://github.com/RedisGears/gears-cli">gears-cli</a> to send a RedisGears Write-Behind and/or Write-Through recipe for execution.
For example, run the sample [redis](example-redis-standalone.py) recipe (contains the mapping of primary DB Redis Hashes with secondary DB Redis Hashes and RedisGears registrations) and install its dependencies with the following command:
Expand All @@ -8,3 +14,26 @@ gears-cli run --host <host> --port <port> --password <password> example-redis-st

NOTE: Exactly once property is not valid for Redis cluster, because Redis cluster do not support transaction over multiple shards.

# Test
Using redis-cli perform:
```bash
redis-cli
127.0.0.1:6379> hset key:1 bin1 1 bin2 2 bin3 3 bin4 4 bin5 5
(integer) 5
```

Make sure data reached the second Redis server:
```bash
redis-cli -p 9001
127.0.0.1:9001> hgetall key:1
1) "bin1"
2) "1"
3) "bin2"
4) "2"
5) "bin4"
6) "4"
7) "bin3"
8) "3"
9) "bin5"
10) "5"
```
2 changes: 2 additions & 0 deletions examples/redis/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
rgsync
redis
redis-py-cluster
2 changes: 1 addition & 1 deletion examples/sqlite/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rgsync
rgsync
2 changes: 1 addition & 1 deletion testWriteBehind.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def Connect():
class testWriteBehind:
def __init__(self):
self.env = Env()
f = open('./example.py', 'rt')
f = open('./examples/mysql/example.py', 'rt')
script = f.read()
f.close()
self.env.cmd('RG.PYEXECUTE', script, 'REQUIREMENTS', 'pymysql')
Expand Down

0 comments on commit c16a737

Please sign in to comment.