import datetime
import sys
import oss2
from itertools import islice
import pandas as pd
import re
import json
from pandas.tseries.offsets import Day
from multiprocessing import Process, JoinableQueue, cpu_count, Manager
import time def mkbuck(bk):
auth = oss2.Auth(username, password)
bucket = oss2.Bucket(auth, address, bk)
return bucket #获取前天最后一小时的paths
def getbflastpt(bucket, bfyespattern):
bfpamax = []
for bf in islice(oss2.ObjectIterator(bucket, prefix=bfyespattern), sys.maxsize):
c = bf.key
if c[-1:] != '/':
bfpamax.append(int(c.split('/')[4]))
last = pd.Series(bfpamax).unique().max()
if last < 10:
bflastpt = bfyespattern + '/0' + str(last)
else:
bflastpt = bfyespattern + '/' + str(last)
return bflastpt #获取当天第一个小时的paths
def getnowfirstpt(bucket, nowpattern):
bfpamin = []
for bf in islice(oss2.ObjectIterator(bucket, prefix=nowpattern), sys.maxsize):
c = bf.key
if c[-1:] != '/':
bfpamin.append(int(c.split('/')[4]))
first = pd.Series(bfpamin).unique().min()
if first < 10:
nowfirstpt = nowpattern + '/0' + str(first)
else:
nowfirstpt = nowpattern + '/' + str(first)
return nowfirstpt #获取所有的昨日paths,并合并得到完全的paths和数量
def getfullnum(bk, bfyespattern, nowpattern, yespattern):
lists = []
bucket = mkbuck(bk)
bfyespattern = getbflastpt(bucket, bfyespattern)
nowpattern = getnowfirstpt(bucket, nowpattern)
timelist = (s for s in (bfyespattern, yespattern, nowpattern))
for pter in timelist:
for bf in islice(oss2.ObjectIterator(bucket, prefix=pter), sys.maxsize):
c = bf.key
lists.append(c)
return lists, len(lists) #以下为进程间通信,即生产者、消费者模型
def getfull(bk, bfyespattern, nowpattern, yespattern, q):
lists, num = getfullnum(bk, bfyespattern, nowpattern, yespattern)
for c in lists:
q.put(c)
q.join() def consumer(bk, q, d):
bucket = mkbuck(bk)
repattern2 = re.compile('{.*"adadji",.*}')
while True:
js = []
ress = q.get()
if ress[-1:] != '/':
remote_data = bucket.get_object(ress).read().decode('utf-8')
aa = (d for d in repattern2.findall(remote_data))
for a in aa:
temdic = json.loads(a)
if (starttime <= temdic['created_at']) and (temdic['created_at'] <= endtime):
js.append(temdic)
df = pd.DataFrame(js, columns=['dd','cc'])
d[ress] = df##d为通过主进程Manager共享变量将数据取出
# print(ress)
q.task_done()# 向q.join()发送一次信号,证明一个数据已经被取走了 if __name__ == '__main__':
s1 = time.time()
now_time = datetime.datetime.now() # 获取当前时间
bfyes_time = (now_time - 2 * Day()).strftime('%Y/%m/%d')
yes_time = (now_time - 1 * Day()).strftime('%Y/%m/%d')
yesdate = (now_time - 1 * Day()).strftime('%Y-%m-%d')
yesdate1 = (now_time - 1 * Day()).strftime('%Y%m%d')
endtime = (now_time - 1 * Day()).strftime('%Y-%m-%d 23:59:59')
starttime = (now_time - 1 * Day()).strftime('%Y-%m-%d 00:00:00')
nowdate = now_time.strftime('%Y/%m/%d') bk = 'xxx'
bfyespattern = '%s/%s' % (bk, bfyes_time)
yespattern = '%s/%s' % (bk, yes_time)
nowpattern = '%s/%s' % (bk, nowdate) q = JoinableQueue(cpu_count())
m = Manager()
d = m.dict() ##创建进程间的共享内存字典,方便各个进程处理好的数据
p1 = Process(target=getfull, args=('xx', bfyespattern, nowpattern, yespattern, q))
#####生成consumer多进程
cc = []
for c in range(cpu_count() - 1):
c1 = Process(target=consumer, args=('xx', q, d))
cc.append(c1) p_l = [p1]
for c in cc:
c.daemon = True
p_l.append(c) for p in p_l:
p.start()
p1.join()
d = d.values()
df1 = pd.concat(d, ignore_index=True)
df1.sort_values('created_at', inplace=True)
print(time.time() - s1)
print('=' * 20)
print(df1)

  说明:需求为获取昨日的数据即可,因oss实时数据存储可能存在提前或延迟情况,因此读取前天的最后一小时,昨日全部,当天最开始一小时数据,读者可根据自身情况进行修改

