GFQL 远程模式#
您可以远程运行GFQL查询和GPU Python,例如当数据已经远程、变得很大,或者您想使用远程GPU时
基本用法#
远程运行链并获取结果#
from graphistry import n, e
g2 = g1.chain_remote([n(), e(), n()])
assert len(g2._nodes) <= len(g1._nodes)
方法 chain_remote 远程运行链并获取计算后的图
chain: 图形节点和边匹配器的序列(
ASTObject实例)。output_type: 默认为“all”,是否返回节点(‘nodes’)、边(‘edges’)或两者。参见
chain_remote_shape以仅返回元数据。node_col_subset: 可选地限制返回的节点属性到一个允许列表中。
edge_col_subset: 可选地限制返回哪些边属性到允许列表中。
engine: 可选的执行引擎。通常不设置引擎,默认为‘auto’。使用‘cudf’进行GPU加速,使用‘pandas’进行CPU处理。
validate: 默认为 True,是否验证查询和数据。
手动选择CPU和GPU引擎#
默认情况下,GFQL 将根据数据集大小等工作负载特征决定使用哪个引擎。您可以通过指定要使用的引擎来覆盖此默认设置。
GPU#
在远程GPU上运行并获取结果
from graphistry import n, e
g2 = g1.chain_remote([n(), e(), n()], engine='cudf')
assert len(g2._nodes) <= len(g1._nodes)
CPU#
在远程CPU上运行并获取结果
from graphistry import n, e
g2 = g1.chain_remote([n(), e(), n()], engine='pandas')
显式上传#
通过upload显式上传将绑定字段Plottable::dataset_id,因此后续的远程调用知道跳过重新上传。始终使用显式上传可以使代码在较大的代码库中更加可预测。
from graphistry import n, e
g2 = g1.upload()
assert g2._dataset_id is not None, "Uploading sets `dataset_id` for subsequent calls"
g3a = g2.chain_remote([n()])
g3b = g2.chain_remote([n(), e(), n()])
assert len(g3a._nodes) >= len(g3b._nodes)
绑定到现有的远程数据#
如果数据已经上传并且您的用户有权访问它,例如来自之前的会话或由其他用户共享,您可以将其绑定到本地的Plottable以进行远程访问。
import graphistry
from graphistry import n, e
g1 = graphistry.bind(dataset_id='abc123')
assert g1._nodes is None, "Binding does not fetch data"
connected_graph_g = g1.chain_remote([n(), e()])
connected_nodes_df = connected_graph_g._nodes
print(connected_nodes_df.shape)
下载更少#
您可能不需要下载所有——或任何——结果,这可以显著加快执行速度
仅返回节点#
g1.chain_remote([n(), e(), n()], output_type="nodes")
仅返回节点和特定列#
cols = [g1._node, 'time']
g2b = g1.chain_remote(
[n(), e(), n()],
output_type="nodes",
node_col_subset=cols)
assert len(g2b._nodes.columns) == len(cols)
仅返回边#
g2a = g1.chain_remote([n(), e(), n()], output_type="edges")
仅返回边和特定列#
cols = [g1._source, g1._destination, 'time']
g2b = g1.chain_remote([n(), e(), n()],
output_type="edges",
edge_col_subset=cols)
assert len(g2b._edges.columns) == len(cols)
返回元数据但不返回实际图形#
from graphistry import n, e
shape_df = g1.chain_remote_shape([n(), e(), n()])
assert len(shape_df) == 2
print(shape_df)
远程Python#
您还可以远程运行完整的GPU Python任务,例如对于更复杂的代码,或者如果您希望服务器本身执行诸如从数据库获取数据的操作。
在当前图表上运行远程Python#
import graphistry
from graphistry import n, e
# Fully self-contained so can be transferred
def my_remote_trim_graph_task(g):
# Trick: You can also put database fetch calls here instead of using 'g'!
return (g
.nodes(g._nodes[:10])
.edges(g._edges[:10])
)
# Upload any local graph data to the remote server
g2 = g1.upload()
g3 = g2.chain_remote_python(my_remote_trim_graph_task)
assert len(g3._nodes) == 10
assert len(g3._edges) == 10
在现有图形上运行Python,返回一个表格#
import graphistry
g = graphistry.bind(dataset_id='ds-abc-123')
def first_n_edges(g):
return g._edges[:10]
some_edges_df = g.remote_python_table(first_n_edges)
assert len(some_edges_df) == 10
在现有图形上运行Python,返回JSON#
import graphistry
g = graphistry.bind(dataset_id='ds-abc-123')
def first_n_edges_shape(g):
return {'num_edges': len(g._edges[:10])}
obj = g.remote_python_json(first_n_edges_shape)
assert obj['num_edges'] == 10