惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

www.infosecurity-magazine.com
www.infosecurity-magazine.com
月光博客
月光博客
IT之家
IT之家
博客园 - Franky
大猫的无限游戏
大猫的无限游戏
博客园 - 【当耐特】
Recorded Future
Recorded Future
罗磊的独立博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tailwind CSS Blog
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 聂微东
Last Week in AI
Last Week in AI
S
Schneier on Security
Google DeepMind News
Google DeepMind News
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
AWS News Blog
AWS News Blog
D
Docker
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
Spread Privacy
Spread Privacy
The GitHub Blog
The GitHub Blog
MongoDB | Blog
MongoDB | Blog
P
Palo Alto Networks Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
T
Tenable Blog
A
Arctic Wolf
C
Cisco Blogs
S
SegmentFault 最新的问题
T
The Exploit Database - CXSecurity.com
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Martin Fowler
Martin Fowler
G
GRAHAM CLULEY
The Register - Security
The Register - Security
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Latest news
Latest news
J
Java Code Geeks
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threatpost
L
LangChain Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
腾讯CDC
I
Intezer
Schneier on Security
Schneier on Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org

风萧古道 - 勤学苦练,年复一年

游戏服务器开发经验(五)应对复杂需求 沉浸式体验东汉末年生活 - 《真三国无双 起源》玩后感 怒其不争!致2025年HLTV的Top18-NiKo Linux家用服务器维护指南 游戏服务器开发经验(四)避免写Bug的习惯、技巧和心态 游戏服务器开发经验(三)线上维护 游戏服务器开发经验(二)避免内存泄露 游戏服务器开发经验(一)道具防刷 35岁找不到工作,绝对不会是软件开发人员的结局 用爱发电项目开发两个月的心得体会 以魏延“子午谷奇谋”讨论软件需求可行性问题 MQTT协议中可变长度的具体计算方式(有计算过程解析) 关于游戏服务器配置表功能的探讨 Java并发编程中上锁的几种方式 如何用C++分割一个字符串? CSAPP第二章-信息的表示与处理 我的自我介绍 Windows XP虚拟机中文版无需激活下载 Java TreeSet的一些用法和特性 Linux C++ Socket实战 传统软件服务器与游戏服务器架构区别 独立个人项目开发心得 - 任务切分、挑战性、实用性和半途而废 使用Python实现简单UDP Ping 使用Python开发一个简单的web服务器 Kotlin手动实现一个最简单的哈希表 Kotlin实现二叉堆、大顶堆、优先级队列 搭建Spark实战环境(3台linux虚拟机集群)(一)样板机的搭建 Springboot操作MongoDB,包括增改查及复杂操作 Unison在Linux下的安装与使用 Java实现类似WINSCP访问远程Linux服务器,执行命令、上传文件、下载文件 一个被废弃的项目——自动爬取信息然后发给我自己邮箱上 MongoDB常用查询语句 vue和springboot项目部署到Linux服务器 Python的一些用法(可能不定时更新) java正则表达式 - 双反斜杠(\)和Pattern的matches()与find() 简述爬虫对两种网站的不同爬取方式 Vue的路由配置及手动改地址栏为啥又跳转回来?? [JavaScript]JS基础知识 [Mybatis]逆向工程中Select语句查询不出‘TEXT’字段 [编译原理]FIRST、FOLLOW和SELECT [Spring]Spring学习笔记 [算法]分布估计算法 - 一种求解多维背包问题的混合分布估计算法_王凌 [日常]我做独立博客的原因 人间值得 深入理解计算机系统 想想就开心! 最重要的事,只有一件 藏书 被讨厌的勇气 关于 简历 朋友 尊重自己:给予与接收的心灵艺术
Python连接MongoDB和Oracle实战
JonathanLin · 2020-02-09 · via 风萧古道 - 勤学苦练,年复一年

安装

首先要安装pymongo,用pip装一下就好了。

工具类python文件

以下直接给出我写的mongodb操作类

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/12/23 14:02
# @Author  : Johnathan Lin 林佳庆
"""
数据导入Mongo模块
"""

import os

import pymongo

from utils.configReader import read_mongo_config

# 设置编码
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'


def get_mongodb_collection(collection):
    """
    根据配置文件和数据库和表得到表(collection)
    :param database: 数据库
    :param collection: 表
    :return:  表的对象
    """
    mongo_config = read_mongo_config()
    client = pymongo.MongoClient(mongo_config['client'])
    db = client[mongo_config['database']]
    if mongo_config['auth'] == 'True' or mongo_config['auth'] == 'true':
        db.authenticate(mongo_config['username'], mongo_config['password'])
    col = db[collection]
    return col


def insert_data(collection, document):
    """
    将数据插入MongoDB中
    :param collection:数据将插入的集合名
    :param doc:数据文档
    :return:无
    """
    col = get_mongodb_collection(collection)
    col.insert_one(document)


def insert_data_list(collection, document_list):
    """
    批量插入数据到MongoDB
    :param collection: 数据将插入的集合名
    :param document_list: 数据文档列表
    :return:
    """
    col = get_mongodb_collection(collection)
    col.insert_many(document_list)


