没有尝试过,但是值得一试:
reply = (redisReply *)redisCommand(c,"GRAPH.QUERY key:%s %s","Project","CREATE (:User {userId:9})");
https://github.com/redis/hiredis#sending-commands
,
我交叉了@SWilly22 的信息,通过阅读replyRedis
的结构,我发现可以使用hiredis 进行RedisGraph 查询。以下是使用 REAMDE 的 RedisGraph 项目的示例图查询的示例代码。
这是创建示例的查询:
GRAPH.QUERY MotoGP "CREATE (:Rider {name:'Valentino Rossi'})-[:rides]->(:Team {name:'Yamaha'}),(:Rider {name:'Dani Pedrosa'})-[:rides]->(:Team {name:'Honda'}),(:Rider {name:'Andrea Dovizioso'})-[:rides]->(:Team {name:'Ducati'})"
这是我们要使用hiredis执行的查询:
GRAPH.QUERY MotoGP "MATCH (r:Rider)-[:rides]->(t:Team {name:'Ducati'}) RETURN count(r)"
这里有一个示例,说明如何使用 hiredis 在 C 中执行此操作:
#include <hiredis/hiredis.h>
#include <stdlib.h>
#include <stdio.h>
#define REDIS_CONTEXT redisContext *context = redisConnect("127.0.0.1",6379);\
if (context == NULL || context->err) {\
if (context) {\
printf("Error: %s\n",context->errstr);\
return -3;\
} else {\
printf("Can't allocate redis context\n");\
return -4;\
}\
} else printf("connected!\n");
int
main(void) {
REDIS_CONTEXT;
redisReply *reply = redisCommand(context,"GRAPH.QUERY MotoGP %s","MATCH (r:Rider)-[:rides]->(t:Team {name:'Ducati'}) RETURN count(r)");
/* Note : you can also try this Query the result is on the readme project
* RedisGraph and gives you aproximatively the same response. */
//redisReply *reply = redisCommand(context,"MATCH (r:Rider)-[:rides]->(t:Team) WHERE t.name = 'Yamaha' RETURN r.name,t.name");
if( reply == NULL )
return 1;
for(int i=0; i<reply->elements; i++) {
printf("\tstr\t:%s#\n",reply->element[i]->str);
printf("\ttype\t:%d#\n",reply->element[i]->type);
printf("\tinteger\t:%llu#\n",reply->element[i]->integer);
printf("\tdval\t:%f#\n",reply->element[i]->dval);
printf("\tlen\t:%zu#\n",reply->element[i]->len);
printf("\tvtype\t:%s#\n",reply->element[i]->vtype);
printf("\telements\t:%zu#\n",reply->element[i]->elements);
for(int j=0; j<reply->element[i]->elements; j++)
switch( reply->element[i]->element[j]->type ) {
case REDIS_REPLY_STRING :
printf("\t\tstr:%s\n",reply->element[i]->element[j]->str);
break;
case REDIS_REPLY_INTEGER :
printf("\t\tinteger:%lld\n",reply->element[i]->element[j]->integer);
break;
case REDIS_REPLY_ARRAY :
printf("\t\t{\n");
for(int k=0; k<reply->element[i]->element[j]->elements; k++)
switch( reply->element[i]->element[j]->element[k]->type ) {
case REDIS_REPLY_STRING :
printf("\t\t\t>str:%s\n",reply->element[i]->element[j]->element[k]->str);
break;
case REDIS_REPLY_INTEGER :
printf("\t\t\t>integer:%lld\n",reply->element[i]->element[j]->element[k]->integer);
break;
default:
continue;
}
printf("\t\t{\n");
break;
default:
continue;
}
printf("---------------------------\n");
}
return 0;
}
结果如下:
str :(null)#
type :2#
integer :0#
dval :0.000000#
len :0#
vtype :#
elements :1#
str:count(r)
---------------------------
str :(null)#
type :2#
integer :0#
dval :0.000000#
len :0#
vtype :#
elements :1#
{
>integer:1
{
---------------------------
str :(null)#
type :2#
integer :0#
dval :0.000000#
len :0#
vtype :#
elements :2#
str:Cached execution: 1
str:Query internal execution time: 0.856161 milliseconds
---------------------------
说明
这是 redisReply 结构体:
/* This is the reply object returned by redisCommand() */
typedef struct redisReply {
int type; /* REDIS_REPLY_* */
long long integer; /* The integer when type is REDIS_REPLY_INTEGER */
double dval; /* The double when type is REDIS_REPLY_DOUBLE */
size_t len; /* Length of string */
char *str; /* Used for REDIS_REPLY_ERROR,REDIS_REPLY_STRING
REDIS_REPLY_VERB,REDIS_REPLY_DOUBLE (in additional to dval),and REDIS_REPLY_BIGNUM. */
char vtype[4]; /* Used for REDIS_REPLY_VERB,contains the null
terminated 3 character content type,such as "txt". */
size_t elements; /* number of elements,for REDIS_REPLY_ARRAY */
struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */
} redisReply;
这是您使用 redis-cli 获得的内容:
127.0.0.1:6379> GRAPH.QUERY MotoGP "MATCH (r:Rider)-[:rides]->(t:Team {name:'Ducati'}) RETURN count(r)"
1) 1) "count(r)"
2) 1) 1) (integer) 1
3) 1) "Query internal execution time: 0.624435 milliseconds"
因此,通过分析 redis-cli 回复,我们可以看到我们有三个名为 1,2 和 3 的元素。它们中的每一个都属于 type
: REDIS_REPLY_ARRAY
。所以现在我们可以在每个数组中输入。在名为 1 的数组中,我们有一个类型为:REDIS_REPLY_STRING
的元素,其值为 "count(r)"
。
另一个例子
对于查询:
GRAPH.QUERY MotoGP "MATCH (r:Rider)-[:rides]->(t:Team) WHERE t.name = 'Yamaha' RETURN r.name,t.name"
通过取消注释主函数中的第二个查询,您可以获得以下结果:
connected!
str :(null)#
type :2#
integer :0#
dval :0.000000#
len :0#
vtype :#
elements :2#
str:r.name
str:t.name
---------------------------
str :(null)#
type :2#
integer :0#
dval :0.000000#
len :0#
vtype :#
elements :1#
{
>str:Valentino Rossi
>str:Yamaha
{
---------------------------
str :(null)#
type :2#
integer :0#
dval :0.000000#
len :0#
vtype :#
elements :2#
str:Cached execution: 1
str:Query internal execution time: 0.827074 milliseconds
---------------------------
与自述文件一一对应:
1) 1) "r.name"
2) "t.name"
2) 1) 1) "Valentino Rossi"
2) "Yamaha"
3) 1) "Query internal execution time: 0.625399 milliseconds"
注意:此算法在“ASIS”下提供请不要在生产模式下使用
本文链接:https://www.f2er.com/2992090.html