以多进程读取oss符合条件的数据为例,综合使用多进程间的通信、获取多进程的数据的更多相关文章

  1. Pandas之:Pandas高级教程以铁达尼号真实数据为例

    Pandas之:Pandas高级教程以铁达尼号真实数据为例 目录 简介 读写文件 DF的选择 选择列数据 选择行数据 同时选择行和列 使用plots作图 使用现有的列创建新的列 进行统计 DF重组 简 ...

  2. zTree实现获取一级节点数据

    zTree实现获取一级节点数据 1.实现源码 <!DOCTYPE html> <html> <head> <title>zTree实现基本树</t ...

  3. request.getParameter()获取不到数据

    HTML中的form表单有一个关键属性 Content-Type=application/x-www-form-urlencoded 或multipart/form-data. 1. Content- ...

  4. python从数据库获取全量数据的方法

    python从数据库获取全量数据的方法 学习了:https://blog.csdn.net/lom9357bye/article/details/79503658 原文膜拜: import psyco ...

  5. request.getParameter()获取不到数据的问题

    最近做项目时,发现手机客户端通过http协议post方式上传数据到服务端,在服务器端通过request.getInputStream()能获取到相应的数据,但用request.getParameter ...

  6. Oracle根据符合条件的数据循环批量更新

    --批量对符合条件的表记录进行更新 --aa代表查询出的符合条件数据的别名 --aa后的表示需要符合的条件 --loop后开始写更新操作 begin for aa in (select a.objec ...

  7. 查询同一张表符合条件的某些数据的id拼接成一个字段返回

    同一张表存在类似多级菜单的上下级关系的数据,查询出符合条件的某些数据的id拼接成一个字段返回: SELECT CONCAT(a.pid, ',', b.subid) AS studentIDS FRO ...

  8. 深度学习实践-物体检测-faster-RCNN(原理和部分代码说明) 1.tf.image.resize_and_crop(根据比例取出特征层,进行维度变化) 2.tf.slice(数据切片) 3.x.argsort()(对数据进行排列,返回索引值) 4.np.empty(生成空矩阵) 5.np.meshgrid(生成二维数据) 6.np.where(符合条件的索引) 7.tf.gather取值

    1. tf.image.resize_and_crop(net, bbox, 256, [14, 14], name)  # 根据bbox的y1,x1,y2,x2获得net中的位置,将其转换为14*1 ...

  9. Shell 筛选符合条件的 ELF 文件

    0 运行环境 本机系统:Windows 10 虚拟机软件:Oracle VM VirtualBox 6 虚拟机系统:Ubuntu 18 1 引言 - 编译过程 我们知道在 CPU 上执行的是低级别的机 ...

随机推荐

  1. (转)HTML&CSS——background: url() no-repeat 0 -64px;CSS中背景图片定位方法

    http://blog.csdn.net/oscar92420aaa/article/details/51304067 CSS中背景图片的定位,困扰我很久了.今天总算搞懂了,一定要记下来. 在CSS中 ...

  2. 使用cydia substrate 来进行android native hook

      cydia不仅可以hook java代码,同样可以hook native代码,下面举一个例子来进行android native hook 我是在网上找到的supermathhook这个项目,在他基 ...

  3. Django会话,用户和注册之用户认证

    通过session,我们可以在多次浏览器请求中保持数据, 接下来的部分就是用session来处理用户登录了. 当然,不能仅凭用户的一面之词,我们就相信,所以我们需要认证. 当然了,Django 也提供 ...

  4. Linux系统磁盘

    所有有系统都一样,都是一种软件被安装于某个硬件之上,这个硬件无外非是一种存储设备,通常操作系统都是安装在磁盘中,所以Linux系统也是一样,都是安装在磁盘中,但是它与Windows系统不一样,因为Li ...

  5. Chrome插件-Postman Interceptor

    postman有一个chrome插件 Postman Interceptor,可以让postman中发送请求的时候使用这个网站的浏览器cookie Postman Interceptor,可以让pos ...

  6. Python项目完成

    今天,来对之前自学python做的一个小任务进行复习和总结,首先回顾之前任务的每一个步骤,按照自己个人的思路,一布一步往下做 需求:根据主网站,爬取出大连市各年地区生产总值,观察2010至2015年的 ...

  7. Burp Suite初探

    Burp Suite 是用于攻击web 应用程序的集成平台.它包含了许多工具,并为这些工具设计了许多接口,以促进加快攻击应用程序的过程. 一.安装部署 需要配置java环境,首先安装java,然后配置 ...

  8. 最长回文串:LeetCode:Longest Palindromic Substring

    class Solution { public: string longestPalindrome(string s) { int length=s.length(); ; ; ][]={false} ...

  9. kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  10. mac os x忘记了root密码怎么办,忘记登录密码(普通帐号密码)也是一样的

    有时候我们给mac设置了root密码,一段时间不用,却忘记了密码,怎么办?下面的办法帮你解决: 步骤1:先关闭你的mac系统 步骤2:开机,按住Command和s两个按键不松手直到出现下面的界面: 步 ...