查看原文
其他

【导语】:Git 和 MySQL 的“孩子”,一个可以使用 Git 操作的数据库。最近连续 3 天在 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使用。

 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 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 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


- EOF - 


更多优秀开源项目(点击下方图片可跳转)



开源前哨

日常分享热门、有趣和实用的开源项目。参与维护10万+star 的开源技术资源库,包括:Python, Java, C/C++, Go, JS, CSS, Node.js, PHP, .NET 等

关注后获取

回复 资源 获取 10万+ star 开源资源



分享、点赞和在看

支持我们分享更多优秀开源项目,谢谢!

: . Video Mini Program Like ,轻点两下取消赞 Wow ,轻点两下取消在看

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存