def get_children_classify_id(parent_id):
    """
    获取所有子孙节点
    :param parent_id: 父节点ID
    :return: 所有子孙节点
    """
    col = get_mongodb_collection('crawler_classify')
    res = col.aggregate([
        {'$match': {'classify_id': parent_id}},
        {'$graphLookup': {'from': 'crawler_classify', 'startWith': '$classify_id', 'connectFromField': 'classify_id',
                          'connectToField': 'parent_id', 'as': 'son'}}
    ])
    res_list = []
    for doc in res:
        res_list.append(doc)
    if len(res_list) == 0 or len(res_list[0]['son']) == 0:
        return []
    else:
        return [classify['classify_id'] for classify in res_list[0]['son']]


def get_document_by_condition(collection, key, value):
    """
    根据集合和单一条件查找匹配的文档
    :param collection:  集合名
    :param key:  键
    :param value: 值
    :return: 匹配的文档列表
    """
    col = get_mongodb_collection(collection)
    res = col.find({key: value})
    res_list = []
    for doc in res:
        res_list.append(doc)
    return res_list


def update_document(collection, condition_key, condition_value, updated_key, new_value):
    """
    更新文档
    :param collection:
    :param condition_key:
    :param condition_value:
    :param updated_key:
    :param new_value:
    :return:
    """
    col = get_mongodb_collection(collection)
    col.update({condition_key: condition_value}, {'$set': {updated_key: new_value}})


def get_document_by_condition_list(collection, condition_list):
    """
    多条件同时成立查询匹配的文档
    :param collection: 集合名
    :param condition_list:  条件列表(列表中的元素为{key:value}形式)
    :return: 匹配的文档
    """
    col = get_mongodb_collection(collection)
    res = col.find({'$and': condition_list})
    res_list = []
    for doc in res:
        res_list.append(doc)
    return res_list


def remove_document(collection, condition_list):
    """
    删除文档
    :param collection: 删除文档的集合
    :param condition_list:
    :return:
    """
    col = get_mongodb_collection(collection)
    col.remove({'$and': condition_list})


def update_document_condition_list_and_new_value_obj(collection, condition_list, new_value_obj):
    """
    更新update文档,通过多个用“&”连接的条件和新对象更新
    :param collection: 集合
    :param condition_list: 与条件的列表
    :param new_value_obj: 要更新的新值对象
    :return:
    """
    col = get_mongodb_collection(collection)
    col.update({'$and': condition_list}, {'$set': new_value_obj})

配置文件

配置文件长这样:

[CONFIG]
client=mongodb://localhost:27017/
auth=False
username=xxx
password=xxx
database=yyy

获取集合

我们以 get_mongodb_collection(collection)函数为例:

def get_mongodb_collection(collection):
    """
    根据配置文件和数据库和表得到表(collection)
    :param database: 数据库
    :param collection: 表
    :return:  表的对象
    """
    mongo_config = read_mongo_config()
    client = pymongo.MongoClient(mongo_config['client'])
    db = client[mongo_config['database']]
    if mongo_config['auth'] == 'True' or mongo_config['auth'] == 'true':
        db.authenticate(mongo_config['username'], mongo_config['password'])
    col = db[collection]
    return col

在使用Mongo的时候,我们需要先获取其Mongo服务器地址,也就是上文配置文件的client字段内的内容,通过pymongo.MongoClient()方法获取其服务器对象

然后,使用client[‘数据库名’]选择一个数据库,但选中的数据库可能需要验证用户名密码,所以如果需要验证,则使用db.authenticate(用户名,密码)验证;

最后,用db[‘集合名’]取出对应的集合即可使用了。

使用

可以发现,在python里使用mongo,用法和直接敲mongoDB脚本差不多,但是也存在一些不同,比如说‘$and’必须是字符串。

Python连接Oracle

pip装 cx-Oracle(这里注意,会有很多双胞胎几胞胎兄弟,认准这个带横杠的,后面O大写的)。

这是我之前写的python调用oracle。

import cx_Oracle

from utils.dataOperator import insert_data


def start():
    conn = cx_Oracle.connect('oracle用户名/oracle密码@IP地址:端口号/oracle用户')
    parent_id = '6c9da9e2b17b4e1da967f2fdfa05d89a'
    sql = 'select * from 一个表的名字 start with CLASSIFY_ID = \'' + parent_id + '\'connect by prior classify_id = parent_id order by seq'
    cur = conn.cursor()
    cur.execute(sql)
    result_list = cur.fetchall()
    for result in result_list:
        res_obj = {}
        res_obj['classify_id'] = str(result[0])
        # 一顿操作。。。。
        # 在此不表
        insert_data('数据库名', '集合名', res_obj)

使用cx_Oracle.connect(注意这里cx_Oracle是下划线了)调用方法,参数是oracle连接字符串

之后创建一个游标cursor,用它执行sql语句,然后用fetchAll()函数把结果拿回来就可以用了。

但具体怎么用呢?

假如CLASSIFY_ID是执行这行sql返回结果中第一列的列名,那么result[0]就是该行数据在该列的值。

所以需要多加注意。