本节为您介绍如何使用Connection
对象的方法下载算法结果文件,从图集导出点和边。
每个示例主要展示如何使用所列方法。点击完整示例查看完整代码示例。
downloadAlgoResultFile()
下载当前图集一个算法任务中的一个结果文件。
参数:
str
:文件名称。str
:生成结果文件的算法任务ID。Callable[[bytes]
:处理字节数据的回调函数。RequestConfig
(可选):配置请求。
返回值:
- 无
requestConfig = RequestConfig(graphName="miniCircle")
# 在图集miniCircle上运行鲁汶算法并打印任务ID
response = Conn.uql("algo(louvain).params({phase1_loop_num: 20, min_modularity_increase: 0.001}).write({file:{filename_community_id: 'communityID', filename_ids: 'ids', filename_num: 'num'}})", requestConfig);
taskTable = response.alias("_task").asTable()
taskID = taskTable.rows[0][0]
print("taskID =", taskID)
time.sleep(3)
filename = 'communityID'
# 定义一个回调函数来处理文件下载
def handle_down_algo_file(stream: bytes):
text = stream.decode('utf-8')
with open(filename, 'a', encoding='utf-8') as f:
writer = csv.writer(f)
if ',' in text:
writer.writerow(text.strip().split(','))
else:
writer.writerow([text])
# 使用回调函数执行文件下载
cb = lambda stream: handle_down_algo_file(stream)
Conn.downloadAlgoResultFile(filename, taskID, cb, requestConfig)
taskID = 78620
文件
communityID
被下载到与您执行的文件相同的目录中。
downloadAllAlgoResultFile()
下载当前图集一个算法任务中的全部结果文件。
参数:
str
:生成结果文件的算法任务ID。Callable[[bytes]
:处理字节数据的回调函数。RequestConfig
可选):配置请求。
返回值:
- 无
requestConfig = RequestConfig(graphName="miniCircle")
# 在图集miniCircle上运行鲁汶算法并打印任务ID
response = Conn.uql("algo(louvain).params({phase1_loop_num: 20, min_modularity_increase: 0.001}).write({file:{filename_community_id: 'communityID', filename_ids: 'ids', filename_num: 'num'}})", requestConfig);
taskTable = response.alias("_task").asTable()
taskID = taskTable.rows[0][0]
print("taskID =", taskID)
time.sleep(3)
# 定义回调函数来处理文件下载
def handle_down_all_algo_file(stream: bytes, filename):
text = stream.decode('utf-8')
with open(filename, 'a', encoding='utf-8') as f:
writer = csv.writer(f)
if ',' in text:
writer.writerow(text.strip().split(','))
else:
writer.writerow([text])
# 使用回调函数执行文件下载
cb = lambda stream,filename: handle_down_all_algo_file(stream,filename)
Conn.downloadAllAlgoResultFile(taskID, cb, requestConfig)
taskId = 1509
文件
communityID
、ids
和num
被下载到与您执行的文件相同的目录中。
export()
从当前图集导出点和边。
参数:
Export
:导出请求的配置信息,包括dbType:DBType
,limit:int
,schemaName:str
和selectPropertiesName:List[str]
。Callable[[List[Node],List[Edge]],None]
:处理点和边的回调函数。RequestConfig
(可选):配置请求。
返回值:
- 无
# 从图集miniCircle名为account的schema中导出10个点,并打印点的信息。
requestConfig = RequestConfig(graphName="miniCircle")
exportRequest = Export(
dbType=DBType.DBNODE,
limit=10,
schemaName="account",
selectPropertiesName=["_id", "_uuid", "name", "year"]
)
# 定义回调函数来处理导出
def handle_export(nodes: List[Node], edges: List[Edge]):
if nodes is not None:
df = pd.DataFrame(nodes)
df.to_excel(exportRequest.schemaName + ".xlsx", index=False)
if edges is not None:
df = pd.DataFrame(edges)
df.to_excel(exportRequest.schemaName + ".xlsx", index=False)
Conn.export(exportRequest, handle_export, requestConfig)
文件
account.xlsx
被导出到与您执行的文件相同的目录中。
完整示例
import csv
from ultipa import Connection, UltipaConfig
from ultipa.configuration.RequestConfig import RequestConfig
ultipaConfig = UltipaConfig()
# URI示例:ultipaConfig.hosts = ["mqj4zouys.us-east-1.cloud.ultipa.com:60010"]
ultipaConfig.hosts = ["192.168.1.85:60061", "192.168.1.87:60061", "192.168.1.88:60061"]
ultipaConfig.username = "<username>"
ultipaConfig.password = "<password>"
Conn = Connection.NewConnection(defaultConfig=ultipaConfig)
requestConfig = RequestConfig(graphName="miniCircle")
# 运行鲁汶算法并打印任务ID
response = Conn.uql("algo(louvain).params({phase1_loop_num: 20, min_modularity_increase: 0.001}).write({file:{filename_community_id: 'communityID', filename_ids: 'ids', filename_num: 'num'}})", requestConfig);
taskTable = response.alias("_task").asTable()
taskID = taskTable.rows[0][0]
print("taskID =", taskID)
time.sleep(3)
# 定义回调函数来处理导出
def handle_down_all_algo_file(stream: bytes, filename):
text = stream.decode('utf-8')
with open(filename, 'a', encoding='utf-8') as f:
writer = csv.writer(f)
if ',' in text:
writer.writerow(text.strip().split(','))
else:
writer.writerow([text])
# 使用回调函数执行文件下载
cb = lambda stream,filename: handle_down_all_algo_file(stream,filename)
Conn.downloadAllAlgoResultFile(taskID, cb, requestConfig)