Skip to content

Commit 3edd5a5

Browse files
committed
feat: add optional parameter rp to the write points method
Signed-off-by: yehao <[email protected]>
1 parent 0bb9317 commit 3edd5a5

File tree

5 files changed

+67
-5
lines changed

5 files changed

+67
-5
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,34 @@ if __name__ == "__main__":
152152
print(f"ping failed, {error}")
153153

154154
```
155+
156+
Write points using GRPC protocol:
157+
158+
```python
159+
from datetime import datetime
160+
from opengemini_client import Client, Config, Address, Point, BatchPoints, Precision
161+
from opengemini_client.models import GrpcConfig
162+
163+
if __name__ == "__main__":
164+
config = Config(address=[Address(host='127.0.0.1', port=8086)],
165+
grpc_config=GrpcConfig(
166+
address=[Address(host='127.0.0.1', port=8305)],
167+
))
168+
cli = Client(config)
169+
try:
170+
database = 'test'
171+
measurement = 'test_measurement'
172+
point = Point(
173+
measurement=measurement,
174+
precision=Precision.PrecisionSecond,
175+
fields={'Humidity': 87, 'Temperature': 25},
176+
tags={'Weather': 'foggy'},
177+
timestamp=datetime.now(),
178+
)
179+
batch_points = BatchPoints(points=[point])
180+
cli.write_by_grpc(database=database, batch_points=batch_points)
181+
print(f"write points success")
182+
except Exception as error:
183+
print(f"write points failed, {error}")
184+
185+
```

README_CN.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,33 @@ if __name__ == "__main__":
153153
print(f"ping failed, {error}")
154154

155155
```
156+
157+
使用grpc协议写入points:
158+
```python
159+
from datetime import datetime
160+
from opengemini_client import Client, Config, Address, Point, BatchPoints, Precision
161+
from opengemini_client.models import GrpcConfig
162+
163+
if __name__ == "__main__":
164+
config = Config(address=[Address(host='127.0.0.1', port=8086)],
165+
grpc_config=GrpcConfig(
166+
address=[Address(host='127.0.0.1', port=8305)],
167+
))
168+
cli = Client(config)
169+
try:
170+
database = 'test'
171+
measurement = 'test_measurement'
172+
point = Point(
173+
measurement=measurement,
174+
precision=Precision.PrecisionSecond,
175+
fields={'Humidity': 87, 'Temperature': 25},
176+
tags={'Weather': 'foggy'},
177+
timestamp=datetime.now(),
178+
)
179+
batch_points = BatchPoints(points=[point])
180+
cli.write_by_grpc(database=database, batch_points=batch_points)
181+
print(f"write points success")
182+
except Exception as error:
183+
print(f"write points failed, {error}")
184+
185+
```

opengemini_client/client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ def query(self, query: Query) -> QueryResult:
4444
"""
4545

4646
@abstractmethod
47-
def write_batch_points(self, database: str, batch_points: BatchPoints):
47+
def write_batch_points(self, database: str, batch_points: BatchPoints, rp: str = ''):
4848
"""
4949
batch points to assigned database
5050
:param database: name
5151
:param batch_points: BatchPoints object
52+
:param rp: retention policy
5253
:return: return an error message
5354
"""
5455

opengemini_client/client_impl.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,9 @@ def _query_post(self, query: Query) -> QueryResult:
223223
return resolve_query_body(resp)
224224
raise HTTPError(f"query_post error resp, code: {resp.status_code}, body: {resp.text}")
225225

226-
def write_batch_points(self, database: str, batch_points: BatchPoints):
226+
def write_batch_points(self, database: str, batch_points: BatchPoints, rp: str = ''):
227227
server_url = self._get_server_url()
228-
params = {'db': database}
228+
params = {'db': database, 'rp': rp}
229229
with io.StringIO() as writer:
230230
for point in batch_points.points:
231231
if point is None:

opengemini_client/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from dataclasses import field, dataclass
1717
from datetime import datetime, timedelta
1818
from enum import Enum
19-
from typing import Dict, Union, Optional, List, Any
19+
from typing import Dict, Union, Optional, List, Any, Iterable
2020

2121

2222
@dataclass
@@ -236,7 +236,7 @@ def write_timestamp(self, writer: io.StringIO):
236236

237237
@dataclass
238238
class BatchPoints:
239-
points: List[Point] = field(default_factory=list)
239+
points: Iterable[Point] = field(default_factory=list)
240240

241241

242242
@dataclass

0 commit comments

Comments
 (0)