Package: nebula3-python
nebula-pythonThis repository holds the official Python API for NebulaGraph.Before you startBefore you start, please read this section to choose the right branch for you. The compatibility between the API and NebulaGraph service can be found inHow to choose nebula-python. The current master branch is compatible with NebulaGraph 3.x.The directory structure|--nebula-python
    |
    |-- nebula3                               // client code
    |   |-- fbthrift                          // the fbthrift lib code
    |   |-- common
    |   |-- data
    |   |-- graph
    |   |-- meta
    |   |-- net                               // the net code for graph client
    |   |-- storage
    |   |-- Config.py                         // the pool config
    |   |__ Exception.py                      // the define exception
    |
    |-- examples
    |   |-- GraphClientMultiThreadExample.py  // the multi thread example
    |   |-- GraphClientSimpleExample.py       // the simple example
    |   |__ ScanVertexEdgeExample.py
    |
    |-- tests                                 // the test code
    |
    |-- setup.py                              // used to install or package
    |
    |__ README.md                             // the introduction of nebula3-pythonHow to get nebula3-pythonOption one: install with pip# for v3.xpipinstallnebula3-python==$version# for v2.xpipinstallnebula2-python==$versionOption two: install from the source codeClone from GitHubgitclonehttps://github.com/vesoft-inc/nebula-python.gitcdnebula-pythonInstall from sourceFor python version >= 3.7.0pipinstall.For python version >= 3.6.2, < 3.7.0python3setup.pyinstallQuick example to use graph-client to connect graphdfromnebula3.gclient.netimportConnectionPoolfromnebula3.ConfigimportConfig# define a configconfig=Config()config.max_connection_pool_size=10# init connection poolconnection_pool=ConnectionPool()# if the given servers are ok, return true, else return falseok=connection_pool.init([('127.0.0.1',9669)],config)# option 1 control the connection release yourself# get session from the poolsession=connection_pool.get_session('root','nebula')# select spacesession.execute('USE nba')# show tagsresult=session.execute('SHOW TAGS')print(result)# release sessionsession.release()# option 2 with session_context, session will be released automaticallywithconnection_pool.session_context('root','nebula')assession:session.execute('USE nba')result=session.execute('SHOW TAGS')print(result)# close the poolconnection_pool.close()Example of using session poolThere are some limitations while using the session pool:

1. There MUST be an existing space in the DB before initializing the session pool.
2. Each session pool is corresponding to a single USER and a single Space. This is to ensure that the user's access control is consistent. i.g. The same user may have different access privileges in different spaces. If you need to run queries in different spaces, you may have multiple session pools.
3. Every time when sessinPool.execute() is called, the session will execute the query in the space set in the session pool config.
4. Commands that alter passwords or drop users should NOT be executed via session pool.see /example/SessinPoolExample.pyQuick example to fetch result to dataframefromnebula3.gclient.netimportConnectionPoolfromnebula3.ConfigimportConfigimportpandasaspdfromtypingimportDictfromnebula3.data.ResultSetimportResultSetdefresult_to_df(result:ResultSet)->pd.DataFrame:"""build list for each column, and transform to dataframe"""assertresult.is_succeeded()columns=result.keys()d:Dict[str,list]={}forcol_numinrange(result.col_size()):col_name=columns[col_num]col_list=result.column_values(col_name)d[col_name]=[x.cast()forxincol_list]returnpd.DataFrame.from_dict(d,orient='columns')# define a configconfig=Config()# init connection poolconnection_pool=ConnectionPool()# if the given servers are ok, return true, else return falseok=connection_pool.init([('127.0.0.1',9669)],config)# option 2 with session_context, session will be released automaticallywithconnection_pool.session_context('root','nebula')assession:session.execute('USE <your graph space>')result=session.execute('<your query>')df=result_to_df(result)print(df)# close the poolconnection_pool.close()Quick example to use storage-client to scan vertex and edgeYou should make sure the scan client can connect to the address of storage which see fromSHOW HOSTSfromnebula3.mclientimportMetaCache,HostAddrfromnebula3.sclient.GraphStorageClientimportGraphStorageClient# the metad servers's addressmeta_cache=MetaCache([('172.28.1.1',9559),('172.28.1.2',9559),('172.28.1.3',9559)],50000)# option 1 metad usually discover the storage address automaticallygraph_storage_client=GraphStorageClient(meta_cache)# option 2 manually specify the storage addressstorage_addrs=[HostAddr(host='172.28.1.4',port=9779),HostAddr(host='172.28.1.5',port=9779),HostAddr(host='172.28.1.6',port=9779)]graph_storage_client=GraphStorageClient(meta_cache,storage_addrs)resp=graph_storage_client.scan_vertex(space_name='ScanSpace',tag_name='person')whileresp.has_next():result=resp.next()forvertex_datainresult:print(vertex_data)resp=graph_storage_client.scan_edge(space_name='ScanSpace',edge_name='friend')whileresp.has_next():result=resp.next()foredge_datainresult:print(edge_data)How to choose nebula-pythonNebula-Python VersionNebulaGraph Version1.01.x2.0.02.0.0/2.0.12.5.02.5.02.6.02.6.0/2.6.13.0.03.0.03.1.03.1.03.3.03.3.03.4.0>=3.4.03.5.0>=3.4.0mastermasterHow to contribute to nebula-pythonForkthis repo, then clone it locally
(be sure to replace the{username}in the repo URL below with your GitHub username):git clone https://github.com/{username}/nebula-python.git
cd nebula-pythonWe usePMDto manage the package, install it first:pipx install pdmVisit thePDM documentationfor alternative installation methods.Install the package and all dev dependencies:pdm installMake sure the Nebula server in running, then run the tests with pytest:pdm testUsing the default formatter withblack.Please runpdm fmtto format python code before submitting.SeeHow to contributefor the general process of contributing to Nebula projects.