插入数据

INSERT页面包含使用SQL语句使用各种编程语言将数据写入hubble的说明。

准备工作

在阅读本页之前,请执行以下操作:

  • 创建hubble无服务器集群或启动本地集群。
  • 安装驱动程序或 ORM框架。
  • 连接到数据库。
  • 插入要对其运行查询的数据。

插入行

插入多行时,单个多行插入语句比多个单行语句快。

sql


create table  emp (id INT PRIMARY KEY, aml INT);
insert into  emp (id, aml) VALUES (1, 100), (2, 200);

go语言


// 'db' is an open database connection


if _, err := db.Exec(
    "insert into  emp (id, aml) VALUES (1, 100), (2, 200)"); err != nil {
    log.Fatal(err)
}

JAVA


try (Connection connection = ds.getConnection()) {
    connection.setAutoCommit(false);
    PreparedStatement pstmt = connection.prepareStatement("insert into  emp (id, aml) VALUES (? , ?)");

    pstmt.setInt(1, 100);
    pstmt.setInt(2, 200);
    pstmt.addBatch();

    pstmt.executeBatch();
    connection.commit();
} catch (SQLException e) {
    System.out.printf("sql state = [%s]\ncause = [%s]\nmessage = [%s]\n",
                      e.getSQLState(), e.getCause(), e.getMessage());
}

python


with conn.cursor() as cur:
    cur.execute('insert into  emp (id, aml) VALUES (1, 100), (2, 200)')

conn.commit()

批量插入

如果您需要快速将大量数据导入hubble集群,请使用IMPORT语句而不是从应用程序INSERT。它会更快,因为它完全绕过SQL层并使用低级命令直接写入数据存储。

限制行的大小

为了帮助您避免因应用程序导致行大小膨胀而导致的故障,您可以指定将大于指定大小的行或单个列族写入数据库时​​的行为。使用集群设置sql.guardrails.max_row_size_log来发现大行并使用sql.guardrails.max_row_size_err拒绝大行。

当写入超过的行时sql.guardrails.max_row_size_log

  • INSERT, UPSERT, UPDATE, CREATE TABLE AS, CREATE INDEX, ALTER TABLE, ALTER INDEX, IMPORT, RESTORE语句会将LargeRow记录到SQL_PERF通道中。
  • SELECT, DELETE,TRUNCATEDROP不受影响。

当写入超过的行时sql.guardrails.max_row_size_err

  • INSERT, UPSERTUPDATE语句将失败并出现代码54000 (program_limit_exceeded)错误。

  • CREATE TABLE AS, CREATE INDEX, ALTER TABLE, ALTER INDEX, IMPORTRESTORE语句会将LargeRowInternal事件记录到SQL_INTERNAL_PERF通道中。

  • SELECT, DELETE,TRUNCATEDROP不受影响。