文档首页
MySQL NDB Cluster API 开发者指南
相关文档 下载此手册
PDF (US Ltr) - 3.6Mb
PDF (A4) - 3.6Mb


MySQL NDB Cluster API 开发者指南  /  ...  /  NDB API 基本插入示例

2.5.1.2 NDB API 基本插入示例

在运行此示例之前,请在连接到集群的 SQL 节点上启动一个 mysql 客户端会话,并执行以下语句以创建示例使用的数据库和表

mysql> CREATE DATABASE ndbapi_examples;

mysql> USE ndbapi_examples;

mysql> CREATE TABLE basic (
     >   ATTR1 INT NOT NULL PRIMARY KEY,
     >   ATTR2 INT NOT NULL
     >   ) ENGINE=NDB;

运行示例后,您可以使用 SELECT * FROM basicmysql 客户端中检查表,或者通过运行示例读取程序(参见 第 2.5.1.3 节,“NDB API 基本读取示例”)检查表。

您还可以在文件 storage/ndb/ndbapi-examples/ndbapi_basic/ndbapi_basic_insert.cpp 中找到此示例的源代码。

#include <iostream>
#include <cstdlib>
#include <string>

#include <NdbApi.hpp>

class BasicInsert
{
  public:
    BasicInsert(const char *connectstring)
      : m_connection(connectstring), m_ndb(&m_connection, "ndbapi_examples") {}
    bool init();
    bool do_insert(long long, long long);

  private:
    Ndb_cluster_connection m_connection;
    Ndb m_ndb;

    inline bool on_error(const struct NdbError &error,
                         const std::string &explanation)
    {
      // prints error in format:
      // ERROR <NdbErrorCode>: <NdbError message>
      //    explanation what went wrong on higher level (in the example code)
      std::cout << "ERROR "<< error.code << ": " << error.message << std::endl;
      std::cout << explanation << std::endl;
      return false;
    }
};


int main(int argc, char **argv)
{
  if (argc != 4)
  {
    std::cout << "Usage: "
              << "ndb_ndbapi_basic_insert <connectstring> <key: int> <value: int>"
              << std::endl;
    return EXIT_FAILURE;
  }

  const char *connectstring = argv[1];
  const long long key = std::strtoll(argv[2], nullptr, 10);
  const long long value = std::strtoll(argv[3], nullptr, 10);

  ndb_init();
  {
    BasicInsert example(connectstring);

    if (!example.init()) return EXIT_FAILURE;

    // Let's verify inserts
    if (example.do_insert(key, value))
      std::cout << "Done, check your database:\n"
                << "\t SELECT * FROM ndbapi_examples.basic;\n"
                << "\t or run the example: ndb_ndbapi_basic_read"
                << std::endl;
    else return EXIT_FAILURE;
  }
  ndb_end(0);

  return EXIT_SUCCESS;
}

bool BasicInsert::do_insert(long long key, long long value)
{
  const NdbDictionary::Dictionary *dict = m_ndb.getDictionary();
  const NdbDictionary::Table *table = dict->getTable("basic");

  if (table == nullptr)
    return on_error(dict->getNdbError(),
                    "Failed to access 'ndbapi_examples.basic'");

  // The insert will be performed within single transaction
  NdbTransaction *transaction = m_ndb.startTransaction(table);
  if(transaction == nullptr)
    return on_error(m_ndb.getNdbError(), "Failed to start transaction");

  NdbOperation *operation = transaction->getNdbOperation(table);
  if(operation == nullptr)
    return on_error(transaction->getNdbError(),
                    "Failed to start insert operation");

  operation->insertTuple();
  operation->equal("ATTR1", key);
  operation->setValue("ATTR2", value);

  if (transaction->execute(NdbTransaction::Commit) != 0)
    return on_error(transaction->getNdbError(),
                    "Failed to execute transaction");

  m_ndb.closeTransaction(transaction);

  return true;
}

bool BasicInsert::init()
{
  if (m_connection.connect() != 0)
  {
    std::cout << "Cannot connect to cluster management server" << std::endl;
    return false;
  }

  if (m_connection.wait_until_ready(30, 0) != 0)
  {
    std::cout << "Cluster was not ready within 30 secs" << std::endl;
    return false;
  }

  if (m_ndb.init() != 0)
    return on_error(m_ndb.getNdbError(), "Failed to initialize ndb object");

  return true;
}