7400 Star!一個(gè)使用 Git 命令操作的數(shù)據(jù)庫(kù)!
【導(dǎo)語(yǔ)】:Git 和 MySQL 的“孩子”,一個(gè)可以使用 Git 操作的數(shù)據(jù)庫(kù)。最近連續(xù) 3 天在 GitHub 趨勢(shì)榜霸榜,新增 4k+ Star。
簡(jiǎn)介
Dolt是一個(gè)SQL數(shù)據(jù)庫(kù),我們可以使用fork、clone、branch、merge、push、pull等功能,就像在操作一個(gè)git倉(cāng)庫(kù)一樣;同時(shí),它也像MySQL一樣,只要連接上Dolt,我們就可以使用SQL語(yǔ)句進(jìn)行數(shù)據(jù)的查詢、更新等操作。使用命令行導(dǎo)入CSV文件,提交更改,將其推送到遠(yuǎn)程或合并團(tuán)隊(duì)成員的更改。
Git的所有命令對(duì)于Dolt來(lái)說(shuō)都是試用的,完全一致,Dolt感覺(jué)就像是Git和MySQL的孩子一樣。
Dolt有以下命令:
$ dolt
Valid commands for dolt are
init - 創(chuàng)建一個(gè)Dolt數(shù)據(jù)倉(cāng)庫(kù).
status - 查看工作空間狀態(tài).
add - 添加修改到暫存區(qū).
reset - 移除暫存區(qū)的修改.
commit - 提交提交到倉(cāng)庫(kù).
sql - 在倉(cāng)庫(kù)中運(yùn)行某一個(gè)sql命令.
sql-server - 啟動(dòng)MySQL兼容服務(wù)器.
log - 查看提交日志.
diff - 比較表.
blame - 查看表每行最后修改的用戶及版本號(hào)e.
merge - 合并分支.
branch - 創(chuàng)建,查看,編輯或刪除分支.
tag - 創(chuàng)建,查看,編輯或刪除標(biāo)簽.
checkout - 切換某個(gè)分支或覆蓋表.
remote - 管理遠(yuǎn)程倉(cāng)庫(kù).
push - 推送到遠(yuǎn)程倉(cāng)庫(kù).
pull - 拉取遠(yuǎn)程倉(cāng)庫(kù)數(shù)據(jù)并合并.
fetch - 從遠(yuǎn)程倉(cāng)庫(kù)更新數(shù)據(jù).
clone - clone遠(yuǎn)程倉(cāng)庫(kù)數(shù)據(jù).
creds - 身份憑證的管理.
login - 登錄遠(yuǎn)程Dolt主機(jī).
version - 查看Dolt版本.
config - Dolt相關(guān)配置.
ls - 查看工作區(qū)中的表.
schema - 查看或?qū)氡斫Y(jié)構(gòu).
table - 復(fù)制,重命名,刪除或?qū)С霰?
conflicts - 查看以及解決合并沖突.
migrate - 執(zhí)行存儲(chǔ)庫(kù)遷移以更新為最新格式.
read-tables - 將特定提交處的表提取到新的倉(cāng)庫(kù)中
gc - 從倉(cāng)庫(kù)中清除未引用的數(shù)據(jù).
項(xiàng)目地址:
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文件直接運(yùn)行即可 源碼安裝,依賴Go環(huán)境,將github源碼clone之后,進(jìn)入到go文件夾,運(yùn)行以下命令:
go install ./cmd/dolt
使用
以存儲(chǔ)州人口數(shù)據(jù)為例,簡(jiǎn)單介紹Dolt使用。
$ mkdir state-pops
$ cd state-pops
執(zhí)行dolt init創(chuàng)建一個(gè)dolt倉(cāng)庫(kù),并運(yùn)行SQL語(yǔ)句添加數(shù)據(jù)
$ 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進(jìn)入SQL命令窗口,或者使用-q直接執(zhí)行SQL語(yǔ)句
$ dolt sql -q "select * from state_populations where state = 'New York'"
+----------+------------+
| state | population |
+----------+------------+
| New York | 340120 |
+----------+------------+
add新的表并提交。每一個(gè)命令的含義都和git一樣,只不過(guò)Dolt針對(duì)表,Git針對(duì)文件
$ dolt add .
$ dolt commit -m "initial data"
$ dolt status
On branch master
nothing to commit, working tree clean
使用SQL更新表,這次進(jìn)入SQL命令窗口執(zhí)行:
$ 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"
導(dǎo)入數(shù)據(jù),使用dolt table import可以導(dǎo)入CSV或者JSON數(shù)據(jù)。-u選項(xiàng)表示將數(shù)據(jù)導(dǎo)入到已有表中,-c表示創(chuàng)建表并導(dǎo)入數(shù)據(jù):
$ 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也支持遠(yuǎn)程倉(cāng)庫(kù)管理,在clone數(shù)據(jù)的時(shí)候,遠(yuǎn)程倉(cāng)庫(kù)會(huì)自動(dòng)建立關(guān)聯(lián)
$ dolt clone dolthub/corona-virus
...
$ cd corona-virus
$ dolt remote -v
origin https://doltremoteapi.dolthub.com/dolthub/corona-virus
如果倉(cāng)庫(kù)是在本地創(chuàng)建,也可以推送到遠(yuǎn)端并創(chuàng)建遠(yuǎn)程倉(cāng)庫(kù)
$ dolt remote add origin myname/myRepo
$ dolt remote -v
origin https://doltremoteapi.dolthub.com/myname/myRepo
$ dolt push origin master
- EOF -
更多優(yōu)秀開(kāi)源項(xiàng)目(點(diǎn)擊下方圖片可跳轉(zhuǎn))
開(kāi)源前哨
日常分享熱門(mén)、有趣和實(shí)用的開(kāi)源項(xiàng)目。參與維護(hù)10萬(wàn)+star 的開(kāi)源技術(shù)資源庫(kù),包括:Python, Java, C/C++, Go, JS, CSS, Node.js, PHP, .NET 等
關(guān)注后獲取
回復(fù) 資源 獲取 10萬(wàn)+ star 開(kāi)源資源
分享、點(diǎn)贊和在看
支持我們分享更多優(yōu)秀開(kāi)源項(xiàng)目,謝謝!



