官术网_书友最值得收藏!

NoSQL data

The Not Only Structured Query Language (NoSQL) database is not a relational database; instead, data can be stored in key-value, JSON, document, columnar, or graph formats. They are frequently used in big data and real-time applications. We will learn here how to access NoSQL data using MongoDB, and we assume you have the MongoDB server configured properly and on:

  1. We will need to establish a connection with the Mongo daemon using the MongoClient object. The following code establishes the connection to the default host, localhost , and port (27017). And it gives us access to the database:
from pymongo import MongoClient
client = MongoClient()
db = client.test
  1. In this example, we try to load the cancer dataset available in scikit-learn to the Mongo database. So, we first get the breast cancer dataset and convert it to a pandas DataFrame:
from sklearn.datasets import load_breast_cancer
import pandas as pd

cancer = load_breast_cancer()
data = pd.DataFrame(cancer.data, columns=[cancer.feature_names])

data.head()
  1. Next, we convert this into the JSON format, use the json.loads() function to decode it, and insert the decoded data into the open database:
import json
data_in_json = data.to_json(orient='split')
rows = json.loads(data_in_json)
db.cancer_data.insert(rows)

  1. This will create a collection named cancer_data that contains the data. We can query the document we just created, using the cursor object:
cursor = db['cancer_data'].find({})
df = pd.DataFrame(list(cursor))
print(df)

When it comes to distributed data on the IoT, Hadoop Distributed File System (HDFS) is another popular method for providing distributed data storage and access in IoT systems. In the next section, we study how to access and store data in HDFS.

主站蜘蛛池模板: 泸溪县| 怀安县| 准格尔旗| 华坪县| 天长市| 玉林市| 涿州市| 齐河县| 伊宁市| 手机| 澜沧| 宜兰市| 漳平市| 旺苍县| 石阡县| 巴中市| 郸城县| 乌拉特后旗| 琼海市| 聊城市| 罗山县| 峨边| 连城县| 东乌珠穆沁旗| 高台县| 通江县| 山阴县| 盐池县| 竹溪县| 扶风县| 汝南县| 偏关县| 招远市| 玛纳斯县| 拉孜县| 大埔县| 锦屏县| 龙里县| 吉木乃县| 清徐县| 湘阴县|