diff --git a/README.md b/README.md index a339abb..595fdfc 100644 --- a/README.md +++ b/README.md @@ -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 --port --password run example.py REQUIREMENTS git+https://github.com/RedisGears/rgsync.git PyMySQL +gears-cli --host --port --password run example.py REQUIREMENTS rgsync PyMySQL cryptography ``` ## Overview of the recipe's operation diff --git a/example.py b/example.py deleted file mode 100644 index d997a84..0000000 --- a/example.py +++ /dev/null @@ -1,53 +0,0 @@ -from rgsync import RGWriteBehind, RGWriteThrough -from rgsync.Connectors import MySqlConnector, MySqlConnection - -''' -Create MySQL connection object -All the arguments to the connection can also be callbacks which will be called each time a reconnect attempt is performed. -Example: -Read from RedisGears configuration using configGet (https://oss.redislabs.com/redisgears/master/runtime.html#configget) function - -def User(): - return configGet('MySqlUser') - -def Password(): - return configGet('MySqlPassword') - -def DB(): - return configGet('MySqlDB') - -connection = MySqlConnection(User, Password, DB) - -''' -connection = MySqlConnection('demouser', 'Password123!', 'localhost:3306/test') - -''' -Create MySQL persons connector -persons - MySQL table to put the data -person_id - primary key -''' -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') - -''' -Create MySQL cars connector -cars - MySQL table to put the data -car_id - primary key -''' -carsConnector = MySqlConnector(connection, 'cars', 'car_id') - -carsMappings = { - 'id':'id', - 'color':'color' -} - -RGWriteBehind(GB, keysPrefix='car', mappings=carsMappings, connector=carsConnector, name='CarsWriteBehind', version='99.99.99') diff --git a/examples/mysql/README.md b/examples/mysql/README.md new file mode 100644 index 0000000..d3355d6 --- /dev/null +++ b/examples/mysql/README.md @@ -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 /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 gears-cli 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 --port --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 /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) + +``` diff --git a/examples/mysql/example.py b/examples/mysql/example.py new file mode 100644 index 0000000..3827a48 --- /dev/null +++ b/examples/mysql/example.py @@ -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') diff --git a/examples/mysql/requirements.txt b/examples/mysql/requirements.txt new file mode 100644 index 0000000..e8806d7 --- /dev/null +++ b/examples/mysql/requirements.txt @@ -0,0 +1,3 @@ +rgsync +PyMySQL +cryptography \ No newline at end of file diff --git a/examples/redis/README.md b/examples/redis/README.md index 1f44df8..a5e9f3f 100644 --- a/examples/redis/README.md +++ b/examples/redis/README.md @@ -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 gears-cli 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: @@ -8,3 +14,26 @@ gears-cli run --host --port --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" +``` diff --git a/examples/redis/requirements.txt b/examples/redis/requirements.txt index 8e78173..4e20f69 100644 --- a/examples/redis/requirements.txt +++ b/examples/redis/requirements.txt @@ -1 +1,3 @@ rgsync +redis +redis-py-cluster diff --git a/examples/sqlite/requirements.txt b/examples/sqlite/requirements.txt index 8e78173..80ba3af 100644 --- a/examples/sqlite/requirements.txt +++ b/examples/sqlite/requirements.txt @@ -1 +1 @@ -rgsync +rgsync \ No newline at end of file diff --git a/testWriteBehind.py b/testWriteBehind.py index 3d2588b..6dfc402 100644 --- a/testWriteBehind.py +++ b/testWriteBehind.py @@ -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')