本节为您介绍如何在UQL语句中使用Connection
对象的uql()
方法和uqlStream()
方法查询数据库。
每个示例主要展示如何使用所列方法。点击完整示例查看完整代码示例。
UQL (Ultipa Query Language) is the language designed for fully interacting with Ultipa graph databases. For detailed information on UQL, refer to the documentation.
Uql()
在当前图集或数据库上执行GQL查询语句并返回查询结果。
参数:
string
:待执行的UQL查询语句。RequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。
// 获取图集miniCircle中@movie下的5个点并打印其名称
RequestConfig requestConfig = new RequestConfig() { Graph = "miniCircle", UseMaster = true };
var res = await ultipa.Uql("find().nodes({@movie}) as n return n{*} limit 5", requestConfig);
var nodeList = res?.Alias("n")?.AsNodes();
foreach (var node in nodeList)
{
Console.WriteLine(node.Values.GetValueOrDefault("name"));
}
The Shawshank Redemption
Farewell My Concubine
Léon: The Professional
Titanic
Life is Beautiful
更多示例,请参阅嬴图数据与C#类型映射.
UqlStream()
在当前图集或数据库上执行GQL查询语句并以增量形式返回查询结果。在处理大型数据集时,无需立刻加载所有数据至内存中。
参数:
string
:待执行的UQL查询语句。UqlResponseStream
:流处理监听器。RequestConfig
(可选):配置请求。
返回值:
void
// 获取图集miniCircle的1步路径
RequestConfig requestConfig = new RequestConfig() { Graph = "miniCircle", UseMaster = true };
var count = 0;
await ultipa.UqlStream(
"n().e().n() as paths return paths{*}",
new UqlResponseStream()
{
OnStart = () => Console.WriteLine("Start"),
OnData = (
resp =>
{
if (resp.Status.ErrorCode != ErrorCode.Success)
{
Console.WriteLine(resp.Status.Msg);
}
var paths = resp.Get(0).AsPaths();
count += paths.Count();
Console.WriteLine($"Count ={count}");
return true;
}
),
OnEnd = () => Console.WriteLine("End"),
},
requestConfig
);
Start
Count = 1390
End
完整示例
using System.Security.Cryptography.X509Certificates;
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() { Graph = "miniCircle", UseMaster = true };
// 获取10个点并打印返回的第一个点的_id和storeName属性值
var res = await ultipa.Uql(
"find().nodes({@movie}) as n return n{*} limit 10",
requestConfig
);
var nodeList = res?.Get(0).AsNodes();
if (nodeList[0].Values.TryGetValue("name", out object? value))
{
Console.WriteLine(value);
}
Console.WriteLine(nodeList[0].Id);
}
}