Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,34 @@ if __name__ == "__main__":
print(f"ping failed, {error}")

```

Write points using GRPC protocol:

```python
from datetime import datetime
from opengemini_client import Client, Config, Address, Point, BatchPoints, Precision
from opengemini_client.models import GrpcConfig

if __name__ == "__main__":
config = Config(address=[Address(host='127.0.0.1', port=8086)],
grpc_config=GrpcConfig(
address=[Address(host='127.0.0.1', port=8305)],
))
cli = Client(config)
try:
database = 'test'
measurement = 'test_measurement'
point = Point(
measurement=measurement,
precision=Precision.PrecisionSecond,
fields={'Humidity': 87, 'Temperature': 25},
tags={'Weather': 'foggy'},
timestamp=datetime.now(),
)
batch_points = BatchPoints(points=[point])
cli.write_by_grpc(database=database, batch_points=batch_points)
print(f"write points success")
except Exception as error:
print(f"write points failed, {error}")

```
30 changes: 30 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,33 @@ if __name__ == "__main__":
print(f"ping failed, {error}")

```

使用grpc协议写入points:
```python
from datetime import datetime
from opengemini_client import Client, Config, Address, Point, BatchPoints, Precision
from opengemini_client.models import GrpcConfig

if __name__ == "__main__":
config = Config(address=[Address(host='127.0.0.1', port=8086)],
grpc_config=GrpcConfig(
address=[Address(host='127.0.0.1', port=8305)],
))
cli = Client(config)
try:
database = 'test'
measurement = 'test_measurement'
point = Point(
measurement=measurement,
precision=Precision.PrecisionSecond,
fields={'Humidity': 87, 'Temperature': 25},
tags={'Weather': 'foggy'},
timestamp=datetime.now(),
)
batch_points = BatchPoints(points=[point])
cli.write_by_grpc(database=database, batch_points=batch_points)
print(f"write points success")
except Exception as error:
print(f"write points failed, {error}")

```
3 changes: 2 additions & 1 deletion opengemini_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ def query(self, query: Query) -> QueryResult:
"""

@abstractmethod
def write_batch_points(self, database: str, batch_points: BatchPoints):
def write_batch_points(self, database: str, batch_points: BatchPoints, rp: str = ''):
"""
batch points to assigned database
:param database: name
:param batch_points: BatchPoints object
:param rp: retention policy
:return: return an error message
"""

Expand Down
4 changes: 2 additions & 2 deletions opengemini_client/client_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ def _query_post(self, query: Query) -> QueryResult:
return resolve_query_body(resp)
raise HTTPError(f"query_post error resp, code: {resp.status_code}, body: {resp.text}")

def write_batch_points(self, database: str, batch_points: BatchPoints):
def write_batch_points(self, database: str, batch_points: BatchPoints, rp: str = ''):
server_url = self._get_server_url()
params = {'db': database}
params = {'db': database, 'rp': rp}
with io.StringIO() as writer:
for point in batch_points.points:
if point is None:
Expand Down
4 changes: 2 additions & 2 deletions opengemini_client/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from dataclasses import field, dataclass
from datetime import datetime, timedelta
from enum import Enum
from typing import Dict, Union, Optional, List, Any
from typing import Dict, Union, Optional, List, Any, Iterable


@dataclass
Expand Down Expand Up @@ -236,7 +236,7 @@ def write_timestamp(self, writer: io.StringIO):

@dataclass
class BatchPoints:
points: List[Point] = field(default_factory=list)
points: Iterable[Point] = field(default_factory=list)


@dataclass
Expand Down