本节为您介绍如何使用Connection
对象的方法管理数据库中的图集。
每个示例主要展示如何使用所列方法。点击完整示例查看完整代码示例。
ShowGraph()
从数据库获取全部图集。
参数:
RequestConfig
(可选):配置请求。
返回值:
List<GraphSet>
:数据库中全部图集列表。
// 获取全部图集并打印边数超过2000条的图集名称
var graphsetList = await ultipa.ShowGraph();
foreach (var graph in graphsetList)
{
if (graph.TotalEdges > 2000)
{
Console.WriteLine(graph.Name);
}
}
Display_Ad_Click
ERP_DATA2
wikiKG
GetGraph()
根据图集名称从数据库获取图集信息。
参数:
string
:图集名称。RequestConfig
(可选):配置请求。
返回值:
GraphSet
:目标图集。
// 获取名为wikiKG的图集并打印其全部信息
var graph = await ultipa.GetGraph("wikiKG");
Console.WriteLine(
$"ID: {graph.Id}\n"
+ $"Name: {graph.Name}\n"
+ $"TotalNodes: {graph.TotalNodes}\n"
+ $"TotalEdges: {graph.TotalEdges}\n"
+ $"Status: {graph.Status}\n"
+ $"Description: {graph.Desc}"
);
True
ID: 13844
Name: wikiKG
TotalNodes: 44449
TotalEdges: 167799
Status: MOUNTED
Description:
CreateGraph()
在数据库中新建图集。
参数:
GraphSet
:待创建的图集;必须设置name
字段,description
字段可选。RequestConfig
(可选):配置请求。
返回值:
UqlResponse
:请求的结果。
// 创建图集并打印错误代码
var graph = new GraphSet();
graph.Name = "testCSharpSDK";
graph.Desc = "Description for testCSharpSDK";
var resp = await ultipa.CreateGraph(graph);
Console.WriteLine(resp.Status.ErrorCode);
数据库中已创建名为testJavaSDK
的新图集,驱动程序打印结果如下:
Success
CreateGraphIfNotExist()
在数据库中新建图集,若同名图集已存在,忽略报错。
参数:
GraphSet
:待创建的图集;必须设置name
字段,description
字段可选。RequestConfig
(可选):配置请求。
返回值:
UqlResponse
:请求的结果。bool
: 图集是否存在。
// 创建图集并打印错误代码
var graph = new GraphSet();
graph.Name = "testCSharpSDK";
graph.Desc = "Description for testCSharpSDK";
var resp1 = await ultipa.CreateGraphIfNotExist(graph);
Console.WriteLine("Graph already exists: " + resp1.Item2);
Console.WriteLine(
"First creation: " + (resp1.Item1 != null ? resp1.Item1.Status.ErrorCode : "null")
);
// 尝试再次创建图集并打印错误代码
var resp2 = await ultipa.CreateGraphIfNotExist(graph);
Console.WriteLine("Graph already exists: " + resp2.Item2);
Console.WriteLine(
"Second creation: " + (resp2.Item1 != null ? resp2.Item1.Status.ErrorCode : "Failed")
);
A new graphset testCSharpSDK
is created in the database, and the driver prints:
Graph already exists: False
First creation: Success
Graph already exists: True
Second creation: Failed
DropGraph()
根据图集名称从数据库中删除图集。
参数:
string
:图集名称。RequestConfig
(可选):配置请求。
返回值:
UqlResponse
:请求的结果。
// 创建图集后将其删除并打印结果
var graph = new GraphSet();
graph.Name = "testCSharpSDK";
graph.Desc = "Description for testCSharpSDK";
var newGraph = await ultipa.CreateGraph(graph);
Console.WriteLine("Creation: " + newGraph.Status.ErrorCode);
Thread.Sleep(2000);
var dropIt = await ultipa.DropGraph("testCSharpSDK");
Console.WriteLine(JsonConvert.SerializeObject(dropIt));
Creation: Success
{"UqlReply":{"Status":{"ErrorCode":0,"Msg":"","ClusterInfo":null},"TotalTimeCost":0,"EngineTimeCost":0,"Alias":[],"Paths":[],"Nodes":[],"Edges":[],"Attrs":[],"Graphs":[],"Tables":[],"Statistics":null,"ExplainPlan":null},"Status":{"ErrorCode":0,"Msg":"","ClusterInfo":null},"Statistic":{"NodeAffected":0,"EdgeAffected":0,"TotalCost":6,"EngineCost":0},"Explain":[]}
AlterGraph()
根据图集名称,修改数据库中已有图集的名称和描述信息。
参数:
GraphSet
:待修改的图集;必须设置name
字段。GraphSet
:现有图集的新配置;必须设置 name 或 description 字段中的一个或两个。RequestConfig
(可选):配置请求。
返回值:
UqlResponse
:请求的结果。
// 将图集 "testPythonSDK" 重命名为 "newGraph",并为其设置描述,然后打印结果。
var graph1 = new GraphSet();
graph1.Name = "testCSharpSDK";
var newGraph = await ultipa.CreateGraph(graph1);
var graph2 = new GraphSet();
graph2.Name = "newGraph";
graph2.Desc = "The graphset is altered";
var alterIt = await ultipa.AlterGraph(graph1, graph2);
Console.WriteLine(JsonConvert.SerializeObject(alterIt));
{"UqlReply":{"Status":{"ErrorCode":0,"Msg":"","ClusterInfo":null},"TotalTimeCost":0,"EngineTimeCost":0,"Alias":[],"Paths":[],"Nodes":[],"Edges":[],"Attrs":[],"Graphs":[],"Tables":[],"Statistics":null,"ExplainPlan":null},"Status":{"ErrorCode":0,"Msg":"","ClusterInfo":null},"Statistic":{"NodeAffected":0,"EdgeAffected":0,"TotalCost":1,"EngineCost":0},"Explain":[]}
Truncate()
清空(删除)给定图集中的指定点或边,或清空全部图集。请注意,清空点数据的同时会删除与点相连的所有边。清空操作仅删除点边数据,不会删除schema和属性信息。
参数:
-
TruncateParams
:待清空的对象;必须设置graphName
字段,schema
和DbType
字段是可选的,但如果设置了其中一个,则必须同时设置另一个。 -
RequestConfig
(可选):配置请求。
返回值:
UqlResponse
:请求的结果。
// 清空图集'myGraph_1'中的 @user 点数据并打印错误码
var truncate1 = new GraphAPI.TruncateParams()
{
graphName = "myGraph_1",
schema = "user",
DbType = DBType.Dbnode,
};
var res1 = await ultipa.Truncate(truncate1);
Console.WriteLine(res1.Status.ErrorCode);
// 清空图集myGraph_2中全部边数据并打印错误代码
var truncate2 = new GraphAPI.TruncateParams()
{
graphName = "myGraph_2",
schema = "*",
DbType = DBType.Dbedge,
};
var res2 = await ultipa.Truncate(truncate2);
Console.WriteLine(res2.Status.ErrorCode);
// 清空图集myGraph_3并打印错误代码
var truncate3 = new GraphAPI.TruncateParams() { graphName = "myGraph_3" };
var res3 = await ultipa.Truncate(truncate3);
Console.WriteLine(res3.Status.ErrorCode);
Success
Success
Success
CompactGraph()
清除服务器磁盘上的无效及冗余数据以压缩图集。有效数据不会受到影响。
参数:
string
:图集名称。RequestConfig
(可选):配置请求。
返回值:
UqlResponse
:请求的结果。
// 压缩图集miniCircle并打印错误代码
var res = await ultipa.CompactGraph("miniCircle");
Console.WriteLine(res.Status.ErrorCode);
Success
HasGraph()
根据图集名称查看数据库中是否存在该图集。
参数:
string
:图集名称。RequestConfig
(可选):配置请求。
返回值:
bool
:请求的结果。
// 查看图集miniCircle是否存在并打印结果
var res = await ultipa.HasGraph("miniCircle");
Console.WriteLine("Graph exists: " + res);
Graph exists: True
UnmountGraph()
卸载图集以节省数据库内存。
参数:
string
:图集名称。RequestConfig
(可选):配置请求。
返回值:
UqlResponse
:请求的结果。
// 卸载图集miniCircle并打印图集状态
var res = await ultipa.UnMountGraph("miniCircle");
Console.WriteLine(res.Status.ErrorCode);
Success
MountGraph()
将图集挂载至数据库内存。
参数:
string
:图集名称。RequestConfig
(可选):配置请求。
返回值:
UqlResponse
:请求的结果。
// 挂载图集“miniCircle”并打印结果。
var res = await ultipa.MountGraph("miniCircle");
Console.WriteLine(res.Status.ErrorCode);
Success
完整示例
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Xml.Linq;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using UltipaService;
using UltipaSharp;
using UltipaSharp.api;
using UltipaSharp.configuration;
using UltipaSharp.connection;
using UltipaSharp.exceptions;
using UltipaSharp.structs;
using UltipaSharp.utils;
using Logger = UltipaSharp.utils.Logger;
using Property = UltipaSharp.structs.Property;
using Schema = UltipaSharp.structs.Schema;
class Program
{
static async Task Main(string[] args)
{
// 配置连接
// URI 示例: Hosts=new[]{"mqj4zouys.us-east-1.cloud.ultipa.com:60010"}
var myconfig = new UltipaConfig()
{
Hosts = new[] { "192.168.1.85:60061", "192.168.1.86:60061", "192.168.1.87:60061" },
Username = "***",
Password = "***",
};
// 建立与数据库的连接
var ultipa = new Ultipa(myconfig);
var isSuccess = ultipa.Test();
Console.WriteLine(isSuccess);
// 请求配置
RequestConfig requestConfig = new RequestConfig() { UseMaster = true };
// 创建新图集 'testCSharpSDK'
var graph = new GraphSet();
graph.Name = "testCSharpSDK";
graph.Desc = "Description for testCSharpSDK";
var newGraph = await ultipa.CreateGraph(graph);
Console.WriteLine("Creation: " + newGraph.Status.ErrorCode);
Thread.Sleep(2000);
// 删除刚创建的图集 'testCSharpSDK'
var dropIt = await ultipa.DropGraph("testCSharpSDK");
Console.WriteLine(dropIt.Status.ErrorCode);
}
}