vlambda博客
学习文章列表

用 Git 操作的数据库?这个项目火爆了!

关注「 Gi thub大本 」,选择“设为星标”
回复「 资源 ,赠送 价值12800元 编程资料一份~
GitHub大本营
分享有价值,有趣,能快速入门的 GitHub 开源项目,主要分布 前端、JAVA,PHP,Python,Go,.NET,人工智能以及数据分析等领域。
47篇原创内容
Official Account

Git 是一个开源的分布式版本控制系统,可以敏捷高效地管理代码,让项目代码支持同时存在多个不同的版本和分支,是程序员在项目开发种的必备工具。

除了代码文件可以进行版本控制之外,数据其实也可以版本控制!

今天给你们介绍一个开源项目 ——  Dolt,是一个 SQL 数据库,咱们可以像 git 存储库一样进行分叉,克隆,分支,合并,推入和拉入。

像连接任何 MySQL 数据库一样,连接到 Dolt 即可运行查询或使用 SQL 命令更新数据。使用命令行姐面导入 CSV 文件,提交更改,将其推送到远程或合并队友的更改。

对 Git 知道的所有命令对于 Dolt 都完全相同。

1、安装 Dolt

  • 在 Linux 或 Mac 上
sudo bash -c 'curl -L https://github.com/dolthub/dolt/releases/latest/download/install.sh | bash'
  • 使用 Homebrew 安装:
brew install dolt
  • Windows 安装下载 msi 文件直接运行即可
下载 msi 地址:

https://github.com/dolthub/dolt/releases
  • 确保已安装 Go,并且 go路径正确。克隆此存储库,然后将 cd 插入 go 目录。然后运行:
go install ./cmd/dolt

2、配置

通过 dolt 在终端中运行,验证安装是否成功。

 dolt
Valid commands for dolt are
[...]

dolt 使用您的用户名和电子邮件进行配置,创建提交将需要它们。这些命令与 git 完全相同。

 dolt config --global --add user.email [email protected]
 dolt config --global --add user.name "YOUR NAME"

3、入门

让我们创建我们的第一个仓库,存储州人口数据。

mkdir state-pops
cd state-pops

dolt init 设置dolt git一样,运行以设置一个新的仓库。然后运行一些SQL查询以插入数据。

$ dolt init
Successfully initialized dolt data repository.
$ dolt sql -q "create table state_populations ( state varchar(14), population int, primary key (state) )"
$ dolt sql -q "show tables"
+-------------------+
| tables            |
+-------------------+
| state_populations |
+-------------------+
$ dolt sql -q "insert into state_populations (state, population) values
('Delaware', 59096),
('Maryland', 319728),
('Tennessee', 35691),
('Virginia', 691937),
('Connecticut', 237946),
('Massachusetts', 378787),
('South Carolina', 249073),
('New Hampshire', 141885),
('Vermont', 85425),
('Georgia', 82548),
('Pennsylvania', 434373),
('Kentucky', 73677),
('New York', 340120),
('New Jersey', 184139),
('North Carolina', 393751),
('Maine', 96540),
('Rhode Island', 68825)"

Query OK, 17 rows affected

使用dolt sql跳进一个 SQL 壳,或运行跟单的查询 -q 选项。

$ dolt sql -q "select * from state_populations where state = 'New York'"
+----------+------------+
| state    | population |
+----------+------------+
| New York | 340120     |
+----------+------------+

add 新表和 commit 它们。每个命令都 git 完全匹配,但是使用表而不是文件。

$ dolt add .
$ dolt commit -m "initial data"
$ dolt status
On branch master
nothing to commit, working tree clean

这次使用Shell使用更多SQL命令更新表:

$ dolt sql
# Welcome to the DoltSQL shell.
# Statements must be terminated with ';'.
# "exit" or "quit" (or Ctrl-D) to exit.
state_pops> update state_populations set population = 0 where state like 'New%';
Query OK, 3 rows affected
Rows matched: 3  Changed: 3  Warnings: 0
state_pops> exit
Bye

查看您对 dolt diff 以下内容所做的更改:

$ dolt diff
diff --dolt a/state_populations b/state_populations
--- a/state_populations @ qqr3vd0ea6264oddfk4nmte66cajlhfl
+++ b/state_populations @ 17cinjh5jpimilefd57b4ifeetjcbvn2
+-----+---------------+------------+
|     | state         | population |
+-----+---------------+------------+
|  <  | New Hampshire | 141885     |
|  >  | New Hampshire | 0          |
|  <  | New Jersey    | 184139     |
|  >  | New Jersey    | 0          |
|  <  | New York      | 340120     |
|  >  | New York      | 0          |
+-----+---------------+------------+

然后使用dolt add和再次提交更改dolt commit

 dolt add state_populations
 dolt commit -m "More like Old Jersey"

使用来查看存储库的历史记录dolt log

% dolt log
commit babgn65p1r5n36ao4gfdj99811qauo8j
Author: Zach Musgrave <[email protected]>
Date:   Wed Nov 11 13:42:27 -0800 2020

    More like Old Jersey

commit 9hgk7jb7hlkvvkbornpldcopqh2gn6jo
Author: Zach Musgrave <[email protected]>
Date:   Wed Nov 11 13:40:53 -0800 2020

    initial data

commit 8o8ldh58pjovn8uvqvdq2olf7dm63dj9
Author: Zach Musgrave <[email protected]>
Date:   Wed Nov 11 13:36:24 -0800 2020

    Initialize data repository

4、汇入资料

如果数据包含在CSV或JSON之类的平面文件中,则可以使用以下dolt table import 命令将其导入。使用dolt table import -u将数据添加到现有的表,或dolt table import -c创建一个新的。

$ head -n3 data.csv
state,population
Delaware,59096
Maryland,319728
$ dolt table import -c -pk=state state_populations data.csv

5、分支并合并

与 git 一样,最好在自己的分支上进行更改,然后将其合并回master。该dolt checkout命令的工作原理与完全相同git checkout

$ dolt checkout -b <branch>

merge命令的工作原理也相同。

$ dolt merge <branch>

6、使用克隆、推送

Dolt像git一样支持遥控器。当您从其中一个克隆数据时,将自动设置遥控器。

$ dolt clone dolthub/corona-virus
...
cd corona-virus
$ dolt remote -v
origin https://doltremoteapi.dolthub.com/dolthub/corona-virus

要推送到远程,您需要凭据。运行dolt login以打开浏览器以登录并缓存您的本地凭据。您可以使用您的 Google 帐户,Github 帐户或用户名和密码登录 DoltHub。

$ dolt login

如果您有一个本地创建的存储库,现在想将其推送到一个远程服务器上,则完全可以像使用 git 一样添加一个远程服务器。

$ dolt remote add origin myname/myRepo
$ dolt remote -v
origin https://doltremoteapi.dolthub.com/myname/myRepo

然后推到它。

$ dolt push origin master

https://github.com/dolthub/dolt

好了,今天的分享就到这里了,咱们下期再见吧。可以动动小手指给在在点个赞哦。

 
   
   
 
—— 推 荐 阅 读 ——