首頁>技術>

將資料讀寫到MongoDB

MongoDB is a cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License (SSPL).--wiki

MongoDB是一個基於分散式檔案儲存的資料庫。由C++語言編寫。旨在為WEB應用提供可拓展的高效能資料儲存解決方案。MongoDB是一個介於關係資料庫和非關係資料庫之間的產品,是非關係資料庫當中功能最豐富,最像關係資料庫的。它支援的資料結構非常鬆散,是類似json的bson格式,因此可以儲存比較複雜的資料型別。MongDB最大的特點是它支援的查詢語言非常強大,其語法有點類似於面向物件的查詢語言,幾乎可以實現類似關係資料庫單表查詢的絕大部分功能,而且還支援對資料建立索引。---百度百科

這裡需要使用到pymongo,沒有安裝的需要先安裝:pip install pymongo.

或者在jupyter裡面用程式碼安裝:

!pip install pymongo

一、連線MongoDB

比如我們要對圖片中的local database下面的test資料集進行讀寫操作。

先和它建立連線:

from pymongo import MongoClientclient=MongoClient(host='localhost',port=27017)db=client.local     #temp 裡面的 local databasecollection=db.test  #local database裡面的test dataset

二、寫入資料到MongoDB,insert documents

插入一條資料使用:Collection.insert_one({:,:,:,:})

插入多條資料使用:collection.insert_many([])

collection.insert_one({"baby_id":1,"gender":'F',"hair_color":"yellow"})

只插入一條資料的結果:

collection.insert_many([{"baby_id":2,"gender":'M',"hair_color":"black"},                       {"baby_id":3,"gender":'M',"hair_color":"brown"},                       {"baby_id":4,"gender":'F',"hair_color":"red"}])

插入多條資料的結果:

三、從MongDB讀取資料 Query Documents

使用find讀取資料,find()括號裡面設定查詢條件,第一個{}裡面具體的查詢條件,第二個{}裡面設定是否要儲存該變數,0為不需要,1為需要讀取該變數。如果對於某個時間的變數要求,大於或等於某個時間,需對該變數設定時間條件,"$gte"表示greater than and equal。

#例如collection.find({"Time":{"$gte":'2020-12-01 00:00:00'}},{"_id":0,"Time":1,"temperature":1})

下面對步驟二中插入的資料進行讀取:

dic=collection.find({"baby_id":2},{"_id":0,"baby_id":1,"gender":1})
list1=[]for item in dic:    list1.append(item)
pd.DataFrame(data=list1)

檢視MongoDB資料庫,可以使用Studio 3T:

Studio3t官網:https://studio3t.com/

將資料讀寫到Redis

Redis is an open source(BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, list, sets,sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.--https://redis.io/

Redis(Remote Dictionary Server),即遠端字典服務,是一個開源的使用ANSI C語言編寫、支援網路、可基於記憶體亦可持久化的日誌型、Key-Value資料庫,並提供多種語言的API。

key指的是資料庫中的鍵:比如下圖中db0,0庫中的檔案如下所示,其中包含animal資料夾,該資料夾下面包含flow資料夾,flow資料夾下面包含tag_on資料夾,再下一級可以看到一個像鑰匙一樣的圖示,這個鑰匙就是一個key,這個可以用“animal:flow:tag_on:49346410xxx”表示,裡面儲存的內容就是這個key的value,組合在一起就是key:value對。

一、連線Redis

先安裝redis哈,jupyter裡面安裝的話,直接在程式碼前面加感嘆號就可以了。

!pip install redis

連線單結點:

import redisr=redis.Redis(host='172.168.1.199',port=6379)

連線叢集:

from rediscluster import RedisClusterstartup_nodes=[{"host":"172.168.1.158","port":"7000"},{"host":"172.168.1.158","port":"7001"},               {"host":"172.168.1.159","port":"7000"},{"host":"172.168.1.159","port":"7001"},               {"host":"172.168.1.160","port":"7000"},{"host":"172.168.1.160","port":"7001"}]rc=RedisCluster(startup_nodes=startup_nodes,decode_responses=True)

二、寫入資料到Redis叢集

如果 把整個表寫到一個key下面,語句寫法如下,但是檢視的時候會很慢。

r.set(key,dataframe.to_json())

所以可以做迴圈,把每行寫到一個key裡面,檢視的時候就不會卡了。

寫入到單節點:

for i in range(len(markid_temp_ok)):    key='animal:flow:tag_on:'+markid_temp_ok['mark_id'][i]    r.set(key,markid_temp_ok[i:i+1].to_json()) #前面建立連線時單節點 r

寫入到叢集:

for i in range(len(markid_temp_ok)):    key='animal:flow:tag_on:'+markid_temp_ok['mark_id'][i]    rc.set(key,markid_temp_ok[i:i+1].to_json()) #前面連線連線時叢集 rc

三、從Redis叢集中讀取資料

基本語句如下:

r.get('key')

如果存的是json格式,那麼讀完之後需要轉化格式可以直接使用pandas中的module,獲得該key下面的內容。

pd.read_json(r.get(“key”))
#單節點pd.read_json(r.get("animal:flow:tag_on:493464105479311360"))
#叢集,結果都是一樣的pd.read_json(rc.get("animal:flow:tag_on:493464105479311360"))

檢視redis叢集可以使用Redis Desktop Manager

Redis Desktop Manager 官網:https://rdm.dev/

14
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 注意,不能錯過的CAS+volatile實現同步程式碼塊