跳转到主要内容

NebulaGraph 网络X 适配器

项目描述

NebulaGraph NetworkX Adaptor(ng_nx)

使用NetworkX API操作和分析NebulaGraph数据

License PyPI version pdm-managed


文档: https://github.com/wey-gu/nebulagraph-nx#documentation

源代码: https://github.com/wey-gu/nebulagraph-nx


目录

简介

NebulaGraph NetworkX (ng_nx) 是一个强大的工具,它连接了NebulaGraph和NetworkX,使您能够利用NetworkX丰富的图算法和分析工具在NebulaGraph中存储的数据。这种集成结合了NebulaGraph的高级存储功能与NetworkX广泛的图分析功能。

功能

  • NebulaGraph和NetworkX之间的无缝集成
  • 多种读取类型,便于灵活检索数据
  • 易于使用的写入器,便于将分析结果存储回NebulaGraph
  • 支持顶点和边数据操作
  • 兼容NetworkX广泛的图算法库

安装

确保您已有一个NebulaGraph集群正在运行。为了快速设置,您可以使用NebulaGraph Lite在Colab中5分钟内设置一个集群。

使用pip安装ng_nx

pip install ng_nx

使用

从NebulaGraph读取数据

使用GraphD客户端

from ng_nx import NebulaReader
from ng_nx.utils import NebulaGraphConfig

config = NebulaGraphConfig(
    space="basketballplayer",
    graphd_hosts="127.0.0.1:9669",
    metad_hosts="127.0.0.1:9559"
)

reader = NebulaReader(
    edges=["follow", "serve"],
    properties=[["degree"], ["start_year", "end_year"]],
    nebula_config=config, limit=10000)

g = reader.read()

使用存储客户端,这需要在NebulaGraph网络中运行ng_nx或将metad和storaged暴露给它,并在NebulaGraph中注册相同的宿主和端口(使用SHOW HOSTS META;SHOW HOSTS;)。

from ng_nx import NebulaScanReader
from ng_nx.utils import NebulaGraphConfig

# Here, we need to be able to resolve the metad and storaged hosts, where they are the same with `SHOW HOSTS META;` and `SHOW HOSTS;`

config = NebulaGraphConfig(
    space="demo_basketballplayer",
    graphd_hosts="graphd0:9669",
    metad_hosts="metad0:9559"
)

reader = NebulaScanReader(
    edges=["follow", "serve"],
    properties=[["degree"], ["start_year", "end_year"]],
    nebula_config=config, limit=10000, with_rank=True)

g = reader.read()

运行算法

我们需要先安装louvain和graspologic,以运行NetworkX(不包括PageRank等)中未包含的louvain和leiden算法。

pip3 install python-louvain graspologic

然后我们可以运行这些算法。

import networkx as nx
import community as community_louvain

# Run PageRank algorithm
pr = nx.pagerank(
    g, alpha=0.85,
    max_iter=100,
    tol=1e-06,
    weight='degree')

# Run Louvain community detection
ug = g.to_undirected()
louvain = community_louvain.best_partition(ug)

# Run Leiden community detection

from graspologic.partition import hierarchical_leiden

# Cast Multi-Graph to Homogeneous Graph
g = nx.Graph(g)
community_hierarchical_clusters = hierarchical_leiden(ug, max_cluster_size=10)

将结果写回NebulaGraph

典型用例包括

  1. 将图算法的结果写入NebulaGraph作为顶点数据。
  2. 将图算法的结果写入NebulaGraph作为边数据。

在图分析后,将顶点数据写入NebulaGraph

我们可以创建像这样为pagerank和louvain创建模式

CREATE TAG IF NOT EXISTS pagerank (
    pagerank double NOT NULL
);

CREATE TAG IF NOT EXISTS louvain (
    cluster_id int NOT NULL
);

然后我们可以运行pagerank和louvain算法,并将结果写入NebulaGraph,如下所示

from ng_nx import NebulaWriter

pr_writer = NebulaWriter(data=pr, nebula_config=config)

# properties to write
properties = ["pagerank"]

pr_writer.set_options(
    label="pagerank",
    properties=properties,
    batch_size=256,
    write_mode="insert",
    sink="nebulagraph_vertex",
)
# write back to NebulaGraph
pr_writer.write()

# write louvain result

