加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Golang语言开发的开源NoSQL数据库——TieDot简介

发布时间:2020-12-16 18:55:43 所属栏目:大数据 来源:网络整理
导读:简介 项目开源地址:https://github.com/HouzuoGuo/tiedot 发起者留下了他的Twitter,貌似姓郭,是个美籍华人 项目简介中,有关于对性能的描述,有人用此数据库抓取了维基百科,保存5900万数据,共73G。 安装 配置好Go环境后,运行 gogetgithub.com/HouzuoGu

简介

项目开源地址:https://github.com/HouzuoGuo/tiedot

发起者留下了他的Twitter,貌似姓郭,是个美籍华人

项目简介中,有关于对性能的描述,有人用此数据库抓取了维基百科,保存5900万数据,共73G。

安装

配置好Go环境后,运行

gogetgithub.com/HouzuoGuo/tiedot

入门

使用有2种方式,使用HTTP做接口,适用任何语言;使用嵌入式,使用Go语言,这里介绍使用Go语言,数据库的嵌入模式,足以应付百万请求/天了。

项目自带了演示代码,通过下面的命令执行

./tiedot-mode=example

性能评估命令

./tiedot-mode=bench#对40万数据,进行增删改查
./tiedot-mode=bench2#还没仔细看

项目中,example.go文件是Go语言的使用示例

//Itisveryimportanttoinitializerandomnumbergeneratorseed!
	rand.Seed(time.Now().UTC().UnixNano())
	//Createandopendatabase创建并打开数据库
	dir:="/tmp/MyDatabase"
	os.RemoveAll(dir)
	deferos.RemoveAll(dir)

	myDB,err:=db.OpenDB(dir)
	iferr!=nil{
		panic(err)
	}

	//CreatetwocollectionsFeedsandVotes创建2张表
	//"2"meanscollectiondataandindexesaredividedintotwohalves,allowingconcurrentaccessfromtwothreads
	iferr:=myDB.Create("Feeds",2);err!=nil{
		panic(err)
	}
	iferr:=myDB.Create("Votes",2);err!=nil{
		panic(err)
	}

	//WhatcollectionsdoInowhave?查询都有哪些表
	forname:=rangemyDB.StrCol{
		fmt.Printf("Ihaveacollectioncalled%sn",name)
	}

	//Renamecollection"Votes"to"Points"把表"Votes"重命名为"Points"
	iferr:=myDB.Rename("Votes","Points");err!=nil{
		panic(err)
	}

	//Drop(delete)collection"Points"删除表"Points"
	iferr:=myDB.Drop("Points");err!=nil{
		panic(err)
	}

	//Scrub(repairandcompact)"Feeds"修复并压缩表"Feeds"
	myDB.Scrub("Feeds")

	//******************DocumentManagement******************
	//Startusingacollection使用表"Feeds"
	feeds:=myDB.Use("Feeds")

	//Insertdocument(documentmustbemap[string]interface{})插入数据
	docID,err:=feeds.Insert(map[string]interface{}{
		"name":"Go1.2isreleased","url":"golang.org"})
	iferr!=nil{
		panic(err)
	}

	//Readdocument根据id查询数据
	varreadBackinterface{}
	feeds.Read(docID,&readBack)//passindocument'sphysicalID
	fmt.Println(readBack)

	//Updatedocument(documentmustbemap[string]interface{})改数据
	err=feeds.Update(docID,map[string]interface{}{
		"name":"Goisverypopular","url":"google.com"})
	iferr!=nil{
		panic(err)
	}

	//Deletedocument删除数据
	feeds.Delete(docID)

	//Deletedocument
	feeds.Delete(123)//AnIDwhichdoesnotexistdoesnoharm

	//******************IndexManagement******************索引管理
	//Secondaryindexesassistinmanytypesofqueries
	//Createindex(pathleadstodocumentJSONattribute)建索引
	iferr:=feeds.Index([]string{"author","name","first_name"});err!=nil{
		panic(err)
	}
	iferr:=feeds.Index([]string{"Title"});err!=nil{
		panic(err)
	}
	iferr:=feeds.Index([]string{"Source"});err!=nil{
		panic(err)
	}

	//WhatindexesdoIhaveoncollectionA?查询有哪些索引
	forpath:=rangefeeds.SecIndexes{
		fmt.Printf("Ihaveanindexonpath%sn",path)
	}

	//Removeindex删索引
	iferr:=feeds.Unindex([]string{"author","first_name"});err!=nil{
		panic(err)
	}

	//******************Queries******************查询表
	//Let'sprepareanumberofdocmentsforastart
	feeds.Insert(map[string]interface{}{"Title":"NewGorelease","Source":"golang.org","Age":3})
	feeds.Insert(map[string]interface{}{"Title":"Kitkatishere","Source":"google.com","Age":2})
	feeds.Insert(map[string]interface{}{"Title":"GoodSlackware","Source":"slackware.com","Age":1})

	queryStr:=`[{"eq":"NewGorelease","in":["Title"]},{"eq":"slackware.com","in":["Source"]}]`
	varqueryinterface{}
	json.Unmarshal([]byte(queryStr),&query)查询条件

	queryResult:=make(map[uint64]struct{})//queryresult(documentIDs)goesintomapkeys保存查询结果的变量

	iferr:=db.EvalQuery(query,feeds,&queryResult);err!=nil{执行查询
		panic(err)
	}

	//QueryresultsarephysicaldocumentIDs打印查询结果
	forid:=rangequeryResult{
		fmt.Printf("QueryreturneddocumentID%dn",id)
	}

	//Tousethedocumentitself,simplyreaditback
	forid:=rangequeryResult{
		feeds.Read(id,&readBack)
		fmt.Printf("Queryreturneddocument%vn",readBack)
	}

	//Gracefullyclosedatabase关闭数据库
	myDB.Close()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读