【问题标题】:How to specify the database in an ArangoDb AQL query?如何在 ArangoDb AQL 查询中指定数据库?
【发布时间】:2020-02-23 16:33:04
【问题描述】:

如果在特定的 ArangoDB 服务器上定义了多个数据库,我如何指定要运行 AQL 查询的数据库?

通过包含数据库名称(替换为下面的 [DBNAME])的 REST 端点运行查询,即:

/_db/[DBNAME]/_api/cursor

似乎不起作用。错误消息显示“未知路径 /_db/[DBNAME]/_api/cursor”

这是我必须在查询本身中指定的内容吗?

另外:我尝试运行的查询是:

FOR col in COLLECTIONS() RETURN col.name

Fwiw,我还没有找到通过 REST API 设置“当前”数据库的方法。另外,我正在使用 fuerte 从 C++ 访问 REST API。

【问题讨论】:

  • 数据库似乎是 RequestHeader 的一部分(参见 include/fuerte/message.h 第 96 行 (github.com/arangodb/fuerte/blob/…));如果你的问题仍然存在,我会说看看标题,修改你的代码并发布它的相关部分
  • 您的问题的答案是:您没有将数据库指定为 AQL 查询的一部分。
  • 感谢汤姆的建议。我尝试设置 request->header.database 值,但没有帮助。我似乎完全无法改变 arangodb 的“当前”数据库概念(由端点 /_api/database/current 返回),我似乎也无法让 /_db/[DBNAME]/ 前缀工作。当前数据库似乎停留在“_system”,至少通过 REST api 访问通过 fuerte。仍在寻找有关如何克服这一点的建议。我会考虑重新表述这个问题,提供一些简单的代码并再次提问。
  • 很高兴你找到了方法;它是一个处于起步阶段的低级库,看起来,这就是它当时的样子。亲切的问候

标签: c++ database arangodb aql fuerte


【解决方案1】:

Tom Regner 在此值得一提,因为他提示了产生此答案的调查。我在这里发布我的发现作为答案,以帮助可能遇到此问题的其他人。

我不知道这是一个 fuerte 错误、缺点还是只是我不清楚的 api 警告......但是......

为了使端点中的“/_db/[DBNAME/”前缀(例如完整端点“/_db/[DBNAME/_api/cursor”)在 ::arangodb::fuerte::Request 的标头中注册和使用,仅调用:

std::unique_ptr<fuerte::Request> request;
const char *endpoint = "/_db/[DBNAME/_api/cursor";
request = fuerte::createRequest(fuerte::RestVerb::Post,endpoint);
// and adding any arguments to the request using a VPackBuilder...
// in this case the query (omitted)

要将数据库名称包含在此类请求中,您必须另外调用以下命令:

request->header.parseArangoPath(endpoint);

不这样做似乎会导致关于'未知路径'的错误。

注1:简单设置数据库成员变量,即

request->header.database = "[DBNAME]";

不起作用。

注意 2: 没有前导 '/_db/[DBNAME]/' 前缀的操作在使用 'current' 数据库时似乎可以正常工作。 (至少对我来说,它似乎停留在“_system”,因为据我所知,似乎没有端点可以通过 HTTP REST Api 更改它。)

【讨论】:

    【解决方案2】:

    这些文档现在不是很有帮助,所以如果有人正在寻找更完整的示例,请考虑以下代码。

    EventLoopService eventLoopService;
    // adjust the connection for your environment!
    std::shared_ptr<Connection> conn = ConnectionBuilder().endpoint("http://localhost:8529")
            .authenticationType(AuthenticationType::Basic)
            .user(?)      // enter a user with access
            .password(?)  // enter the password
            .connect(eventLoopService);
    
    // create the request
    std::unique_ptr<Request> request = createRequest(RestVerb::Post, ContentType::VPack);
    
    // enter the database name (ensure the user has access)
    request->header.database = ?;
    
    // API endpoint to submit AQL queries
    request->header.path = "/_api/cursor";
    
    // Create a payload to be submitted to the API endpoint
    VPackBuilder builder;
    builder.openObject();
    // here is your query
    builder.add("query", VPackValue("for col in collections() return col.name"));
    builder.close();
    
    // add the payload to the request
    request->addVPack(builder.slice());
    
    // send the request (blocking)
    std::unique_ptr<Response> response = conn->sendRequest(std::move(request));
    
    // check the response code - it should be 201
    unsigned int statusCode = response->statusCode();
    
    // slice has the response data
    VPackSlice slice = response->slices().front();
    
    std::cout << slice.get("result").toJson() << std::endl;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多