8000+ Star!一個使用 Git 命令操做的數據庫!

Git 和 MySQL 的「孩子」,一個可使用 Git 操做的數據庫。近期連續在 GitHub 趨勢榜霸榜,新增 4k+ Star。git

簡介

Dolt 是一個 SQL 數據庫,咱們可使用 fork、clone、branch、merge、push、pull 等功能,就像在操做一個 git 倉庫同樣;同時,它也像 MySQL 同樣,只要鏈接上 Dolt,咱們就可使用 SQL 語句進行數據的查詢、更新等操做。使用命令行導入 CSV 文件,提交更改,將其推送到遠程或合併團隊成員的更改。github

Git 的全部命令對於 Dolt 來講都是試用的,徹底一致,Dolt 感受就像是 Git 和 MySQL 的孩子同樣。sql

Dolt 有如下命令:shell

$ 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 使用。api

$ 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
內容轉載於 「開源前哨」
相關文章
相關標籤/搜索