louvain_writer = NebulaWriter(data=louvain, nebula_config=config)
# properties to write
properties = ["cluster_id"]
louvain_writer.set_options(
    label="louvain",
    properties=properties,
    batch_size=256,
    write_mode="insert",
    sink="nebulagraph_vertex",
)
louvain_writer.write() # write back to NebulaGraph

在图分析后,将边数据写入NebulaGraph

比如说我们有一个包含玩家和关注边的关系图,我们可以将结果写入NebulaGraph,如下所示

CREATE TAG IF NOT EXISTS player (
    name string NOT NULL,
    age int NOT NULL
);

CREATE EDGE IF NOT EXISTS follow (
    start_year int NOT NULL,
    end_year int NOT NULL
);

我们可以这样将结果写入NebulaGraph

from ng_nx import NebulaWriter

# Example edge data
edge_data = [
    ("player1", "player2", 0, [2022, 2023]),  # src, dst, rank, [start_year, end_year]
    ("player2", "player3", 1, [2021, 2022]),
    # ... more edges ...
]

edge_writer = NebulaWriter(data=edge_data, nebula_config=config)

# properties to write, map the properties to the edge data
properties = ["start_year", "end_year"]

edge_writer.set_options(
    label="follow",  # Edge type name
    properties=properties,
    batch_size=256,
    write_mode="insert",
    sink="nebulagraph_edge",
)

# Write edges to NebulaGraph
edge_writer.write()

使用NebulaQueryReader

NebulaQueryReader允许您执行任何NebulaGraph查询,并从结果中构建一个NetworkX图。

from ng_nx import NebulaQueryReader
from ng_nx.utils import NebulaGraphConfig

config = NebulaGraphConfig(
    space="demo_basketballplayer",
    graphd_hosts="127.0.0.1:9669",
    metad_hosts="127.0.0.1:9559"
)

reader = NebulaQueryReader(nebula_config=config)

# Execute a custom query
query = "MATCH p=(v:player{name:'Tim Duncan'})-[e:follow*1..3]->(v2) RETURN p"
g = reader.read(query)

这种方法允许您利用NebulaGraph查询语言的全部功能,同时仍然能够使用NetworkX分析结果。

高级使用

NebulaQueryReader

NebulaQueryReader允许您执行任何NebulaGraph查询,并从结果中构建一个NetworkX图。

from ng_nx import NebulaQueryReader
from ng_nx.utils import NebulaGraphConfig

config = NebulaGraphConfig(
    space="demo_basketballplayer",
    graphd_hosts="127.0.0.1:9669",
    metad_hosts="127.0.0.1:9559"
)

reader = NebulaQueryReader(nebula_config=config)

# Execute a custom query
query = "MATCH p=(v:player{name:'Tim Duncan'})-[e:follow*1..3]->(v2) RETURN p"
g = reader.read(query)

读取器

NG-NX提供了三种类型的读取器,用于从NebulaGraph获取数据

  1. NebulaReader通过MATCH从边和属性加载):根据指定的边和属性从NebulaGraph读取图,返回一个NetworkX图。它使用内部MATCH子句从NebulaGraph获取数据。

  2. NebulaQueryReader从任何查询构建):执行自定义NebulaGraph查询,并从结果中构建一个NetworkX图。此读取器特别适用于需要执行复杂查询或具有特定数据检索要求的情况。

  3. NebulaScanReader更适合大型数据集):将使用类似于NebulaReader的配置从NebulaGraph读取图数据,但它将绕过MATCH子句,并利用存储客户端的SCAN接口,可能在大型数据集上提高性能。请注意,此读取器需要NebulaGraph集群配置metad和storaged,以便ng_nx可以访问。

每个读取器都旨在满足不同的用例,为如何与NebulaGraph交互以及如何从NebulaGraph检索数据进行分析提供灵活性。

文档

API参考

贡献

欢迎贡献!如果您发现任何问题或对改进有建议,请在此GitHub仓库上打开问题或提交拉取请求。

项目详情


下载文件

下载您平台的文件。如果您不确定选择哪个,请了解更多关于安装软件包的信息。

源分布

ng-nx-0.2.3.tar.gz (16.3 kB 查看哈希值)

上传于 源码

构建版本

ng_nx-0.2.3-py3-none-any.whl (14.7 kB 查看哈希值)

上传于 Python 3

支持者

AWSAWS云计算和安全赞助商DatadogDatadog监控FastlyFastlyCDNGoogleGoogle下载分析MicrosoftMicrosoftPSF赞助商PingdomPingdom监控SentrySentry错误记录StatusPageStatusPage状态页面