284-bolt数据库入门






bolt数据库入门



我们来使用一下bolt数据库

特点:
1.轻量级
2.开源
3.go语言实现
4.key-value进行读写



我们打开github搜索一下bolt
找到bolt
https://github.com/boltdb/bolt




我们看下简介
Bolt is a pure Go key/value store inspired by Howard Chu's LMDB project. 
The goal of the project is to provide a simple, fast, and reliable database for projects 
that don't require a full database server such as Postgres or MySQL.

翻译一下
Bolt是一个纯净的go语言的key-value存储,是受到LMDB项目的启发而设计出来的
这个项目的目标是提供一个简单,快速,可靠的数据库
为那些不需要完整数据库服务(比如Postgres或者MySQL)的项目提供支持





然后我们看下他的用法简介

To start a read-write transaction, you can use the DB.Update() function:

err := db.Update(func(tx *bolt.Tx) error {
	...
	return nil
})




Using key/value pairs
To save a key/value pair to a bucket, use the Bucket.Put() function:

db.Update(func(tx *bolt.Tx) error {
	b := tx.Bucket([]byte("MyBucket"))
	err := b.Put([]byte("answer"), []byte("42"))
	return err
})
This will set the value of the "answer" key to "42" in the MyBucket bucket. To retrieve this value, we can use the Bucket.Get() function:

db.View(func(tx *bolt.Tx) error {
	b := tx.Bucket([]byte("MyBucket"))
	v := b.Get([]byte("answer"))
	fmt.Printf("The answer is: %s\n", v)
	return nil
})







然后我们来简单说一下bolt的存储结构

首先第一层
1.数据库的名字 test.db

然后第二层
2.bucket 
这里的bucket就是桶的意思
我们可以理解为容器
这里我们可以有很多个bucket
testBucket01
testBucket02
testBucket03

然后第三层
3.key-value键值对
一个bucket里面存储很多键值对
key-value都是[]byte ==> []byte






然后我们来使用一下bolt
来点代码
写个例子





我们把步骤写出来
func main() {

	//1.创建数据库
	//2.操作数据库
	//	- 写入数据
	//	- 读取数据

}




然后写一下

//1.创建数据库
db, err := bolt.Open(dbName, 0600, nil)
if err!=nil{
	panic(err)
}

defer db.Close()

//2.操作数据库
db.Update(func(tx *bolt.Tx) error{
	
	//1.检查bucket是否存在
	b := tx.Bucket([]byte(bucketName))

	if b == nil{
	b, err = tx.CreateBucket([]byte(bucketName))
	if err !=nil{
	panic(err)
	}
	}

	//2.写入数据
	b.Put([]byte("111"), []byte("hello"))
	b.Put([]byte("222"), []byte("world"))

	//3.读取数据
	v1 := b.Get([]byte("111"))
	v2 := b.Get([]byte("222"))
	v3 := b.Get([]byte("333"))

	return nil
})






然后我们看一下完整代码

package main

import (
	"../lib/bolt-master"
	"fmt"
)

const dbName = "test.db"
const bucketName = "testBucket"

func main() {

	//1.创建数据库
	db, err := bolt.Open(dbName, 0600, nil)
	if err != nil {
		panic(err)
	}

	defer db.Close()

	//2.操作数据库
	db.Update(func(tx *bolt.Tx) error {

		//1.检查bucket是否存在
		b := tx.Bucket([]byte(bucketName))

		if b == nil {
			b, err = tx.CreateBucket([]byte(bucketName))
			if err != nil {
				panic(err)
			}
		}

		//2.写入数据
		b.Put([]byte("111"), []byte("hello"))
		b.Put([]byte("222"), []byte("world"))

		//3.读取数据
		v1 := b.Get([]byte("111"))
		v2 := b.Get([]byte("222"))
		v3 := b.Get([]byte("333"))

		fmt.Println(string(v1))
		fmt.Println(string(v2))
		fmt.Println(string(v3))

		return nil
	})

}






 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值