zoukankan      html  css  js  c++  java
  • Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy

    一、Memcached 

    Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。是线程安全的

    1.1 Memcached安装和基本使用

    Memcached安装:

    依赖libevent
    yum -y install libevent-devel
    apt-get install libevent-dev

    wget http://memcached.org/latest
    tar -zxvf memcached-1.x.x.tar.gz
    ./configure --prefix=/usr/local/memcached; make; make install; make test; 

    安装完后把 /usr/local/memcached/bin 添加到/etc/profile环境变量中,最后. /etc/profile 重新加载环境变量

    1.2 启动Memcached

    memcached -d -m 10 -u root -l 0.0.0.0 -p 12000 -c 256 -P /tmp/memcached.pid

    参数说明:
    -d 是启动一个守护进程
    -m 是分配给Memcache使用的内存数量,单位是MB
    -u 是运行Memcache的用户
    -l 是监听的服务器IP地址
    -p 是设置Memcache监听的端口,最好是1024以上的端口
    -c 选项是最大运行的并发连接数,默认是1024,按照你服务器的负载量来设定
    -P 是设置保存Memcache的pid文件

    1.3 Memcached命令

      存储命令: set | add | replace | append | prepend | cas
      获取命令: get | gets
      其他命令: delete | stats ..

    1.4 Python操作Memcached

    1.4.1 安装API

    python操作Memcached使用Python-memcached模块

    下载安装:https://pypi.python.org/pypi/python-memcached
    pip install python-memcached

    1.4.2 第一次操作

    import memcache
    # 连接memcached服务器 
    mc = memcache.Client(['10.211.55.4:12000'], debug=True)
    mc.set("foo", "bar")     # 设置key value
    ret = mc.get('foo')     # 通过key 获取value
    print ret
    #Ps:debug = True 表示运行出现错误时,现实错误信息,上线后移除该参数。

    1.4.3 memcached集群操作

    python-memcached模块原生支持集群操作,其原理是在内存维护一个主机列表,且集群中主机的权重值和主机在列表中重复出现的次数成正比

        主机    权重
        1.1.1.1   1
        1.1.1.2   2
        1.1.1.3   1
     
    那么在内存中主机列表为:
        host_list = ["1.1.1.1", "1.1.1.2", "1.1.1.2", "1.1.1.3", ]

    如果用户根据如果要在内存中创建一个键值对(如:k1 = "v1"),那么要执行一下步骤:

    • 根据算法将 k1 转换成一个数字
    • 将数字和主机列表长度求余数,得到一个值 N( 0 <= N < 列表长度 )
    • 在主机列表中根据 第2步得到的值为索引获取主机,例如:host_list[N]
    • 连接 将第3步中获取的主机,将 k1 = "v1" 放置在该服务器的内存中

    代码实现如下:

    import memcache
    
    mc = memcache.Client([('10.100.11.216:12000',1), ('10.100.11.211:12000',2), ('10.100.11.212:12000',1)], debug=True)
    mc.set('k1', 'v1')

    1.4.4 add

    添加一条键值对,如果已经存在的 key,重复执行add操作异常

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    import memcache
     
    mc = memcache.Client(['10.211.55.4:12000'], debug=True)
    mc.add('k1', 'v1')
    # mc.add('k1', 'v2') # 对已经存在的key重复添加,失败!!!报错

    1.4.5 replace

    replace 修改某个key的值,如果key不存在,则异常

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    import memcache
     
    mc = memcache.Client(['10.211.55.4:12000'], debug=True)
    # 如果memcache中存在kkkk,则替换成功,否则异常
    mc.replace('kkkk','999')

    1.4.6 set 和 set_multi

        set            设置一个键值对,如果key不存在,则创建,如果key存在,则修改
        set_multi   设置多个键值对,如果key不存在,则创建,如果key存在,则修改

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    import memcache
     
    mc = memcache.Client(['10.211.55.4:12000'], debug=True)
     
    mc.set('k0', 'v5')
    mc.set_multi({'k11':'v11', 'k12':'v12'})
    mc.set_multi({'k13':'v13', 'k14':'v14'})
    mc.set_multi({'k15':'v15', 'k16':'v16'})

    1.4.7 delete 和 delete_multi

      delete 在Memcached中删除指定的一个键值对
      delete_multi 在Memcached中删除指定的多个键值对

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    import memcache
     
    mc = memcache.Client(['10.211.55.4:12000'], debug=True)
     
    mc.delete('k0')
    mc.delete_multi(['k14','k15'])

    1.4.8 get 和 get_multi

      get            获取一个键值对
      get_multi   获取多一个键值对

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    import memcache
     
    mc = memcache.Client(['10.211.55.4:12000'], debug=True)
    
    print(mc.get('k11'))
    print(mc.get_multi(['k11','k12','k16']))

    1.4.9 append 和 prepend

      append    修改指定key的值,在该值 后面 追加内容
      prepend   修改指定key的值,在该值 前面 插入内容

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    import memcache
     
    mc = memcache.Client(['10.211.55.4:12000'], debug=True)
    # k1 = "v1"
     
    mc.append('k1', 'after')
    # k1 = "v1after"
     
    mc.prepend('k1', 'before')
    # k1 = "beforev1after"

    1.4.10 decr 和 incr

      incr  自增,将Memcached中的某一个值增加 N ( N默认为1 )
      decr 自减,将Memcached中的某一个值减少 N ( N默认为1 )

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    import memcache
     
    mc = memcache.Client(['10.211.55.4:12000'], debug=True)
    mc.set('k1', '777')
     
    mc.incr('k1')
    # k1 = 778
     
    mc.incr('k1', 10)
    # k1 = 788
     
    mc.decr('k1')
    # k1 = 787
     
    mc.decr('k1', 10)
    # k1 = 777

    1.4.11 gets 和 cas

    如商城商品剩余个数,假设改值保存在memcache中,product_count = 900
    A用户刷新页面从memcache中读取到product_count = 900
    B用户刷新页面从memcache中读取到product_count = 900

    如果A、B用户均购买商品

    A用户修改商品剩余个数 product_count=899
    B用户修改商品剩余个数 product_count=899

    如此一来缓存内的数据便不在正确,两个用户购买商品后,商品剩余还是 899
    如果使用python的set和get来操作以上过程,那么程序就会如上述所示情况!

    如果想要避免此情况的发生,只要使用 gets 和 cas 即可,如:

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    import memcache
    mc = memcache.Client(['10.211.55.4:12000'], debug=True, cache_cas=True)
     
    v = mc.gets('product_count')
    # ...
    # 如果有人在gets之后和cas之前修改了product_count,那么,下面的设置将会执行失败,抛出异常,从而避免非正常数据的产生
    mc.cas('product_count', "899")

    Ps:本质上每次执行gets时,会从memcache中获取一个自增的数字,通过cas去修改gets的值时,会携带之前获取的自增值和memcache中的自增值进行比较,如果相等,则可以提交,如果不想等,那表示在gets和cas执行之间,又有其他人执行了gets(获取了缓冲的指定值), 如此一来有可能出现非正常数据,则不允许修改。

    Memcached 真的过时了吗?

     二、Redis

    redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。redis-sentinel。redis也是线程安全的 

    2.1 Redis安装和基本使用

    详见 http://tianshili.blog.51cto.com/5050423/1638506

    安装依赖

    yum -y install tcl tcl-devel

    wget http://download.redis.io/releases/redis-3.0.7.tar.gz

    tar -zxvf redis-3.0.7.tar.gz

    cd  redis-3.0.7

    make; make PREFIX=/usr/local/redis install

    /usr/local/redis/bin 添加到PATH环境变量

    2.2 Python操作Redis

    sudo pip install redis

    or
    sudo easy_install redis
    or
    源码安装
     
    详见:https://github.com/WoLpH/redis-py

    redis-clie基本操作

    redis-cli -a passwd -h host -p port  #连接redis服务器

    select db    # db 0 - 15,  redis默认16个数据库

    API使用

    redis-py 的API的使用可以分类为:

    • 连接方式
    • 连接池
    • 操作
      • String 操作
      • Hash 操作
      • List 操作
      • Set 操作
      • Sort Set 操作
    • 管道
    • 发布订阅

    2.2.1 操作模式

    redis-py提供两个类Redis和StrictRedis用于实现Redis的命令,StrictRedis用于实现大部分官方的命令,并使用官方的语法和命令,Redis是StrictRedis的子类,用于向后兼容旧版本的redis-py。

    #!/use/bin/env python
    # -*- coding:utf-8 -*-
    '''
    非连接池
    '''
    import redis
    r = redis.Redis(host='py',port=6379,db=0,password='admin')
    r.set('foo', 'Bar')
    print(r.get('foo'))

    2.2.2 连接池

    #!/use/bin/env python
    # -*- coding:utf-8 -*-
    '''
    连接池
    '''
    import redis
    
    pool = redis.ConnectionPool(host='py',port=6379, db=0, password='admin')
    r = redis.Redis(connection_pool=pool)
    r.set('k1','v1')
    print(r.get('k1'))

    2.2.3 操作

    String操作,redis中的String在在内存中按照一个name对应一个value来存储。如图:

    set(name, value, ex=None, px=None, nx=False, xx=False)

    在Redis中设置值,默认,不存在则创建,存在则修改
    参数:
      ex  过期时间(秒)
      px  过期时间(毫秒)
      nx  如果设置为True,则只有name不存在时,当前set操作才执行
      xx  如果设置为True,则只有name存在时,当前set操作才执行

    setnx(name, value)

      设置值,只有name不存在时,执行设置操作(添加)

    setex(name, value, time)  

    设置值
    参数:
      time,过期时间(数字秒 或 timedelta对象)

    psetex(name, time_ms, value)

    设置值

    参数:
        ime_ms,过期时间(数字毫秒 或 timedelta对象)
     
    mset(*args, **kwargs)

    批量设置值

    如:
        mset(k1='v1', k2='v2')
        
        mget({'k1''v1''k2''v2'})

    get(name)

    获取值

    get(key)

    mget(keys, *args)

    批量获取值

    如:
        mget('ylr''wupeiqi')
        
        r.mget(['ylr''wupeiqi'])

    getset(name, value)

    设置新值并获取原来的值

    getrange(key, start, end)

    # 获取子序列(根据字节获取,非字符)

    # 参数:
        # name,Redis 的 name
        # start,起始位置(字节)
        # end,结束位置(字节)
    # 如:
    set('name','武沛齐') ,0-2表示 '武'  3-5表示 '沛' 6-8 表示 '齐'

    setrange(name, offset, value)

     修改字符串内容,从指定字符串索引开始向后替换(新值太长时,则向后添加)

    # 参数:
        # offset,字符串的索引,字节(一个汉字三个字节)
        # value,要设置的值

    如   r.mset({'z1':'中国经济','z2':'武沛齐'})

    r.setrange('z1',6,'房产业')
    print(r.get('z1'))   # ==> '中国房产业'

    setbit(name, offset, value)

    # 对name对应值的二进制表示的位进行操作
     
    # 参数:
        # name,redis的name
        # offset,位的索引(将值变换成二进制后再进行索引)
        # value,值只能是 1 或 0
     
    # 注:如果在Redis中有一个对应: n1 = "foo",
            那么字符串foo的二进制表示为:01100110 01101111 01101111
        所以,如果执行 setbit('n1', 7, 1),则就会将第7位设置为1,
            那么最终二进制则变成 01100111 01101111 01101111,即:"goo"
     
    # 扩展,转换二进制表示:
     
        # source = "武沛齐"
        source = "foo"
     
        for i in source:
            num = ord(i)
            print bin(num).replace('b','')
     
        特别的,如果source是汉字 "武沛齐"怎么办?
        答:对于utf-8,每一个汉字占 3 个字节,那么 "武沛齐" 则有 9个字节
           对于汉字,for循环时候会按照 字节 迭代,那么在迭代时,将每一个字节转换 十进制数,然后再将十进制数转换成二进制
            11100110 10101101 10100110 11100110 10110010 10011011 11101001 10111101 10010000
            -------------------------- ----------------------------- -----------------------------
                        武                         沛                           齐

    getbit(name, offset)

    # 获取name对应的值的二进制表示中的某位的值 (0或1)

    bitcount(key, start=None, end=None)

    # 获取name对应的值的二进制表示中 1 的个数

    # 参数:
        # key,Redis的name
        # start,位起始位置
        # end,位结束位置
     
    bitcount(self, key, start=None, end=None)
    #Returns the count of set bits in the value of ``key``
    # 获取name对应的值的二进制表示中 1 的个数
    # 参数:
        # key,Redis的name
        # start,位起始位置
        # end,位结束位置
     
    bitop(operation, dest, *keys)
    # 获取多个值,并将值做位运算,将最后的结果保存至新的name对应的值
     
    # 参数:
        # operation,AND(并) 、 OR(或) 、 NOT(非) 、 XOR(异或)
        # dest, 新的Redis的name
        # *keys,要查找的Redis的name
     
    # 如:
        bitop("AND", 'new_name', 'n1', 'n2', 'n3')
        # 获取Redis中n1,n2,n3对应的值,然后讲所有的值做位运算(求并集),然后将结果保存 new_name 对应的值中

    strlen(name)

    返回name对应值的字节长度(一个汉字3个字节)

    incr(self, name, amount=1)

    # 自增 name对应的值,当name不存在时,则创建name=amount,否则,则自增。
     
    # 参数:
        # name,Redis的name
        # amount,自增数(必须是整数)
     
    # 注:同incrby

    incrbyfloat(self, name, amount=1.0)

    # 自增 name对应的值,当name不存在时,则创建name=amount,否则,则自增。
     
    # 参数:
        # name,Redis的name
        # amount,自增数(浮点型)

    decr(self, name, amount=1)

    # 自减 name对应的值,当name不存在时,则创建name=amount,否则,则自减。

    # 参数:
        # name,Redis的name
        # amount,自减数(整数)

    append(key, value)

    # 在redis name对应的值后面追加内容

    # 参数:
        key, redis的name
        value, 要追加的字符串

    2.4 Hash操作,redis中Hash在内存中的存储格式如下图:

     hash 为一个字典

     hset(name, key, value)

    # name对应的hash中设置一个键值对(不存在,则创建;否则,修改)
     
    # 参数:
        # name,redis的name
        # key,name对应的hash中的key
        # value,name对应的hash中的value
     
    # 注:
        # hsetnx(name, key, value),当name对应的hash中不存在当前key时则创建(相当于添加)
    r.hset('n1','nk1','nv2')
    r.hset('n1','nk2','nv22')
    r.hset('n1','nk3','nv33')
     

     hmset(name, mapping)

    # 在name对应的hash中批量设置键值对
     
    # 参数:
        # name,redis的name
        # mapping,字典,如:{'k1':'v1', 'k2': 'v2'}
     
    # 如:
        # r.hmset('xx', {'k1':'v1', 'k2': 'v2'})
    r.hmset('n3',{'k1':1,'k2':2, 'k3':3})
    print(r.hmget('n3', ['k1','k2','k3','k4']))  # --> ['1', '2', '3', None]

    hget(name,key)

    # 在name对应的hash中获取根据key获取value

    print(r.hget('n1','nk3'))  # --> nv33

     hmget(name, keys, *args)

    # 在name对应的hash中获取多个key的值
     
    # 参数:
        # name,reids对应的name
        # keys,要获取key集合,如:['k1', 'k2', 'k3']
        # *args,要获取的key,如:k1,k2,k3
     
    # 如:
        # r.mget('xx', ['k1', 'k2'])
        #
        # print r.hmget('xx', 'k1', 'k2')

    hgetall(name)

    获取name对应hash的所有键值

    print(r.hgetall('n3'))  # --> {'k3': '3', 'k2': '2', 'k1': '1'}

    hlen(name)

    # 获取name对应的hash中所有的key的值

    hvals(name)

    # 获取name对应的hash中所有的value的值

    print(r.hvals('n3'))    # --> ['3', '2', '1']

     hexists(name, key)

     #检查name对应的hash是否存在当前传入的key

    hdel(name,*keys)

    # 将name对应的hash中指定key的键值对删除

    hincrby(name, key, amount=1)

    # 自增name对应的hash中的指定key的值,不存在则创建key=amount
    # 参数:
        # name,redis中的name
        # key, hash对应的key
        # amount,自增数(整数)

    hincrbyfloat(name, key, amount=1.0)

    # 自增name对应的hash中的指定key的值,不存在则创建key=amount
     
    # 参数:
        # name,redis中的name
        # key, hash对应的key
        # amount,自增数(浮点数)
     
    # 自增name对应的hash中的指定key的值,不存在则创建key=amount

    hscan(name, cursor=0, match=None, count=None)

    #!/use/bin/env python
    # -*- coding:utf-8 -*-
    
    
    # 增量式迭代获取,对于数据大的数据非常有用,hscan可以实现分片的获取数据,并非一次性将数据全部获取完,从而防止内存被撑爆
    
    # 参数:
        # name,redis的name
        # cursor,游标(基于游标分批取获取数据)
        # match,匹配指定key,默认None 表示所有的key
        # count,每次分片最少获取个数,默认None表示采用Redis的默认分片个数
    
    # 如:
        # 第一次:cursor1, data1 = r.hscan('xx', cursor=0, match=None, count=None)
        # 第二次:cursor2, data1 = r.hscan('xx', cursor=cursor1, match=None, count=None)
        # ...
        # 直到返回值cursor的值为0时,表示数据已经通过分片获取完毕
    # 如一个10000个key的name
    # 生成
    def gen_dict(n):
        d = {}
        for i in range(n):
            k = 'k'+str(i)
            d[k] = i
        return(d)
    
    dic = gen_dict(10000)
    r.hmset('n4',dic)
    
    # 分片获取
    cursor1 = -1
    while cursor1 != 0:
        if cursor1 == -1:
            cursor1 = 0
        cursor1, data = r.hscan('n4',cursor=cursor1,count=10)
        print(cursor1, data)
    '''打印结果:
    (2048L, {'k1949': '1949', 'k5983': '5983', 'k3959': '3959', 'k686': '686', 'k8119': '8119'})
    (512L, {'k9340': '9340', 'k7996': '7996', 'k9320': '9320', 'k4229': '4229', 'k5338': '5338'})
    ...
    (4095L, {'k3201': '3201', 'k5123': '5123', 'k3165': '3165', 'k67': '67'})
    (0L, {'k1582': '1582', 'k6605': '6605', 'k3159': '3159'}
    
    '''

    hscan_iter(name, match=None, count=None) 

    # 利用yield封装hscan创建生成器,实现分批去redis中获取数据
     
    # 参数:
        # match,匹配指定key,默认None 表示所有的key
        # count,每次分片最少获取个数,默认None表示采用Redis的默认分片个数
     
    # 如:
    for item in r.hscan_iter('n4',match='k11*', count=10):
        print(item)
    '''
    
    ('k1160', '1160')
    ('k1155', '1155')
    ('k1117', '1117')
    ('k1183', '1183')
    
    ...
    
    '''

     2.5 List操作,redis中的List在在内存中按照一个name对应一个List来存储。如图:

    lpush(name,values)

    # 在name对应的list中添加元素,每个新的元素都添加到列表的最左边

    # 如:
        # r.lpush('oo', 11,22,33)
        # 保存顺序为: 33,22,11 
    # 扩展:
        # rpush(name, values) 表示从右向左操作
      r.lpush('oo', 41,42,43,44,45)  # print(r.lrange('oo', 0, -1))  => ['45', '44', '43', '42', '41', '33', '22', '11']
      r.rpush('oo', 51,52,53,54)   # print(r.lrange('oo',start=0,end=-1))  => ['45', '44', '43', '42', '41', '33', '22', '11', '51', '52', '53', '54']

    lpushx(name,value)

    # 在name对应的list中添加元素,只有name已经存在时,值添加到列表的最左边,只能输入一个value

    # 更多:
        # rpushx(name, value) 表示从右向左操作

    llen(name)

    # name对应的list元素的个数

    linsert(name, where, refvalue, value))

     # 在name对应的列表的某一个值前或后插入一个新值

    # 参数:
        # name,redis的name
        # where: 'BEFORE' 或 'AFTER'
        # refvalue,标杆值,即:在它前后插入数据
        # value,要插入的数据
    r.linsert('oo','BEFORE','51','50')
    r.linsert('oo','AFTER','52','48')

     r.lset(name, index, value)

    # 对name对应的list中的某一个索引位置重新赋值

    # 参数:
        # name,redis的name
        # index,list的索引位置
        # value,要设置的值

    r.lrem(name, value, num)

    # 在name对应的list中删除指定的值

    # 参数:
        # name,redis的name
        # value,要删除的值
        # num,  num=0,删除列表中所有的指定值;
               # num=2,从前到后,删除2个;
               # num=-2,从后向前,删除2个

    lpop(name)

    # 在name对应的列表的左侧获取第一个元素并在列表中移除,返回值则是第一个元素

    # 更多:
        # rpop(name) 表示从右向左操作

    lindex(name, index)

    在name对应的列表中根据索引获取列表元素

    lrange(name, start, end)

    # 在name对应的列表分片获取数据

    # 参数:
        # name,redis的name
        # start,索引的起始位置
        # end,索引结束位置

    ltrim(name, start, end)

    # 在name对应的列表中移除没有在start-end索引之间的值

    # 参数:
        # name,redis的name
        # start,索引的起始位置
        # end,索引结束位置

    rpoplpush(src, dst)

    # 从src列表取出最右边的元素,同时将其添加至另dst列表的最左边

    # 参数:
        # src,要取数据的列表的name
        # dst,要添加数据的列表的name

    blpop(keys, timeout)

    """
    LPOP a value off of the first non-empty list
    named in the ``keys`` list.

    If none of the lists in ``keys`` has a value to LPOP, then block
    for ``timeout`` seconds, or until a value gets pushed on to one
    of the lists.

    If timeout is 0, then block indefinitely.
    """

    # 将多个列表排列,按照从左到右去pop对应列表的元素

    # 参数:
        # keys,redis的name的集合
        # timeout,超时时间,当元素所有列表的元素获取完之后,阻塞等待列表内有数据的时间(秒), 0 表示永远阻塞
    # 更多:
        # r.brpop(keys, timeout),从右向左获取数据

    brpoplpush(src, dst, timeout=0)

    # 从src列表的右侧移除一个元素并将其添加到dst列表的左侧

     
    # 参数:
        # src,取出并要移除元素的列表对应的name
        # dst,要插入元素的列表对应的name
        # timeout,当src对应的列表中没有数据时,阻塞等待其有数据的超时时间(秒),0 表示永远阻塞

    自定义增量迭代

    # 由于redis类库中没有提供对列表元素的增量迭代,如果想要循环name对应的列表的所有元素,那么就需要:
        # 1、获取name对应的所有列表
        # 2、循环列表
    # 但是,如果列表非常大,那么就有可能在第一步时就将程序的内容撑爆,所有有必要自定义一个增量迭代的功能:
     
    def list_iter(name):
        """
        自定义redis列表增量迭代
        :param name: redis中的name,即:迭代name对应的列表
        :return: yield 返回 列表元素
        """
        list_count = r.llen(name)
        for index in xrange(list_count):
            yield r.lindex(name, index)
     
    # 使用
    for item in list_iter('pp'):
        print item

    2.6 Set操作,Set集合就是不允许重复的列表

    sadd(name,values)

    # name对应的集合中添加元素

    scard(name)

    获取name对应的集合中元素个数

    sdiff(keys, *args)

    在第一个name对应的集合中且不在其他name对应的集合的元素集合(取差集)

    sdiffstore(dest, keys, *args)

    # 获取第一个name对应的集合中且不在其他name对应的集合,再将其新加入到dest对应的集合中 

    sinter(keys, *args)

    # 获取多一个name对应集合的并集 

    sinterstore(dest, keys, *args)

    #获取多一个name对应集合的并集,再将其加入到dest对应的集合中

    sismember(name, value)

    # 检查value是否是name对应的集合的成员

    smembers(name)

    # 获取name对应的集合的所有成员

    smove(src, dst, value)

    # 将某个成员从src集合中移动到dst集合

    spop(name)

    # 从集合的右侧(尾部)移除一个成员,并将其返回

    srandmember(name, numbers)

    # 从name对应的集合中随机获取 numbers 个元素

    srem(name, values)

    # 在name对应的集合中删除某些值

    sunion(keys, *args)

    # 获取多个name对应的集合的并集

    sunionstore(dest,keys, *args

    # 获取多个name对应的集合的并集,并将结果保存到dest对应的集合中

    sscan(name, cursor=0, match=None, count=None)

    ""
    Incrementally return lists of elements in a set. Also return a cursor
    indicating the scan position.

    ``match`` allows for filtering the keys by pattern

    ``count`` allows for hint the minimum number of returns
    """

    # 增量获取name 中的元素

    sscan_iter(name, match=None, count=None)

    # 同字符串的操作,用于增量迭代分批获取元素,避免内存消耗太大

    2.6.2 有序集合

    有序集合,在集合的基础上,为每元素排序;元素的排序需要根据另外一个值来进行比较,所以,对于有序集合,每一个元素有两个值,即:值和分数,分数专门用来做排序。

    zadd(name, *args, **kwargs)

    # 在name对应的有序集合中添加元素

    # 如:
         # zadd('zz', 'n1', 1, 'n2', 2)
         # 或
         # zadd('zz', n1=11, n2=22)

    zcard(name)

    # 获取name对应的有序集合元素的数量

    zcount(name, min, max)

     # 获取name对应的有序集合中分数 在 [min,max] 之间的个数

     zincrby(name, value, amount)

     # 自增name对应的有序集合的 name 对应的分数

     r.zrange( name, start, end, desc=False, withscores=False, score_cast_func=float)

    # 按照索引范围获取name对应的有序集合的元素
     
    # 参数:
        # name,redis的name
        # start,有序集合索引起始位置(非分数)
        # end,有序集合索引结束位置(非分数)
        # desc,排序规则,默认按照分数从小到大排序
        # withscores,是否获取元素的分数,默认只获取元素的值
        # score_cast_func,对分数进行数据转换的函数
     
    # 更多:
        # 从大到小排序
        # zrevrange(name, start, end, desc=True, withscores=False, score_cast_func=float)
     
        # 按照分数范围获取name对应的有序集合的元素
        # zrangebyscore(name, min, max, start=None, num=None, withscores=False, score_cast_func=float)
        # 从大到小排序
        # zrevrangebyscore(name, max, min, start=None, num=None, desc=True withscores=False, score_cast_func=float)

    zrank(name, value)

    # 获取某个值在 name对应的有序集合中的排行(从 0 开始)

     
    # 更多:
        # zrevrank(name, value),从大到小排序

    zrangebylex(name, min, max, start=None, num=None)

    # 当有序集合的所有成员都具有相同的分值时,有序集合的元素会根据成员的 值 (lexicographical ordering)来进行排序,而这个命令则可以返回给定的有序集合键 key 中, 元素的值介于 min 和 max 之间的成员
    # 对集合中的每个成员进行逐个字节的对比(byte-by-byte compare), 并按照从低到高的顺序, 返回排序后的集合成员。 如果两个字符串有一部分内容是相同的话, 那么命令会认为较长的字符串比较短的字符串要大
     
    # 参数:
        # name,redis的name
        # min,左区间(值)。 + 表示正无限; - 表示负无限; ( 表示开区间; [ 则表示闭区间
        # min,右区间(值)
        # start,对结果进行分片处理,索引位置
        # num,对结果进行分片处理,索引后面的num个元素
     
    # 如:
        # ZADD myzset 0 aa 0 ba 0 ca 0 da 0 ea 0 fa 0 ga
        # r.zrangebylex('myzset', "-", "[ca") 结果为:['aa', 'ba', 'ca']
     
    # 更多:
        # 从大到小排序
        # zrevrangebylex(name, max, min, start=None, num=None)

    zrem(name, values)

    # 删除name对应的有序集合中值是values的成员

    # 如:zrem('zz', ['s1', 's2'])

    zremrangebyrank(name, min, max)

    # 根据排行范围删除

    zremrangebyscore(name, min, max)

    # 根据分数范围删除

    zremrangebylex(name, min, max)

    # 根据值返回删除

    zscore(name, value)

    # 获取name对应有序集合中 value 对应的分数

    zinterstore(dest, keys, aggregate=None)

     # 获取两个有序集合的交集并保存到新的dest集合中,如果遇到相同值不同分数,则按照aggregate进行操作

    # aggregate的值为:  SUM  MIN  MAX

    zunionstore(dest, keys, aggregate=None) 

    # 获取两个有序集合的并集新的dest集合中,如果遇到相同值不同分数,则按照aggregate进行操作

    # aggregate的值为:  SUM  MIN  MAX

    zscan(name, cursor=0, match=None, count=None, score_cast_func=float)

    """
    Incrementally return lists of elements in a sorted set. Also return a
    cursor indicating the scan position.

    ``match`` allows for filtering the keys by pattern

    ``count`` allows for hint the minimum number of returns

    ``score_cast_func`` a callable used to cast the score return value
    """

    zscan_iter(name, match=None, count=None,score_cast_func=float)

    # 同字符串相似,相较于字符串新增score_cast_func,用来对分数进行操作

    2.7 其他常用操作

    delete(*names)

    "Delete one or more keys specified by ``names``"

    #删除redis中的任意数据类型

    exists(name)

    # 检测redis的name是否存在

    keys(pattern='*')

    # 根据模型获取redis的name
     
    # 更多:
        # KEYS * 匹配数据库中所有 key 。
        # KEYS h?llo 匹配 hello , hallo 和 hxllo 等。
        # KEYS h*llo 匹配 hllo 和 heeeeello 等。
        # KEYS h[ae]llo 匹配 hello 和 hallo ,但不匹配 hillo

    expire(name ,time)

    # 为某个redis的某个name设置超时时间

    rename(src, dst)

    # 对redis的name重命名为

    move(name, db))

    # 将redis的某个值移动到指定的db下

    randomkey()

    # 随机获取一个redis的name(不删除)

    type(name)

    # 获取name对应值的类型

    scan(cursor=0, match=None, count=None)

    """
    Incrementally return lists of key names. Also return a cursor
    indicating the scan position.

    ``match`` allows for filtering the keys by pattern

    ``count`` allows for hint the minimum number of returns
    """


    scan_iter(match=None, count=None)

    # 同字符串操作,用于增量迭代获取key

    2.8 管道

    redis-py默认在执行每次请求都会创建(连接池申请连接)和断开(归还连接池)一次连接操作,如果想要在一次请求中指定多个命令,则可以使用pipline实现一次请求指定多个命令,并且默认情况下一次pipline 是原子性操作。

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
     
    import redis
     
    pool = redis.ConnectionPool(host='10.211.55.4', port=6379)
     
    r = redis.Redis(connection_pool=pool)
     
    # pipe = r.pipeline(transaction=False)
    pipe = r.pipeline(transaction=True)
     
    r.set('name', 'alex')
    r.set('role', 'sb')
     
    pipe.execute()

    2.9 发布订阅

    发布者:服务器

    订阅者:Dashboad和数据处理

    Demo如下:

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    import redis
    
    
    class RedisHelper:
    
        def __init__(self):
            self.__conn = redis.Redis(host='10.211.55.4')
            self.chan_sub = 'fm104.5'
            self.chan_pub = 'fm104.5'
    
        def public(self, msg):
            self.__conn.publish(self.chan_pub, msg)
            return True
    
        def subscribe(self):
            pub = self.__conn.pubsub()
            pub.subscribe(self.chan_sub)
            pub.parse_response()
            return pub

    订阅者:

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
     
    from monitor.RedisHelper import RedisHelper
     
    obj = RedisHelper()
    redis_sub = obj.subscribe()
     
    while True:
        msg= redis_sub.parse_response()
        print msg

    发布者:

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
     
    from monitor.RedisHelper import RedisHelper
     
    obj = RedisHelper()
    obj.public('hello')

    更多参见:https://github.com/andymccurdy/redis-py/

    http://doc.redisfans.com/

    三、RabbitMQ

    RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统。他遵循Mozilla Public License开源协议。

    MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法。应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们。消 息传递指的是程序之间通过在消息中发送数据进行通信,而不是通过直接调用彼此来通信,直接调用通常是用于诸如远程过程调用的技术。排队指的是应用程序通过 队列来通信。队列的使用除去了接收和发送应用程序同时执行的要求。

    3.1 RabbitMQ安装

    安装配置epel源

       $ rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
    安装erlang
       $ yum -y install erlang
     
    安装RabbitMQ
       $ yum -y install rabbitmq-server
    注意:service rabbitmq-server start/stop
    源码或bin文件安装参照 http://tianshili.blog.51cto.com/5050423/1638502
     
    安装API
    pip install pika
    or
    easy_install pika
    or
    源码
     
    https://pypi.python.org/pypi/pika
     

    使用API操作RabbitMQ

    基于Queue实现生产者消费者模型

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    import Queue
    import threading
    
    
    message = Queue.Queue(10)
    
    
    def producer(i):
        while True:
            message.put(i)
    
    
    def consumer(i):
        while True:
            msg = message.get()
    
    
    for i in range(12):
        t = threading.Thread(target=producer, args=(i,))
        t.start()
    
    for i in range(10):
        t = threading.Thread(target=consumer, args=(i,))
        t.start()

    3.2 rabbitmq生产者、消费者

    对于RabbitMQ来说,生产和消费不再针对内存里的一个Queue对象,而是某台服务器上的RabbitMQ Server实现的消息队列。

    生产者:

    #!/use/bin/env python
    # -*- coding:utf-8 -*-
    
    # 生产者
    # rabbitmq_producer
    import pika
    connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='py',
    port=5672,
    virtual_host='/' credentials
    =pika.PlainCredentials(username='admin',password='admin') # Authentication Credentials 认证凭据 ) ) channel = connection.channel() channel.queue_declare(queue='hello')    #声明 queue def p(n): for i in range(n):
         # RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange. channel.basic_publish(exchange
    ='',routing_key='hello',body='Hello World! v{}'.format(i)) print("[x] Sent 'Hello World!' v%s", (i)) connection.close() p(1000)

    消费者:

    #!/use/bin/env python
    # -*- coding:utf-8 -*-
    
    # 消费者
    # rabbitMQ_consumer
    
    import pika
    
    connection = pika.BlockingConnection(pika.ConnectionParameters(host='py',credentials=pika.PlainCredentials('admin','admin')))
    channel = connection.channel()

    #You may ask why we declare the queue again ‒ we have already declared it in our previous code.
    # We could avoid that if we were sure that the queue already exists. For example if send.py program
    #was run before. But we're not yet sure which program to run first. In such cases it's a good
    # practice to repeat declaring the queue in both programs.

    
    channel.queue_declare(queue='hello')    #声明queue
    
    def callback(ch, method, properties, body):
        print(" [x] Received %r" % body)
        # print(" [x] Received %r" % body, ch, method, properties)  # ==> (" [x] Received 'Hello World! v15044'", <pika.adapters.blocking_connection.BlockingChannel object at 0x0000000003D7D748>, <Basic.Deliver(['consumer_tag=ctag1.350f5f092a8c4c018ab468fe9f09b107', 'delivery_tag=15045', 'exchange=', 'redelivered=False', 'routing_key=hello'])>, <BasicProperties>)
    
    
    
    channel.basic_consume(callback, queue='hello',no_ack=True)
    print(' [*] Waiting for messages. To exit press CTRL+C')
    
    channel.start_consuming()

    3.2.1 acknowledgment 消息不丢失

    no-ack = False,如果生产者遇到情况(its channel is closed, connection is closed, or TCP connection is lost)挂掉了,那么,RabbitMQ会重新将该任务添加到队列中。

    3.2.2 durable  消息持久化

    We have learned how to make sure that even if the consumer dies, the task isn't lost(by default, if wanna disable  use no_ack=True). But our tasks will still be lost if RabbitMQ server stops.

    When RabbitMQ quits or crashes it will forget the queues and messages unless you tell it not to. Two things are required to make sure that messages aren't lost: we need to mark both the queue and messages as durable.

    First, we need to make sure that RabbitMQ will never lose our queue. In order to do so, we need to declare it as durable:

    channel.queue_declare(queue='hello', durable=True)

    Although this command is correct by itself, it won't work in our setup. That's because we've already defined a queue called hello which is not durable. RabbitMQ doesn't allow you to redefine an existing queue with different parameters and will return an error to any program that tries to do that. But there is a quick workaround - let's declare a queue with different name, for exampletask_queue:

    channel.queue_declare(queue='task_queue', durable=True)

    This queue_declare change needs to be applied to both the producer and consumer code.

    At that point we're sure that the task_queue queue won't be lost even if RabbitMQ restarts. Now we need to mark our messages as persistent - by supplying a delivery_mode property with a value 2.

    channel.basic_publish(exchange='',
                          routing_key="task_queue",
                          body=message,
                          properties=pika.BasicProperties(
                             delivery_mode = 2, # make message persistent
                          ))

     生产者:

    #!/use/bin/env python
    # -*- coding:utf-8 -*-
    
    import pika
    
    conncection = pika.BlockingConnection(pika.ConnectionParameters(
        host='py',port=5672,virtual_host='/',credentials=pika.PlainCredentials('admin','admin')
    ))
    channel = conncection.channel()
    
    # make message persistent(消息持久化)
    #channel.queue_delete(queue='hello')
    channel.queue_declare(queue='hello2',durable=True)     # 声明queue持久化,RabbitMQ重启后queue还会存在。若无下面的delivery_mode=2,则mq重启后消息体会丢失
    
    def p(n):
        for i in range(n):
            channel.basic_publish(exchange='',
                                  routing_key='hello2',
                                  body='Hello World! %s' %(i),
                                  properties=pika.BasicProperties(delivery_mode=2, # 消息体持久化。properties性能, delivery_mode生产模式 make message persistent
                                ))
            print(" [x] Sent 'Hello World! No.%s'" %(i))
        conncection.close()
    
    p(1000000)

     消费者

    #!/use/bin/env python
    # -*- coding:utf-8 -*-
    
    # 消费者
    # rabbitMQ_consumer
    
    import pika
    
    connection = pika.BlockingConnection(pika.ConnectionParameters(host='py',
                                            credentials=pika.PlainCredentials('admin','admin')))
    channel = connection.channel()
    channel.queue_declare(queue='hello2',durable=True)       #声明queue持久化
    
    def callback(ch, method, properties, body):
        print(" [x] Received %r" % body)
        import time
        time.sleep(1)
        print('ok')
        ch.basic_ack(delivery_tag = method.delivery_tag)        # 消息体持久化,在此来确认消息消费了,这时候,mq里才会删除这条消息
    
    channel.basic_consume(callback,
                          queue='hello2',
                          no_ack=False)
    print('[*] Waiting for messages. To exit press CTRL+C')
    channel.start_consuming()

     3.2.3 Work Queues

    在这种模式下,RabbitMQ会默认把p发的消息依次分发给各个消费者(c),跟负载均衡差不多

    生产者:

    import pika
     
    connection = pika.BlockingConnection(pika.ConnectionParameters(
                   'localhost'))
    channel = connection.channel()
     
    #声明queue
    channel.queue_declare(queue='task_queue')
     
    #n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
    import sys
     
    message = ' '.join(sys.argv[1:]) or "Hello World!"
    channel.basic_publish(exchange='',
                          routing_key='task_queue',
                          body=message,
                          properties=pika.BasicProperties(
                          delivery_mode = 2, # make message persistent
                          ))
    print(" [x] Sent %r" % message)
    connection.close()

    消费者:

    import pika,time
     
    connection = pika.BlockingConnection(pika.ConnectionParameters(
                   'localhost'))
    channel = connection.channel()
     
     
     
    def callback(ch, method, properties, body):
        print(" [x] Received %r" % body)
        time.sleep(body.count(b'.'))
        print(" [x] Done")
        ch.basic_ack(delivery_tag = method.delivery_tag)
     
     
    channel.basic_consume(callback,
                          queue='task_queue',
                          )
     
    print(' [*] Waiting for messages. To exit press CTRL+C')
    channel.start_consuming()

    此时,先启动消息生产者,然后再分别启动3个消费者,通过生产者多发送几条消息,你会发现,这几条消息会被依次分配到各个消费者身上  

    Doing a task can take a few seconds. You may wonder what happens if one of the consumers starts a long task and dies with it only partly done. With our current code once RabbitMQ delivers message to the customer it immediately removes it from memory. In this case, if you kill a worker we will lose the message it was just processing. We'll also lose all the messages that were dispatched to this particular worker but were not yet handled.

    But we don't want to lose any tasks. If a worker dies, we'd like the task to be delivered to another worker.

    In order to make sure a message is never lost, RabbitMQ supports message acknowledgments. An ack(nowledgement) is sent back from the consumer to tell RabbitMQ that a particular message had been received, processed and that RabbitMQ is free to delete it.

    If a consumer dies (its channel is closed, connection is closed, or TCP connection is lost) without sending an ack, RabbitMQ will understand that a message wasn't processed fully and will re-queue it. If there are other consumers online at the same time, it will then quickly redeliver it to another consumer. That way you can be sure that no message is lost, even if the workers occasionally die.

    There aren't any message timeouts; RabbitMQ will redeliver the message when the consumer dies. It's fine even if processing a message takes a very, very long time.

    Message acknowledgments are turned on by default. In previous examples we explicitly turned them off via the no_ack=True flag. It's time to remove this flag and send a proper acknowledgment from the worker, once we're done with a task.

    def callback(ch, method, properties, body):
        print " [x] Received %r" % (body,)
        time.sleep( body.count('.') )
        print " [x] Done"
        ch.basic_ack(delivery_tag = method.delivery_tag)
     
    channel.basic_consume(callback,
                          queue='hello')

    Using this code we can be sure that even if you kill a worker using CTRL+C while it was processing a message, nothing will be lost. Soon after the worker dies all unacknowledged messages will be redelivered

     3.2.4 消息公平分发

    默认消息队列里的数据是按照顺序被消费者拿走,例如:消费者1 去队列中获取 奇数 序列的任务,消费者2去队列中获取 偶数 序列的任务。

    channel.basic_qos(prefetch_count=1) (这里的1表示一次取1个,也可以设置其他数)表示谁来谁取,不再按照奇偶数排列

     如果Rabbit只管按顺序把消息发到各个消费者身上,不考虑消费者负载的话,很可能出现,一个机器配置不高的消费者那里堆积了很多消息处理不完,同时配置高的消费者却一直很轻松。为解决此问题,可以在各个消费者端,配置prefetch_count=1,意思就是告诉RabbitMQ在我这个消费者当前消息还没处理完的时候就不要再给我发新消息了。

    然而在实际使用过程中,由于消费者自身处理能力有限,从rabbitmq获取一定数量的消息后,希望rabbitmq不再将队列中的消息推送过来,当对消息处理完后(即对消息进行了ack,并且有能力处理更多的消息)再接收来自队列的消息。在这种场景下,我们可以通过设置basic.qos信令中的prefetch_count来达到这种效果。

    1. rabbitmq对basic.qos信令的处理

    首先,basic.qos是针对channel进行设置的,也就是说只有在channel建立之后才能发送basic.qos信令。

    在rabbitmq的实现中,每个channel都对应会有一个rabbit_limiter进程,当收到basic.qos信令后,在rabbit_limiter进程中记录信令中prefetch_count的值,同时记录的还有该channel未ack的消息个数。

    注:其实basic.qos里还有另外两个参数可进行设置(global和prefetch_size),但rabbitmq没有相应的实现。

    2. 队列中的消息投递给消费者时的处理

    当rabbitmq要将队列中的一条消息投递给消费者时,会遍历该队列上的消费者列表,选一个合适的消费者,然后将消息投递出去。其中挑选消费者的一个依据就是看消费者对应的channel上未ack的消息数是否达到设置的prefetch_count个数,如果未ack的消息数达到了prefetch_count的个数,则不符合要求。当挑选到合适的消费者后,中断后续的遍历。

     

    channel.basic_qos(prefetch_count=1)

    带消息持久化+公平分发的完整代码

    生产者:

    #!/usr/bin/env python
    import pika
    import sys
     
    connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='localhost'))
    channel = connection.channel()
     
    channel.queue_declare(queue='task_queue', durable=True)
     
    message = ' '.join(sys.argv[1:]) or "Hello World!"
    channel.basic_publish(exchange='',
                          routing_key='task_queue',
                          body=message,
                          properties=pika.BasicProperties(
                             delivery_mode = 2, # make message persistent
                          ))
    print(" [x] Sent %r" % message)
    connection.close()

    消费者:

    #!/usr/bin/env python
    import pika
    import time
     
    connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='localhost'))
    channel = connection.channel()
     
    channel.queue_declare(queue='task_queue', durable=True)
    print(' [*] Waiting for messages. To exit press CTRL+C')
     
    def callback(ch, method, properties, body):
        print(" [x] Received %r" % body)
        time.sleep(body.count(b'.'))
        print(" [x] Done")
        ch.basic_ack(delivery_tag = method.delivery_tag)
     
    channel.basic_qos(prefetch_count=1)
    channel.basic_consume(callback,
                          queue='task_queue')
     
    channel.start_consuming()

    3.3 PublishSubscribe(消息发布订阅)

    发布订阅和简单的消息队列区别在于,发布订阅会将消息发送给所有的订阅者,而消息队列中的数据被消费一次便消失。所以,RabbitMQ实现发布和订阅时,会为每一个订阅者创建一个队列,而发布者发布消息时,会将消息放置在所有相关队列中。

    之前的例子都基本都是1对1的消息发送和接收,即消息只能发送到指定的queue里,但有些时候你想让你的消息被所有的Queue收到,类似广播的效果,这时候就要用到exchange了,

    An exchange is a very simple thing. On one side it receives messages from producers and the other side it pushes them to queues. The exchange must know exactly what to do with a message it receives. Should it be appended to a particular queue? Should it be appended to many queues? Or should it get discarded. The rules for that are defined by the exchange type.

    Exchange在定义的时候是有类型的,以决定到底是哪些Queue符合条件,可以接收消息


    fanout: 所有bind到此exchange的queue都可以接收消息
    direct: 通过routingKey和exchange决定的那个唯一的queue可以接收消息
    topic:所有符合routingKey(此时可以是一个表达式)的routingKey所bind的queue可以接收消息

       表达式符号说明:#代表一个或多个字符,*代表任何字符
          例:#.a会匹配a.a,aa.a,aaa.a等
              *.a会匹配a.a,b.a,c.a等
         注:使用RoutingKey为#,Exchange Type为topic的时候相当于使用fanout 

    headers: 通过headers 来决定把消息发给哪些queue

    3.3.1 fanout(发送到所有bind绑定到exchange的queue)

    消息publisher:

    import pika
    import sys
     
    connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='localhost'))
    channel = connection.channel()
     
    channel.exchange_declare(exchange='logs',
                             exchange_type='fanout')
     
    message = ' '.join(sys.argv[1:]) or "info: Hello World!"
    channel.basic_publish(exchange='logs',
                          routing_key='',
                          body=message)
    print(" [x] Sent %r" % message)
    connection.close()

    消息subscriber:

    #_*_coding:utf-8_*_
    
    import pika
     
    connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='localhost'))
    channel = connection.channel()
     
    channel.exchange_declare(exchange='logs',
                             exchange_type='fanout')
     
    result = channel.queue_declare(exclusive=True) #不指定queue名字,rabbit会随机分配一个名字,exclusive=True会在使用此queue的消费者断开后,自动将queue删除
    queue_name = result.method.queue
     
    channel.queue_bind(exchange='logs',
                       queue=queue_name)
     
    print(' [*] Waiting for logs. To exit press CTRL+C')
     
    def callback(ch, method, properties, body):
        print(" [x] %r" % body)
     
    channel.basic_consume(callback,
                          queue=queue_name,
                          no_ack=True)
     
    channel.start_consuming()

    3.3.2  direct(关键字发送,有选择的收发消息,exchange type=direct)

    RabbitMQ还支持根据关键字发送,即:队列绑定关键字,发送者将数据根据关键字发送到消息exchange,exchange根据 关键字 判定应该将数据发送至指定队列。

    可以绑定一个或多个关键字

    publisher:

    import pika
    import sys
     
    connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='localhost'))
    channel = connection.channel()
     
    channel.exchange_declare(exchange='direct_logs',
                             exchange_type='direct')
     
    severity = sys.argv[1] if len(sys.argv) > 1 else 'info'
    message = ' '.join(sys.argv[2:]) or 'Hello World!'
    channel.basic_publish(exchange='direct_logs',
                          routing_key=severity,
                          body=message)
    print(" [x] Sent %r:%r" % (severity, message))
    connection.close() 

    subscriber:

    import pika
    import sys
     
    connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='localhost'))
    channel = connection.channel()
     
    channel.exchange_declare(exchange='direct_logs',
                             exchange_type='direct')
     
    result = channel.queue_declare(exclusive=True)
    queue_name = result.method.queue
     
    severities = sys.argv[1:]
    if not severities:
        sys.stderr.write("Usage: %s [info] [warning] [error]
    " % sys.argv[0])
        sys.exit(1)
     
    for severity in severities:
        channel.queue_bind(exchange='direct_logs',
                           queue=queue_name,
                           routing_key=severity)
     
    print(' [*] Waiting for logs. To exit press CTRL+C')
     
    def callback(ch, method, properties, body):
        print(" [x] %r:%r" % (method.routing_key, body))
        ch.basic_ack(delivery_tag=method.delivery_tag)    # ACK确认消息
    channel.basic_consume(callback, 
    queue
    =queue_name,
    no_ack
    =True)
    channel.start_consuming()

     测试:

    sub1:  python rabbitmq_direct_sub.py info error

    sub2:  python rabbitmq_direct_sub.py info warning

    pub:   python rabbitmq_direct_pub.py info hiall    # sub1 sub2收到

          python rabbitmq_direct_pub.py error server down    # sub1收到

              python rabbitmq_direct_pub.py warning disk full    # sub2收到

    3.3.3 topic(更细致的消息过滤,模糊匹配) 

    在topic类型下,可以让队列绑定几个模糊的关键字,之后发送者将数据发送到exchange,exchange将传入”路由值“和 ”关键字“进行匹配,匹配成功,则将数据发送到指定队列。

    • # 表示可以匹配 0 个 或 多个 单词
    • *  表示只能匹配 一个 单词
    发送者路由值              队列中
    old.boy.python          old.*  -- 不匹配
    old.boy.python          old.#  -- 匹配

    Although using the direct exchange improved our system, it still has limitations - it can't do routing based on multiple criteria.

    In our logging system we might want to subscribe to not only logs based on severity, but also based on the source which emitted the log. You might know this concept from the syslog unix tool, which routes logs based on both severity (info/warn/crit...) and facility (auth/cron/kern...).

    That would give us a lot of flexibility - we may want to listen to just critical errors coming from 'cron' but also all logs from 'kern'.

    publisher:

    import pika
    import sys
     
    connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='localhost'))
    channel = connection.channel()
     
    channel.exchange_declare(exchange='topic_logs',
                             exchange_type='topic')
     
    routing_key = sys.argv[1] if len(sys.argv) > 1 else 'anonymous.info'
    message = ' '.join(sys.argv[2:]) or 'Hello World!'
    channel.basic_publish(exchange='topic_logs',
                          routing_key=routing_key,
                          body=message)
    print(" [x] Sent %r:%r" % (routing_key, message))
    connection.close()

    subscriber:

    import pika
    import sys
     
    connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='localhost'))
    channel = connection.channel()
     
    channel.exchange_declare(exchange='topic_logs',
                             exchange_type='topic')
     
    result = channel.queue_declare(exclusive=True)
    queue_name = result.method.queue
     
    binding_keys = sys.argv[1:]
    if not binding_keys:
        sys.stderr.write("Usage: %s [binding_key]...
    " % sys.argv[0])
        sys.exit(1)
     
    for binding_key in binding_keys:
        channel.queue_bind(exchange='topic_logs',
                           queue=queue_name,
                           routing_key=binding_key)
     
    print(' [*] Waiting for logs. To exit press CTRL+C')
     
    def callback(ch, method, properties, body):
        print(" [x] %r:%r" % (method.routing_key, body))
     
    channel.basic_consume(callback,
                          queue=queue_name,
                          no_ack=True)
     
    channel.start_consuming()

    To receive all the logs run:

    python receive_logs_topic.py "#"
    

    To receive all logs from the facility "kern":

    python receive_logs_topic.py "kern.*"
    

    Or if you want to hear only about "critical" logs:

    python receive_logs_topic.py "*.critical"
    

    You can create multiple bindings:

    python receive_logs_topic.py "kern.*" "*.critical"
    

    And to emit a log with a routing key "kern.critical" type:

    python emit_log_topic.py "kern.critical" "A critical kernel error"

    测试:

    sub1:  python rabbitmq_topic_sub.py *.apache *.info

    sub2:  python rabbitmq_topic_sub.py *.mysql.error *.info

    pub:   python rabbitmq_topic_pub.py test.info ping all server  # sub1  sub2 收到ping all server

        python rabbitmq_topic_pub.py test.mysql.error mysql down  # sub2收到 mysql down

        python rabbitmq_topic_pub.py test.apache apache down  # sub1收到 apache down

    3.3.4 Remote procedure call (RPC)

    To illustrate how an RPC service could be used we're going to create a simple client class. It's going to expose a method named call which sends an RPC request and blocks until the answer is received:

    fibonacci_rpc = FibonacciRpcClient()
    result = fibonacci_rpc.call(4)
    print("fib(4) is %r" % result)

    RPC server(被调用端):

    #_*_coding:utf-8_*_
    
    import pika
    import time
    connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='localhost'))
     
    channel = connection.channel()
     
    channel.queue_declare(queue='rpc_queue')
     
    def fib(n):
        if n == 0:
            return 0
        elif n == 1:
            return 1
        else:
            return fib(n-1) + fib(n-2)
     
    def on_request(ch, method, props, body):
        n = int(body)
     
        print(" [.] fib(%s)" % n)
        response = fib(n)
     
        ch.basic_publish(exchange='',
                         routing_key=props.reply_to,
                         properties=pika.BasicProperties(correlation_id = 
                                                             props.correlation_id),
                         body=str(response))
        ch.basic_ack(delivery_tag = method.delivery_tag)
     
    channel.basic_qos(prefetch_count=1)
    channel.basic_consume(on_request, queue='rpc_queue')
     
    print(" [x] Awaiting RPC requests")
    channel.start_consuming()

    RPC client:

    #!/use/bin/env python
    # -*- coding:utf-8 -*-
    
    import pika
    import uuid
    
    class FibonacciRpcClient(object):
        def __init__(self):
            self.connection = pika.BlockingConnection(pika.ConnectionParameters(
                    host='py',
                    credentials=pika.PlainCredentials(username='admin',password='admin')      # Authentication Credentials 认证凭据
                ))
    
            self.channel = self.connection.channel()
    
            result = self.channel.queue_declare(exclusive=True)
            self.callback_queue = result.method.queue
    
            self.channel.basic_consume(self.on_response, no_ack=True,
                                       queue=self.callback_queue)
    
        def on_response(self, ch, method, props, body):
            if self.corr_id == props.correlation_id:
                self.response = body
    
        def call(self, n):
            self.response = None
            self.corr_id = str(uuid.uuid4())    # 生成随机的字符串 UUID('fdb90029-938e-414b-a054-a94cec24da75')
            self.channel.basic_publish(exchange='',
                                       routing_key='rpc_queue',
                                       properties=pika.BasicProperties(
                                             reply_to = self.callback_queue,
                                             correlation_id = self.corr_id,
                                             ),
                                       body=str(n))
            while self.response is None:
                self.connection.process_data_events()
            return int(self.response)
    
    fibonacci_rpc = FibonacciRpcClient()
    
    print(" [x] Requesting fib(30)")
    response = fibonacci_rpc.call(30)
    print(" [.] Got %r" % response)

    RPC,queue绑定到交换机

    server:

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    
    import pika
    import time
    
    # 创建连接
    connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='10.100.16.221',
        port=5672,
        virtual_host='/log',
        credentials=pika.PlainCredentials(username='admin', password='admin')
    ))
    
    channel = connection.channel()
    
    channel.exchange_declare(exchange='rpc_ex',
                             exchange_type='direct')
    
    channel.queue_declare(queue='rpc_queue')
    
    
    def fib(n):
        if n == 0:
            return 0
        elif n == 1:
            return 1
        else:
            return fib(n - 1) + fib(n - 2)
    
    
    def on_request(ch, method, props, body):
        n = int(body)
    
        print(" [.] fib(%s)" % n)
        response = fib(n)
        time.sleep(10)
        ch.basic_publish(exchange='rpc_ex',
                         routing_key=props.reply_to,
                         properties=pika.BasicProperties(correlation_id= props.correlation_id),
                         body=str(response))
        ch.basic_ack(delivery_tag=method.delivery_tag)
    
    
    channel.basic_qos(prefetch_count=1)
    channel.queue_bind(exchange='rpc_ex',
                       queue='rpc_queue',
                       routing_key='rpc_queue')
    channel.basic_consume(consumer_callback=on_request,
                          queue='rpc_queue',
                          no_ack=False
                          )
    
    
    print(" [x] Awaiting RPC requests")
    channel.start_consuming()

    client:

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    import pika
    import uuid
    import sys
    
    
    class FibonacciRpcClient(object):
        def __init__(self):
            self.connection = pika.BlockingConnection(pika.ConnectionParameters(
                host='10.100.16.221',
                port=5672,
                virtual_host='/log',
                credentials=pika.PlainCredentials(username='admin', password='admin')
                ))
    
            self.channel = self.connection.channel()
    
            result = self.channel.queue_declare(exclusive=True)
            self.callback_queue = result.method.queue
    
            self.channel.exchange_declare(exchange='rpc_ex',
                                          exchange_type='direct')
    
            self.channel.queue_bind(exchange='rpc_ex',
                                    queue=self.callback_queue,
                                    routing_key=self.callback_queue)
    
            self.channel.basic_consume(consumer_callback=self.on_response,
                                       queue=self.callback_queue,
                                       no_ack=True
                                       )
    
    
        def on_response(self, ch, method, props, body):
            if self.corr_id == props.correlation_id:
                self.response = body
    
        def call(self, n):
            self.response = None
            self.corr_id = str(uuid.uuid4())    # 生成随机的字符串 UUID('fdb90029-938e-414b-a054-a94cec24da75')
            self.channel.basic_publish(exchange='rpc_ex',
                                       routing_key='rpc_queue',
                                       properties=pika.BasicProperties(
                                             reply_to = self.callback_queue,
                                             correlation_id = self.corr_id,
                                             ),
                                       body=str(n))
            count = 0
            while self.response is None:
                print("loop: %s" % count)
                self.connection.process_data_events()   #以不阻塞的形式去检测有没有新事件,如果没事件,那就什么也不做, 如果有事件,就触发on_response事件
                count += 1
            return int(self.response)
    
    fibonacci_rpc = FibonacciRpcClient()
    
    print(" [x] Requesting fib(%s)" % sys.argv[1])
    import time
    print(time.time())
    response = fibonacci_rpc.call(sys.argv[1])
    print(" [.] Got: %r" % response)
    print(time.time())

    一个queue绑定多个exchange

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    import pika
    
    # 创建连接
    connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='10.100.16.221',
        port=5672,
        virtual_host='/log',
        credentials=pika.PlainCredentials(username='admin', password='admin')
    ))
    
    channel = connection.channel()
    
    channel.exchange_declare(exchange='log',
                             exchange_type='fanout')
    channel.exchange_declare(exchange='logs_direct_test1',
                             exchange_type='direct')
    
    
    ret = channel.queue_declare(exclusive=True)
    queue_name = ret.method.queue
    
    serverities = ['error', 'info', 'warning']
    
    for serverity in serverities:
        channel.queue_bind(exchange='log',
                           queue=queue_name,
                           routing_key=serverity)
        channel.queue_bind(exchange='logs_direct_test1',
                           queue=queue_name,
                           routing_key=serverity)
    
    print('c2_1 [***] 开始接受消息!')
    
    def callback(ch, method, properties, body):
        print('Callback %r:%r' %(method.routing_key, body))
        # ch.basic_ack(delivery_tag=method.delivery_tag)
    
    channel.basic_consume(consumer_callback=callback,
                          queue=queue_name,
                          no_ack=True)
    channel.start_consuming()

    四、SQLAlchemy

    SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用对象关系映射进行数据库操作,简而言之:将对象转换成SQL,然后使用数据API执行SQL并获取执行结果。

    Dialect用于和数据API进行交流,根据配置文件的不同调用不同的数据库API,从而实现对数据库的操作,如: 

    MySQL-Python
        mysql+mysqldb://<user>:<password>@<host>[:<port>]/<dbname>
     
    pymysql
        mysql+pymysql://<username>:<password>@<host>/<dbname>[?<options>]
     
    MySQL-Connector
        mysql+mysqlconnector://<user>:<password>@<host>[:<port>]/<dbname>
     
    cx_Oracle
        oracle+cx_oracle://user:pass@host:port/dbname[?key=value&key=value...]
     
    更多详见:http://docs.sqlalchemy.org/en/latest/dialects/index.html

    步骤一:

    使用 Engine/ConnectionPooling/Dialect 进行数据库操作,Engine使用ConnectionPooling连接数据库,然后再通过Dialect执行SQL语句。

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
     
    from sqlalchemy import create_engine
     
     
    engine = create_engine("mysql+mysqldb://root:123@127.0.0.1:3306/s11", max_overflow=5)
     
    engine.execute(
        "INSERT INTO ts_test (a, b) VALUES ('2', 'v1')"
    )
     
    engine.execute(
         "INSERT INTO ts_test (a, b) VALUES (%s, %s)",
        ((555, "v1"),(666, "v1"),)
    )
    engine.execute(
        "INSERT INTO ts_test (a, b) VALUES (%(id)s, %(name)s)",
        id=999, name="v1"
    )
     
    result = engine.execute('select * from ts_test')
    result.fetchall()

    事务操作:

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    from sqlalchemy import create_engine
    
    
    engine = create_engine("mysql+mysqldb://root:123@127.0.0.1:3306/s11", max_overflow=5)
    
    
    # 事务操作
    with engine.begin() as conn:
        conn.execute("insert into table (x, y, z) values (1, 2, 3)")
        conn.execute("my_special_procedure(5)")
        
        
    conn = engine.connect()
    # 事务操作 
    with conn.begin():
           conn.execute("some statement", {'x':5, 'y':10})

    注:查看数据库连接:show status like 'Threads%';

     步骤二:

    使用 Schema Type/SQL Expression Language/Engine/ConnectionPooling/Dialect 进行数据库操作。Engine使用Schema Type创建一个特定的结构对象,之后通过SQL Expression Language将该对象转换成SQL语句,然后通过 ConnectionPooling 连接数据库,再然后通过 Dialect 执行SQL,并获取结果。

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
     
    from sqlalchemy import create_engine, Table, Column, Integer, String, MetaData, ForeignKey
     
    metadata = MetaData()
     
    user = Table('user', metadata,
        Column('id', Integer, primary_key=True),
        Column('name', String(20)),
    )
     
    color = Table('color', metadata,
        Column('id', Integer, primary_key=True),
        Column('name', String(20)),
    )
    engine = create_engine("mysql+mysqldb://root:123@127.0.0.1:3306/s11", max_overflow=5)
     
    metadata.create_all(engine)
    # metadata.clear()
    # metadata.remove()

    增删改查:

    #!/use/bin/env python
    # -*- coding:utf-8 -*-
    
    from sqlalchemy import create_engine, Table, Column, Integer, String, MetaData, ForeignKey, select
    
    metadata = MetaData()       # 实例化基类
    user2 =Table(
        'user2',
        metadata,       # Table 绑定到mmetadta
        Column('id', Integer, primary_key=True),
        Column('name', String(20)),
    )
    
    user =Table(
        'user',
        metadata,       # Table 绑定到mmetadta
        Column('id', Integer, primary_key=True),
        Column('name', String(20)),
    )
    
    color2 = Table(
        'color2',metadata,
        Column('id', Integer, primary_key=True),
        Column('name', String(20)),
    )
    
    engine = create_engine("mysql+mysqldb://root:py123@192.168.18.219:3306/py")
    # metadata.create_all(engine)     # 执行metadata对象里的sql语句
    conn = engine.connect()
    
    ##  创建   sql语句: insert into 'user' (id, name) values(:id, :name)
    #conn.execute(user2.insert(), {'id':23, 'name':'seven'})
    # conn.execute(user2.insert(), {'name':'n1'})
    # conn.execute(user2.insert(), {'name':'n2'})
    # conn.execute(user2.insert(), {'name':'n3'})
    # conn.execute(user2.insert(), {'name':'n4'})
    # conn.execute(user2.insert(), {'name':'n5'})
    # conn.close()
    
    # conn.execute(color2.insert(), {'name':'c1'})
    # conn.execute(color2.insert(), {'name':'c2'})
    # conn.execute(color2.insert(), {'name':'c3'})
    # conn.execute(color2.insert(), {'name':'c4'})
    # conn.execute(color2.insert(), {'name':'c5'})
    # conn.close()
    
    # sql = user2.insert().values(id=123, name='wu')
    # conn.execute(sql)
    # conn.close()
    
    # sql = user2.delete().where(user2.c.id > 30)       # c --> Column
    # conn.execute(sql)
    
    # sql = user2.update().where(user2.c.id == 23).values(name='jack')
    # conn.execute(sql)
    
    
    # sql = select([user2.c.id])
    # res = conn.execute(sql)
    # print(res.fetchall())
    
    # sql = select([user2,])
    # print(conn.execute(sql).fetchall())
    
    # sql = select([user2.c.name, color2.c.name]).where(user2.c.id == color2.c.id)
    # print(conn.execute(sql).fetchall())     # result==> [('n1', 'c1'), ('n2', 'c2'), ('n3', 'c3'), ('n4', 'c4'), ('n5', 'c5')]
    
    sql = select([user2.c.name]).order_by(user2.c.name)
    print(conn.execute(sql).fetchall())
    conn.close()

    更多内容详见:

        http://www.jianshu.com/p/e6bba189fcbd

        http://docs.sqlalchemy.org/en/latest/core/expression_api.html

    注:SQLAlchemy无法修改表结构,如果需要可以使用SQLAlchemy开发者开源的另外一个软件Alembic来完成。

    步骤三:

    使用 ORM/Schema Type/SQL Expression Language/Engine/ConnectionPooling/Dialect 所有组件对数据进行操作。根据类创建对象,对象转换成SQL,执行SQL。

    #!/use/bin/env python
    # -*- coding:utf-8 -*-
    
    from sqlalchemy import create_engine
    from sqlalchemy.ext.declarative import declarative_base     # declarative声明
    from sqlalchemy import Column, Integer, String
    from sqlalchemy.orm import sessionmaker
    
    Base = declarative_base()       # 生成一个SqlORM基类,这里返回的是一个类
    #engine = create_engine("mysql+mysqldb://root:py123@py/pytest", echo=True)       # echo:是否显示ORM映射及执行过程,默认 False
    engine = create_engine("mysql+mysqldb://root:py123@py/pytest", echo=False)       # echo:是否显示ORM映射及执行过程
    
    class User(Base):
        __tablename__ = 'user'
        id = Column(Integer, primary_key=True)
        name = Column(String(20))
    # 寻找Base的所有子类,按照子类的结构在数据库中生成对应的数据库表信息
    # Base.metadata.create_all(engine)
    
    Session = sessionmaker(bind=engine)
    session = Session()
    
    #
    # u = User(name='sb')
    # session.add(u)
    # session.add_all([
    #     User(name='sb2'),
    #     User(name='sb3'),
    # ])
    # session.commit()
    
    #
    # session.query(User).filter(User.id > 2).delete()
    # session.commit()
    
    #
    # session.query(User).filter(User.id == 2).update({'name':'SSBB'})
    # session.commit()
    
    #
    # res = session.query(User).filter_by(name='sb').all()        # 以list形式返回结果
    # print(res)
    # for i in res:
    #     print(i.id, i.name)
    
    # res = session.query(User).filter(User.name.in_(['sb','bb'])).all()
    # for i in res:
    #     print(i.id, i.name)
    
    res = session.query(User.name.label('name_label')).all()
    print(res, type(res))       # ==> ([('sb',), ('SSBB',)], <type 'list'>)

    # 一个简单完整例子

    #!/use/bin/env python
    # -*- coding:utf-8 -*-
    
    # 一个简单完整例子
    
    from sqlalchemy import create_engine
    from sqlalchemy.ext.declarative import declarative_base     # declarative声明
    from sqlalchemy import Column, Integer, String
    from sqlalchemy.orm import sessionmaker
    
    Base = declarative_base()       # 生成一个SqlORM基类,这里返回的是一个类
    #engine = create_engine("mysql+mysqldb://root:py123@py/pytest", echo=True)       # echo:是否显示ORM映射及执行过程,默认 False
    engine = create_engine("mysql+mysqldb://root:py123@py/pytest", echo=False)       # echo:是否显示ORM映射及执行过程
    
    class Host(Base):
        __tablename__ = 'hosts'
        id = Column(Integer, primary_key=True, autoincrement=True)
        hostname = Column(String(64), unique=True,nullable=False)
        ip_add = Column(String(128), unique=True, nullable=False)
        port = Column(Integer, default=22)
    
    Base.metadata.create_all(engine)  # 创建表结构
    
    if __name__ == '__main__':
        SessionCls = sessionmaker(bind = engine)        # 创建连数据库的会话session class, 这里返回的是一个类
        session = SessionCls()          # 连接的实例
    
        h1 = Host(hostname='localhost',ip_add='127.0.0.1')
        h2 = Host(hostname='Centos6',ip_add='192.168.18.219',port=2200)
        h4 = Host(hostname='Centos6_1',ip_add='192.168.18.210',port=2200)
        h5 = Host(hostname='Centos6_2',ip_add='192.168.18.211',port=2200)
        h6 = Host(hostname='Centos6_3',ip_add='192.168.18.212',port=2200)
        h3 = Host(hostname='Ubuntu',ip_add='192.168.88.130',port=22)
        # session.add(h1)
        #session.add_all([h2, h3])
        #session.add_all([h4, h5, h6])
        #session.rollback()      # 回滚,这里执行commit后数据库不会添加数据,但是自增ID不会回滚回来
        #session.commit()
    
    
        res = session.query(Host).filter(Host.hostname.in_(['Centos6', 'localhost'])).all()
        print(res)
        for i in res:
            print(i.id, i.hostname, i.ip_add, i.port)
    
        res2 = session.query(Host).filter(Host.hostname=='Ubuntu').first()      # all() ==>以列表形式返回结果,first()则不是
        print(res2.id, res2.hostname, res2.ip_add, res2.port)
        #res2.hostname = 'Ubuntu 14'        # 修改数据
        session.commit()

    更多ORM功能参见文档,猛击这里下载PDF

    外键关联

    A one to many relationship places a foreign key on the child table referencing the parent.relationship() is then specified on the parent, as referencing a collection of items represented by the child

    from sqlalchemy import Table, Column, Integer, ForeignKey
    from sqlalchemy.orm import relationship
    from sqlalchemy.ext.declarative import declarative_base
    
    Base = declarative_base()
    
    class Parent(Base):
        __tablename__ = 'parent'
        id = Column(Integer, primary_key=True)
        children = relationship("Child")
     
    class Child(Base):
        __tablename__ = 'child'
        id = Column(Integer, primary_key=True)
        parent_id = Column(Integer, ForeignKey('parent.id'))

    To establish a bidirectional relationship in one-to-many, where the “reverse” side is a many to one, specify an additional relationship() and connect the two using therelationship.back_populates parameter:

    lass Parent(Base):
        __tablename__ = 'parent'
        id = Column(Integer, primary_key=True)
        children = relationship("Child", back_populates="parent")
     
    class Child(Base):
        __tablename__ = 'child'
        id = Column(Integer, primary_key=True)
        parent_id = Column(Integer, ForeignKey('parent.id'))
        parent = relationship("Parent", back_populates="children")

    Child will get a parent attribute with many-to-one semantics.

    Alternatively, the backref option may be used on a single relationship() instead of usingback_populates:

    class Parent(Base):
        __tablename__ = 'parent'
        id = Column(Integer, primary_key=True)
        children = relationship("Child", backref="parent")
    #!/use/bin/env python
    # -*- coding:utf-8 -*-
    
    # 一个简单完整例子
    
    from sqlalchemy import create_engine
    from sqlalchemy.ext.declarative import declarative_base     # declarative声明
    from sqlalchemy import Column, Integer, String, ForeignKey
    from sqlalchemy.orm import sessionmaker, relationship
    
    Base = declarative_base()       # 生成一个SqlORM基类,这里返回的是一个类
    #engine = create_engine("mysql+mysqldb://root:py123@py/pytest", echo=True)       # echo:是否显示ORM映射及执行过程,默认 False
    engine = create_engine("mysql+mysqldb://root:py123@py/pytest", echo=False)       # echo:是否显示ORM映射及执行过程
    
    class Host(Base):
        __tablename__ = 'hosts'
        id = Column(Integer, primary_key=True, autoincrement=True)
        hostname = Column(String(64), unique=True,nullable=False)
        ip_add = Column(String(128), unique=True, nullable=False)
        port = Column(Integer, default=22)
        group_id = Column(Integer, ForeignKey('groups.id'))
        #groups = relationship('Groups')     # 关联 Groups类(效果类似于联表查询),这里是通过反射查找的,如果Groups里也要反射那Groups类里也要做关联
                                            # 有时候为省去要在两个类里都做关联,则只需要在其中一个类里做,如:
                                            # <==>
         # groups = relationship('Groups', back_populates='hosts')  # 效果同上
        #groups = relationship('Groups', backref='hosts')        #backref值为关联字段
    
    
    class Groups(Base):
        __tablename__ = 'groups'
        id = Column(Integer, primary_key=True)
        name = Column(String(64), unique=True, nullable=False)
        #host_id = Column(Integer, ForeignKey('hosts.id'))
        #hosts = relationship('Host')
        # hosts = relationship('Host', back_populates='groups')     # 效果同
    
    #Base.metadata.create_all(engine)  # 创建表结构
    
    if __name__ == '__main__':
        SessionCls = sessionmaker(bind = engine)        # 创建连数据库的会话session class, 这里返回的是一个类
        session = SessionCls()          # 连接的实例
        # g1 = Groups(name='g1')
        # g2 = Groups(name='g2')
        # g3 = Groups(name='g3')
        # g4 = Groups(name='g4')
        #session.add_all([g1,g2,g3,g4,])
    
        g1 = session.query(Groups).filter(Groups.name=='g1').first()
        g2 = session.query(Groups).filter(Groups.name=='g2').first()
        g4 = session.query(Groups).filter(Groups.name=='g4').first()
        h = session.query(Host).filter(Host.hostname=='localhost').first()
        #print(h.groups.id)
        # 通过host name查找所属组的组name
        print('localhost:', h.groups.name)      # Host类中需要 groups relationship 到Groups类
        #通过组name查看有哪些主机属于该组,如查找组名为 g2 下所有主机名
        for i in g2.hosts:          # Groups类中需要 hosts 关联 到Host类
            print(i.hostname)
    
        #h1 = session.query(Host).filter(Host.hostname=='localhost').update({'group_id':g1.id})
        # h2 = session.query(Host).filter(Host.id==2).update({'group_id':g1.id})
        # h3 = session.query(Host).filter(Host.id==3).update({'group_id':g4.id})
        # h4 = session.query(Host).filter(Host.id==4).update({'group_id':g2.id})
        # h5 = session.query(Host).filter(Host.id==5).update({'group_id':g2.id})
        # h6 = session.query(Host).filter(Host.id==6).update({'group_id':g2.id})
    
        # h1 = Host(hostname='localhost',ip_add='127.0.0.1',group_id=g1.id)
        # h2 = Host(hostname='Centos6',ip_add='192.168.18.219',port=2200,group_id=g1.id)
        # h4 = Host(hostname='Centos6_1',ip_add='192.168.18.210',port=2200,group_id=g1.id)
        # h5 = Host(hostname='Centos6_2',ip_add='192.168.18.211',port=2200,group_id=g1.id)
        # h6 = Host(hostname='Centos6_3',ip_add='192.168.18.212',port=2200,group_id=g1.id)
        # h3 = Host(hostname='Ubuntu',ip_add='192.168.88.130',port=22,group_id=g4.id)
    
        # session.add(h1)
        #session.add_all([h2, h3])
        #session.add_all([h1,h2,h3,h4, h5, h6])
        #session.rollback()      # 回滚,这里执行commit后数据库不会添加数据,但是自增ID不会回滚回来
        session.commit()
    
    
        # res = session.query(Host).filter(Host.hostname.in_(['Centos6', 'localhost'])).all()
        # print(res)
        # for i in res:
        #     print(i.id, i.hostname, i.ip_add, i.port)
        #
        # res2 = session.query(Host).filter(Host.hostname=='Ubuntu').first()      # all() ==>以列表形式返回结果,first()则不是
        # print(res2.id, res2.hostname, res2.ip_add, res2.port)
        # #res2.hostname = 'Ubuntu 14'        # 修改数据
        # session.commit()
    #!/use/bin/env python
    # -*- coding:utf-8 -*-
    
    '''表结构
    hosts主机名                groups组表            Host2Group主机对应组表
    id  hostname                id  name            id  host_id group_id
    1   h1                      1   g1              1   1       1
    2   h2                      2   g2              2   1       2
    3   h3                      3   g3              3   2       1
    4   h4                                          4   2       3
    mysql> select * from hosts;
    +----+----------+---------------+-------+
    | id | hostname | ip_add        | port  |
    +----+----------+---------------+-------+
    |  1 | h1       | 192.168.18.10 |    22 |
    |  2 | Centos7  | 192.168.18.11 | 10000 |
    |  3 | ubuntu   | 192.168.18.12 | 10000 |
    |  4 | CentOS6  | 192.168.18.13 | 10000 |
    +----+----------+---------------+-------+
    
    mysql> select * from groups;
    +----+------+
    | id | name |
    +----+------+
    |  1 | g1   |
    |  2 | g2   |
    |  3 | g3   |
    |  4 | g4   |
    +----+------+
    
    mysql> select * from host2group;
    +----+---------+----------+
    | id | host_id | group_id |
    +----+---------+----------+
    |  1 |       1 |        1 |
    |  2 |       1 |        2 |
    |  3 |       1 |        3 |
    |  5 |       2 |        2 |
    |  6 |       2 |        3 |
    |  7 |       3 |        3 |
    +----+---------+----------+
    
    '''
    
    from sqlalchemy import create_engine,and_,or_, func, Table
    from sqlalchemy.ext.declarative import declarative_base     # declarative声明
    from sqlalchemy import Column, Integer, String, ForeignKey
    from sqlalchemy.orm import sessionmaker, relationship
    
    Base = declarative_base()       # 生成一个SqlORM基类,这里返回的是一个类
    #engine = create_engine("mysql+mysqldb://root:admin@py/pytest", echo=True)       # echo:是否显示ORM映射及执行过程,默认 False
    #engine = create_engine("mysql+mysqldb://root:admin@py/pytest", echo=False)       # echo:是否显示ORM映射及执行过程
    engine = create_engine("mysql+pymysql://root:admin@py/pytest", echo=False)       # echo:是否显示ORM映射及执行过程
    
    Host2Group = Table(         # 这里返回的是一个实例
        'host2group',Base.metadata,
        Column('id',Integer,primary_key=True,autoincrement=True),
        Column('host_id',ForeignKey('hosts.id'),primary_key=True),
        Column('group_id',ForeignKey('groups.id'),primary_key=True),
    )
    # 这里 id host_id group_id三个列组合成primary_key,意思是id host_id group_id组成的要非空且唯一
    
    '''
    sql:
    CREATE TABLE host2group (
        id INTEGER NOT NULL AUTO_INCREMENT,
        host_id INTEGER NOT NULL,
        group_id INTEGER NOT NULL,
        PRIMARY KEY (id, host_id, group_id),
        FOREIGN KEY(host_id) REFERENCES hosts (id),
        FOREIGN KEY(group_id) REFERENCES groups (id)
    )
    
    '''
    
    
    class Host(Base):
        __tablename__ = 'hosts'
        id = Column(Integer, primary_key=True, autoincrement=True)
        hostname = Column(String(64), unique=True,nullable=False)
        ip_add = Column(String(128), unique=True, nullable=False)
        port = Column(Integer, default=22)
        #group_id = Column(Integer, ForeignKey('groups.id'))
        groups = relationship('Groups',
                              backref='hosts',  #backref值为关联字段
                              secondary=Host2Group)            # secondary:第三方关联表在哪
    
        def __repr__(self):
            """
            修改返回对象数据格式,默认打印的是对象的内存地址
            :return:
            """
            return('id:%s hostname:%s ip_add:%s port:%s' %(self.id, self.hostname, self.ip_add, self.port))
    
    
    class Groups(Base):
        __tablename__ = 'groups'
        id = Column(Integer, primary_key=True)
        name = Column(String(64), unique=True, nullable=False)
    
        def __repr__(self):
            """
            修改返回对象数据格式,默认打印的是对象的内存地址
            :return:
            """
            return('id:%s	name:%s' %(self.id, self.name))
    
    
    ## 两个 primary_key 表示两个列合并成一个主键,这两个列组合起来是非空用且唯一的
    
    #Base.metadata.create_all(engine)  # 创建表结构
    
    if __name__ == '__main__':
        SessionCls = sessionmaker(bind = engine)        # 创建连数据库的会话session class, 这里返回的是一个类
        session = SessionCls()          # 连接的实例
        '''
        g1= Groups(name='g1')
        g2= Groups(name='g2')
        g3= Groups(name='g3')
        g4= Groups(name='g4')
    
        sql = session.add_all([g1, g2, g3, g4,])
        '''
        '''
        h1 = Host(hostname='h1',ip_add='192.168.18.10')
        h2 = Host(hostname='Centos7',ip_add='192.168.18.11',port=10000)
        h3 = Host(hostname='ubuntu',ip_add='192.168.18.12',port=10000)
        h4 = Host(hostname='CentOS6',ip_add='192.168.18.13',port=10000)
    
        sql = session.add_all([h1, h2, h3, h4,])
        '''
        g = session.query(Groups).filter().all()
        g1 = session.query(Groups).filter(Groups.id==1).first()
        h1 = session.query(Host).filter(Host.hostname=='h1').first()
        h2 = session.query(Host).filter(Host.hostname=='Centos7').first()
        h3 = session.query(Host).filter(Host.hostname=='ubuntu').first()
        #h1.groups = g
        #h1.groups.pop()
        #h2.groups = g[1:-1]
        #h3.groups = g[2:-1]
        # print(g)
        # for i in g:
        #     print(i)
        print('h2 in groups:',h2.groups)            # h2 in groups: [id:2    name:g2, id:3    name:g3]
        print('g1 include hosts:',g1.hosts)     # g1 include hosts: [id:1 hostname:h1 ip_add:192.168.18.10 port:22]
    
        session.commit()

    附,原生sql join查询

    几个Join的区别 http://stackoverflow.com/questions/38549/difference-between-inner-and-outer-joins 

    • INNER JOIN: Returns all rows when there is at least one match in BOTH tables
    • LEFT JOIN: Return all rows from the left table, and the matched rows from the right table
    • RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table
    select host.id,hostname,ip_addr,port,host_group.name from host right join host_group on host.id = host_group.host_id
    Examples
    
    Suppose you have two tables, with a single column each, and data as follows:
    
    A    B
    -    -
    1    3
    2    4
    3    5
    4    6
    Note that (1,2) are unique to A, (3,4) are common, and (5,6) are unique to B.
    
    Inner join
    
    An inner join using either of the equivalent queries gives the intersection of the two tables, i.e. the two rows they have in common.
    
    select * from a INNER JOIN b on a.a = b.b;
    select a.*,b.*  from a,b where a.a = b.b;
    
    a | b
    --+--
    3 | 3
    4 | 4
    Left outer join
    
    A left outer join will give all rows in A, plus any common rows in B.
    
    select * from a LEFT OUTER JOIN b on a.a = b.b;
    select a.*,b.*  from a,b where a.a = b.b(+);
    
    a |  b
    --+-----
    1 | null
    2 | null
    3 |    3
    4 |    4
    Right outer join
    
    A right outer join will give all rows in B, plus any common rows in A.
    
    select * from a RIGHT OUTER JOIN b on a.a = b.b;
    select a.*,b.*  from a,b where a.a(+) = b.b;
    
    a    |  b
    -----+----
    3    |  3
    4    |  4
    null |  5
    null |  6
    Full outer join
    
    A full outer join will give you the union of A and B, i.e. all the rows in A and all the rows in B. If something in A doesn't have a corresponding datum in B, then the B portion is null, and vice versa.
    
    select * from a FULL OUTER JOIN b on a.a = b.b;
    
     a   |  b
    -----+-----
       1 | null
       2 | null
       3 |    3
       4 |    4
    null |    6
    null |    5

    in SQLAchemy

    session.query(Host).join(Host.host_groups).filter(HostGroup.name=='t1').group_by("Host").all()

    group by 查询

    select name,count(host.id) as NumberOfHosts from host right join host_group on host.id= host_group.host_id group by name;

    in SQLAchemy

    from sqlalchemy import func
    session.query(HostGroup, func.count(HostGroup.name )).group_by(HostGroup.name).all()
     
    #another example
    session.query(func.count(User.name), User.name).group_by(User.name).all() SELECT count(users.name) AS count_1, users.name AS users_name
    FROM users GROUP BY users.name
     
  • 相关阅读:
    ubuntu 编译安装 bochs2.4.6
    Android和QtExtended在设计方法上的共通之处
    MySQL C API programming tutorial
    linux书籍
    登陆、非登陆shell,交互、非交互shell,以及它们的startup文件
    CSS中的margin属性
    如何比较两个内容相似的Word文档
    汇编语言基础之三 变量的访问和流程控制指令
    [转]80X86 汇编指令符号大全(A~Z)
    找不到VS2008中三个新的样式窗口
  • 原文地址:https://www.cnblogs.com/linkenpark/p/5370330.html
Copyright © 2011-2022 走看看