返回

DolphinDB C++ API 数据写入使用指南

后端

DolphinDB C++ API 数据写入使用指南

本文为 DolphinDB C++ API 数据写入接口的使用指南,用户在有数据写入需求时,可以根据本篇教程快速明确地选择写入方式。

DolphinDB C++ API 提供了多种写入接口,支持多种数据格式的写入,例如 CSV、JSON、Parquet 等。用户可以选择最适合自己需求的写入方式。

一、流式写入

流式写入是指将数据逐条写入数据库。这种方式适合数据量不大、写入速度要求较高的场景。

DolphinDB C++ API 提供了两种流式写入接口:

  1. insertRow():将一行数据写入数据库。

  2. insertRows():将多行数据写入数据库。

示例

// 包含必要的头文件
#include <iostream>
#include <vector>

#include "dbconnector/dbconnector.h"

using namespace dbconnector;

int main() {
  // 创建一个数据库连接对象
  DBConnector conn("localhost", 8848, "admin", "123456");

  // 打开连接
  conn.open();

  // 创建一个表
  conn.execute("create table test (id int, name string)");

  // 将一行数据写入数据库
  conn.insertRow("test", {1, "John Doe"});

  // 将多行数据写入数据库
  std::vector<std::vector<Value>> rows = {{2, "Jane Doe"}, {3, "Bob Smith"}};
  conn.insertRows("test", rows);

  // 关闭连接
  conn.close();

  return 0;
}

二、批量写入

批量写入是指将数据一次性写入数据库。这种方式适合数据量较大、写入速度要求不高的场景。

DolphinDB C++ API 提供了两种批量写入接口:

  1. insertBatch():将一批数据写入数据库。

  2. insertBatches():将多批数据写入数据库。

示例

// 包含必要的头文件
#include <iostream>
#include <vector>

#include "dbconnector/dbconnector.h"

using namespace dbconnector;

int main() {
  // 创建一个数据库连接对象
  DBConnector conn("localhost", 8848, "admin", "123456");

  // 打开连接
  conn.open();

  // 创建一个表
  conn.execute("create table test (id int, name string)");

  // 将一批数据写入数据库
  std::vector<std::vector<Value>> batch = {{1, "John Doe"}, {2, "Jane Doe"}, {3, "Bob Smith"}};
  conn.insertBatch("test", batch);

  // 将多批数据写入数据库
  std::vector<std::vector<std::vector<Value>>> batches = {{{1, "John Doe"}, {2, "Jane Doe"}}, {{3, "Bob Smith"}, {4, "Mary Johnson"}}};
  conn.insertBatches("test", batches);

  // 关闭连接
  conn.close();

  return 0;
}

三、CSV 文件写入

CSV 文件写入是指将 CSV 文件中的数据写入数据库。这种方式适合数据量较大、数据格式固定的场景。

DolphinDB C++ API 提供了两种 CSV 文件写入接口:

  1. insertCSV():将一个 CSV 文件中的数据写入数据库。

  2. insertCSVs():将多个 CSV 文件中的数据写入数据库。

示例

// 包含必要的头文件
#include <iostream>
#include <vector>

#include "dbconnector/dbconnector.h"

using namespace dbconnector;

int main() {
  // 创建一个数据库连接对象
  DBConnector conn("localhost", 8848, "admin", "123456");

  // 打开连接
  conn.open();

  // 创建一个表
  conn.execute("create table test (id int, name string)");

  // 将一个 CSV 文件中的数据写入数据库
  conn.insertCSV("test", "path/to/file.csv");

  // 将多个 CSV 文件中的数据写入数据库
  std::vector<std::string> files = {"path/to/file1.csv", "path/to/file2.csv", "path/to/file3.csv"};
  conn.insertCSVs("test", files);

  // 关闭连接
  conn.close();

  return 0;
}

四、JSON 文件写入

JSON 文件写入是指将 JSON 文件中的数据写入数据库。这种方式适合数据量较大、数据格式固定的场景。

DolphinDB C++ API 提供了两种 JSON 文件写入接口:

  1. insertJSON():将一个 JSON 文件中的数据写入数据库。

  2. insertJSONs():将多个 JSON 文件中的数据写入数据库。

示例

// 包含必要的头文件
#include <iostream>
#include <vector>

#include "dbconnector/dbconnector.h"

using namespace dbconnector;

int main() {
  // 创建一个数据库连接对象
  DBConnector conn("localhost", 8848, "admin", "123456");

  // 打开连接
  conn.open();

  // 创建一个表
  conn.execute("create table test (id int, name string)");

  // 将一个 JSON 文件中的数据写入数据库
  conn.insertJSON("test", "path/to/file.json");

  // 将多个 JSON 文件中的数据写入数据库
  std::vector<std::string> files = {"path/to/file1.json", "path/to/file2.json", "path/to/file3.json"};
  conn.insertJSONs("test", files);

  // 关闭连接
  conn.close();

  return 0;
}

五、Parquet 文件写入

Parquet 文件写入是指将 Parquet 文件中的数据写入数据库。这种方式适合数据量较大、数据格式固定的场景。

DolphinDB C++ API 提供了两种 Parquet 文件写入接口:

  1. insertParquet():将一个 Parquet 文件中的数据写入数据库。

  2. insertParquets():将多个 Parquet 文件中的数据写入数据库。

示例

// 包含必要的头文件
#include <iostream>
#include <vector>

#include "dbconnector/dbconnector.h"

using namespace dbconnector;

int main() {
  // 创建一个数据库连接对象
  DBConnector conn("localhost", 8848, "admin", "123456");

  // 打开连接
  conn.open();

  // 创建一个表
  conn.execute("create table test (id int, name string)");

  // 将一个 Parquet 文件中的数据写入数据库
  conn.insertParquet("test", "path/to/file.parquet");

  // 将多个 Parquet 文件中的数据写入数据库
  std::vector<std::string> files = {"path/to/file1.parquet", "path/to/file2.parquet", "path/to/file3.parquet"};
  conn.insertParquets("test", files);

  // 关闭连接
  conn.close();

  return 0;
}

六、其他写入方式

除了以上提到的写入方式外,DolphinDB C++ API 还提供了其他一些写入方式,例如:

  • insertValues():将一个值列表写入数据库。
  • insertVectors():将多个值列表写入数据库。
  • insertTable():将一个表中的数据写入另一个表。
  • insertSelect():将一个查询的结果写入另一个表。

用户可以根据自己的实际需要选择最合适的方式进行数据写入。

七、注意事项

在使用 DolphinDB C++ API 进行数据写入时,需要注意以下几点:

  • 在写入数据之前,需要先创建一个表。
  • 在写入数据时,需要指定表的名称和要写入的数据列。
  • 如果要写入的数据包含特殊字符,需要先对其进行转义。
  • 如果要写入的数据量较大,建议使用批量写入的方式。
  • 在写入数据后,需要提交事务才能使数据持久化。