hi
The following error occurred while testing pydgraph
Traceback (most recent call last):
File "/Users/henson/PycharmProjects/personal/temp/dgraph/tls_example.py", line 71, in <module>
main()
File "/Users/henson/PycharmProjects/personal/temp/dgraph/tls_example.py", line 32, in main
client.alter(pydgraph.Operation(drop_all=True))
File "/usr/local/var/pyenv/versions/py373/lib/python3.7/site-packages/pydgraph/client.py", line 112, in alter
self._common_except_alter(error)
File "/usr/local/var/pyenv/versions/py373/lib/python3.7/site-packages/pydgraph/client.py", line 122, in _common_except_alter
raise error
File "/usr/local/var/pyenv/versions/py373/lib/python3.7/site-packages/pydgraph/client.py", line 100, in alter
credentials=credentials)
File "/usr/local/var/pyenv/versions/py373/lib/python3.7/site-packages/pydgraph/client_stub.py", line 46, in alter
credentials=credentials)
File "/usr/local/var/pyenv/versions/py373/lib/python3.7/site-packages/grpc/_channel.py", line 826, in __call__
return _end_unary_response_blocking(state, call, False, None)
File "/usr/local/var/pyenv/versions/py373/lib/python3.7/site-packages/grpc/_channel.py", line 729, in _end_unary_response_blocking
raise _InactiveRpcError(state)
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
status = StatusCode.UNAVAILABLE
details = "failed to connect to all addresses"
debug_error_string = "{"created":"@1592552261.602447000","description":"Failed to pick subchannel","file":"src/core/ext/filters/client_channel/client_channel.cc","file_line":3981,"referenced_errors":[{"created":"@1592552261.602444000","description":"failed to connect to all addresses","file":"src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc","file_line":394,"grpc_status":14}]}"
>
The steps are as follows
Start dgraph using Docker-Compose
# This Docker Compose file can be used to quickly bootup Dgraph Zero
# and Alpha in different Docker containers.
# It mounts /tmp/data on the host machine to /dgraph within the
# container. You can change /tmp/data to a more appropriate location.
# Run `docker-compose up` to start Dgraph.
version: "3.2"
services:
zero:
image: dgraph/dgraph:latest
volumes:
- /tmp/data:/dgraph
- ./tls:/dgraph/tls
ports:
- 5080:5080
- 6080:6080
restart: on-failure
command: dgraph zero --my=zero:5080
alpha:
image: dgraph/dgraph:latest
volumes:
- /tmp/data:/dgraph
- ./tls:/dgraph/tls
ports:
- 8080:8080
- 9080:9080
restart: on-failure
command: dgraph alpha --my=alpha:7080 --lru_mb=2048 --zero=zero:5080 --tls_dir tls
ratel:
image: dgraph/dgraph:latest
ports:
- 8000:8000
command: dgraph-ratel
Generate certificate files
dgraph cert -n live -c dgraphuser
The directory structure is as follows
├── docker-compose.yml
├── example.py
├── tls
│ ├── ca.crt
│ ├── ca.key
│ ├── client.dgraphuser.crt
│ ├── client.dgraphuser.key
│ ├── node.crt
│ └── node.key
└── tls_example.py
tls_example.py
# -*- coding: utf-8 -*-
# @Time : 2019/11/8 8:51 上午
# @Author : Henson
# @Email : henson_wu@foxmail.com
# @File : api_test.py
import pydgraph
import grpc
def create_client(addr='127.0.0.1:9080'):
# Read certs
with open('./tls/ca.crt', 'rb') as f:
root_ca_cert = f.read()
with open('./tls/client.dgraphuser.key', 'rb') as f:
client_cert_key = f.read()
with open('./tls/client.dgraphuser.crt', 'rb') as f:
client_cert = f.read()
# Connect to Dgraph via gRPC with mutual TLS.
creds = grpc.ssl_channel_credentials(root_certificates=root_ca_cert,
private_key=client_cert_key,
certificate_chain=client_cert)
client_stub = pydgraph.DgraphClientStub(addr, credentials=creds)
return pydgraph.DgraphClient(client_stub)
def main():
client = create_client('127.0.0.1:9080')
# client.login("groot", "password")
# Drop all
client.alter(pydgraph.Operation(drop_all=True))
# Update schema
schema = '''
name: string @index(exact) .
description: string .
url: string .
'''
op = pydgraph.Operation(schema=schema)
client.alter(op)
# Mutate
dgraph = {
"name": "Dgraph",
"description": "Scalable, Distributed, Low Latency Graph Database",
"url": "https://dgraph.io"
}
txn = client.txn()
try:
txn.mutate(set_obj=dgraph)
txn.commit()
finally:
txn.discard()
# Query
res = client.txn(read_only=True).query('''
query dgraph($name: string) {
data(func: eq(name, $name)) {
uid
name
description
url
}
}
''', variables={"$name": "Dgraph"})
print(res.json)
if __name__ == '__main__':
main()
An error occurred when executing the “python tls_example.py” command
Did something go wrong?