Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: readme file add sdk usage cases #66

Merged
merged 1 commit into from
Aug 28, 2024
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
93 changes: 84 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,90 @@ pip install opengemini_client
Getting Started:

```python
from opengemini_client import Client, Address ,Config, BatchConfig
import datetime
from opengemini_client import Client, Config, Address

cfg = Config(address=[Address(host='127.0.0.1', port=8086)],
batch_config=BatchConfig(batch_size=10, batch_interval=10),
timeout=datetime.timedelta(seconds=30), connection_timeout=datetime.timedelta(seconds=10),
gzip_enabled=False, tls_enabled=False
)
if __name__ == "__main__":
config = Config(address=[Address(host='127.0.0.1', port=8086)])
cli = Client(config)
try:
cli.ping(0)
print("ping success")
except Exception as error:
print(f"ping failed, {error}")

```

Create a database:
```python
from opengemini_client import Client, Config, Address

if __name__ == "__main__":
config = Config(address=[Address(host='127.0.0.1', port=8086)])
cli = Client(config)
try:
database = 'test'
res = cli.create_database(database=database)
if res.error is None:
print(f"create database {database} success")
else:
print(f"create database {database} failed, {res.error}")
except Exception as error:
print(f"create database {database} failed, {error}")

```

Write points:

```python
from opengemini_client import Client, Config, Address, Point, BatchPoints, Precision

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

```

Do a query:

```python
from opengemini_client import Client, Config, Address, Query

if __name__ == "__main__":
config = Config(address=[Address(host='127.0.0.1', port=8086)])
cli = Client(config)
try:
database = 'test'
measurement = 'test_measurement'
query = Query(
database=database,
command=f'select * from {measurement}',
retention_policy=''
)
res = cli.query(query=query)
if res.error is not None:
print(f"query error, {res.error}")
else:
for result in res.results:
if result.error is not None:
print(f"query result error, {result.error}")
continue
for s in result.series:
print(f"name={s.name}, columns={s.columns}, values={s.values}")
except Exception as error:
print(f"query failed, {error}")

cli = Client(cfg)
cli.ping(0)
```
93 changes: 84 additions & 9 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,90 @@ pip install opengemini_client
快速开始:

```python
from opengemini_client import Client, Address ,Config, BatchConfig
import datetime
from opengemini_client import Client, Config, Address

cfg = Config(address=[Address(host='127.0.0.1', port=8086)],
batch_config=BatchConfig(batch_size=10, batch_interval=10),
timeout=datetime.timedelta(seconds=30), connection_timeout=datetime.timedelta(seconds=10),
gzip_enabled=False, tls_enabled=False
)
if __name__ == "__main__":
config = Config(address=[Address(host='127.0.0.1', port=8086)])
cli = Client(config)
try:
cli.ping(0)
print("ping success")
except Exception as error:
print(f"ping failed, {error}")

```

创建数据库:
```python
from opengemini_client import Client, Config, Address

if __name__ == "__main__":
config = Config(address=[Address(host='127.0.0.1', port=8086)])
cli = Client(config)
try:
database = 'test'
res = cli.create_database(database=database)
if res.error is None:
print(f"create database {database} success")
else:
print(f"create database {database} failed, {res.error}")
except Exception as error:
print(f"create database {database} failed, {error}")

```

写入points:

```python
from opengemini_client import Client, Config, Address, Point, BatchPoints, Precision

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

```

查询:

```python
from opengemini_client import Client, Config, Address, Query

if __name__ == "__main__":
config = Config(address=[Address(host='127.0.0.1', port=8086)])
cli = Client(config)
try:
database = 'test'
measurement = 'test_measurement'
query = Query(
database=database,
command=f'select * from {measurement}',
retention_policy=''
)
res = cli.query(query=query)
if res.error is not None:
print(f"query error, {res.error}")
else:
for result in res.results:
if result.error is not None:
print(f"query result error, {result.error}")
continue
for s in result.series:
print(f"name={s.name}, columns={s.columns}, values={s.values}")
except Exception as error:
print(f"query failed, {error}")

cli = Client(cfg)
cli.ping(0)
```