Using Python to Operate GridGain
To use Python to operate GridGain database connections and perform data insertion, querying, modification, and deletion, it is necessary to use the PyGridGain library. The following is a complete Python example code that demonstrates how to complete these operations.
Firstly, ensure that the PyGridGain library is installed. You can install it using the following command:
python
pip install pygridgain
Next, import the required modules:
python
from pygridgain import Client
from pygridgain.datatypes.cache_config import CacheMode, CacheAtomicityMode
Initialize the GridGain client connection as needed in the code:
python
client = Client()
client.connect('localhost', 10800)
It is recommended to change the host name and port number of the GridGain connection according to the actual situation.
Next, you can perform data insertion, query, modification, and deletion operations.
1. Data insertion:
python
cache = client.get_or_create_cache('my_cache')
#Insert Data
for i in range(10):
cache.put(i, str(i))
2. Data query:
python
#Obtain values based on keys
value = cache.get(5)
print(value)
#Query all data
query = cache.query_iterable()
for key, value in query:
print(key, value)
3. Data modification:
python
#Update data
cache.put(5, 'updated_value')
4. Data deletion:
python
#Delete data based on key
cache.remove_key(5)
#Delete all data
cache.clear()
After completing the operation, you can close the GridGain client connection:
python
client.close()
This is a basic example that demonstrates how to use Python to operate GridGain database connections and data insertion, querying, modification, and deletion. According to actual needs, other functions provided by PyGridGain can also be used for more complex operations and data processing.