8000+ Star!一个使用 Git 命令操作的数据库!

Git 和 MySQL 的「孩子」,一个可以使用 Git 操作的数据库。近期连续在 GitHub 趋势榜霸榜,新增 4k+ Star。

简介

Dolt 是一个 SQL 数据库,我们可以使用 fork、clone、branch、merge、push、pull 等功能,就像在操作一个 git 仓库一样;同时,它也像 MySQL 一样,只要连接上 Dolt,我们就可以使用 SQL 语句进行数据的查询、更新等操作。使用命令行导入 CSV 文件,提交更改,将其推送到远程或合并团队成员的更改。

Git 的所有命令对于 Dolt 来说都是试用的,完全一致,Dolt 感觉就像是 Git 和 MySQL 的孩子一样。

Dolt 有以下命令:

$ dolt
Valid commands for dolt are

init - 创建一个Dolt数据仓库.
status - 查看工作空间状态.
add - 添加修改到暂存区.
reset - 移除暂存区的修改.
commit - 提交提交到仓库.
sql - 在仓库中运行某一个sql命令.
sql-server - 启动MySQL兼容服务器.
log - 查看提交日志.
diff - 比较表.
blame - 查看表每行最后修改的用户及版本号e.
merge - 合并分支.
branch - 创建,查看,编辑或删除分支.
tag - 创建,查看,编辑或删除标签.
checkout - 切换某个分支或覆盖表.
remote - 管理远程仓库.
push - 推送到远程仓库.
pull - 拉取远程仓库数据并合并.
fetch - 从远程仓库更新数据.
clone - clone远程仓库数据.
creds - 身份凭证的管理.
login - 登录远程Dolt主机.
version - 查看Dolt版本.
config - Dolt相关配置.
ls - 查看工作区中的表.
schema - 查看或导入表结构.
table - 复制,重命名,删除或导出表.
conflicts - 查看以及解决合并冲突.
migrate - 执行存储库迁移以更新为最新格式.
read-tables - 将特定提交处的表提取到新的仓库中
gc - 从仓库中清除未引用的数据

项目地址:
https://github.com/dolthub/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 文件直接运行即可
源码安装,依赖 Go 环境,将 github 源码 clone 之后,进入到 go 文件夹,运行以下命令:

go install ./cmd/dolt

使用
以存储州人口数据为例,简单介绍 Dolt 使用。

$ mkdir state-pops
$ cd state-pops
执行 dolt init 创建一个 dolt 仓库,并运行 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直接执行SQL语句

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

add 新的表并提交。每一个命令的含义都和 git 一样,只不过 Dolt 针对表,Git 针对文件

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

使用 SQL 更新表,这次进入 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

使用 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 state_populations
$ dolt commit -m "More like Old Jersey"

导入数据,使用 dolt table import 可以导入 CSV 或者 JSON 数据。-u 选项表示将数据导入到已有表中,-c表示创建表并导入数据:

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

就像 git 一样,最好在自己的分支上修改,然后再合并到 master 中

$ dolt checkout -b <branch>
$ dolt merge <branch>

Dolt 也支持远程仓库管理,在 clone 数据的时候,远程仓库会自动建立关联

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

如果仓库是在本地创建,也可以推送到远端并创建远程仓库

$ dolt remote add origin myname/myRepo
$ dolt remote -v
origin https://doltremoteapi.dolthub.com/myname/myRepo
$ dolt push origin master
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值