diff --git a/README.md b/README.md index 4ce0d53..0e0314f 100644 --- a/README.md +++ b/README.md @@ -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}") + +``` diff --git a/README_CN.md b/README_CN.md index 480b1ac..7f310e0 100644 --- a/README_CN.md +++ b/README_CN.md @@ -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}") + +``` diff --git a/opengemini_client/client.py b/opengemini_client/client.py index e29450f..1e4c94a 100644 --- a/opengemini_client/client.py +++ b/opengemini_client/client.py @@ -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 """ diff --git a/opengemini_client/client_impl.py b/opengemini_client/client_impl.py index 15c8ee9..add99f2 100644 --- a/opengemini_client/client_impl.py +++ b/opengemini_client/client_impl.py @@ -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: diff --git a/opengemini_client/models.py b/opengemini_client/models.py index 56a5fe0..adc27d7 100644 --- a/opengemini_client/models.py +++ b/opengemini_client/models.py @@ -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 @@ -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