Skip to Content
CockroachDB:权威指南
book

CockroachDB:权威指南

by Guy Harrison, Jesse Seldess, Ben Darnell
May 2025
Intermediate to advanced
488 pages
6h 44m
Chinese
O'Reilly Media, Inc.
Content preview from CockroachDB:权威指南

第 6 章. 应用设计与实施

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

与所有数据库一样,CockroachDB 响应应用程序代码的请求。 应用程序如何请求和使用数据对应用程序的性能和可扩展性有很大影响。 在本章中,我们将回顾应用程序应如何与 CockroachDB 配合使用,包括 CockroachDB 请求和事务模型的最佳编码实践。

由于 CockroachDB 与 PostgreSQL 线协议兼容,因此任何支持 PostgresSQL 的语言都可以与 CockroachDB 配合使用。 一般来说,PostgreSQL 的编程习惯和最佳实践也适用于 CockroachDB。 不过,由于 CockroachDB 的分布式特性,CockroachDB 和 PostgreSQL 的编程风格存在一些差异。

尽管你可以使用几乎所有常用的编程语言来使用 CockroachDB,但在本章中,我们将只讨论四种语言: Go、Java、Python 和 JavaScript。

之前,我们介绍了如何为每种语言安装语言驱动。 有关驱动安装的说明,请参阅第 3 章,或者参阅CockroachDB 文档,以获取更详细的指南,包括如何安装其他语言的驱动或替代驱动。

CockroachDB 编程

CockroachDB 与 SQL 关系数据库广泛兼容,尤其与 PostgreSQL 兼容。 不过,由于 CockroachDB 的分布式特性和事务一致性模型,它有一些独特的编程习惯用法。

在下面的章节中,我们将回顾针对 CockroachDB 服务器编写应用程序的一般原则,并探讨 CockroachDB 特有的一些问题。

执行 CRUD 操作

第 3 章中,我们为每种语言都提供了基本的 "Hello world "示例。让我们扩展这些示例,执行一些非简单的 "CRUD "操作--创建、读取、更新和删除。

编程驱动程序在词汇方面各有不同,但一般都采用类似的语法。 数据库程序的基本操作包括

  • 驱动程序会建立一个连接对象,代表与数据库服务器的连接。在本章中,我们将创建单个连接,但应用程序通常会使用连接池来管理多个可重复使用的连接。

  • 连接对象用于执行 SQL 语句。

  • 有些语句返回的结果集可用于,以遍历SELECT 语句、包含RETURNING 子句的 DML 语句和其他一些返回结果的语句返回的表格输出。

在这里,我们看到了 Java 中的这种基本模式:

package chapter06c;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class example1 {

  public static void main(String[] args) {
    try {
      Class.forName("org.postgresql.Driver");
      String connectionURL = "jdbc:" + args[0];
      String userName = args[1];
      String passWord = args[2];

      Connection connection = DriverManager.getConnection(
          connectionURL, userName, passWord);
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Nanosatellites

Nanosatellites

Rogerio Atem de Carvalho, Jaime Estela, Martin Langer
Trino:权威指南,第二版

Trino:权威指南,第二版

Matt Fuller, Manfred Moser, Martin Traverso
软件工程基础

软件工程基础

Nathaniel Schutta, Dan Vega

Publisher Resources

ISBN: 9798341659186