流式数据处理

1、 直接登陆服务器:ssh 2014210***@thumedia.org -p 6349

创建streaming.py:   touch streaming.py,并且如下编辑:

<span style="font-size:14px;">#! /usr/bin/python
import logging
import math
import time
pg2count={}
t=1
while 1:
    fp=open('/tmp/hw3.log','r')
    for line in fp:
        line = line.strip()
        times, page, count = line.split()[0],line.split()[1],line.split()[2]
        if count.isdigit() & page.startswith('Page-'):
            try:
                pg2count[page] = [pg2count[page][0] + int(count),t]             
            except:
                pg2count[page] = [int(count),t]
    fp.close()
    a=sorted(pg2count.items(), key=lambda page:page[1][0], reverse = True)
    print '%s%s%s' % ('the page rank at current time ',times,' is:')
    for i in range(0,10):
        print '%s\t%d' % (a[i][0],a[i][1][0])
    logger = logging.getLogger()
    #set loghandler 
    file = logging.FileHandler("output.log")
    logger.addHandler(file)
    #set formater   
    formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s")
    file.setFormatter(formatter)
    #set log level 
    logger.setLevel(logging.NOTSET)
    logger.info('%s%s%s' % ('the page rank at current time ',times,' is:'))
    for i in range(0,10):
        logger.info('%s\t%d' % (a[i][0],a[i][1][0]))
        time.sleep(60)</span>

2、 写好代码之后测试运行:python streaming.py输出如下:

nohup: ignoring input and appending output to `nohup.out',则表示后台运行成功,输出显示会保存到nohup.out中,

clip_image002

也可以查看output.log文件里的输出:

clip_image004

最后我们让它在后台一直执行:nohup python streaming.py &输出:

[1] 8994

2014210***@cluster-3-1:~$ nohup: ignoring input and appending output to `nohup.out'

一天之后,我们再次查看结果:

clip_image006

可以看到,累计的结果已经和第一次不太一样

3、 杀掉进程:ps -ef|grep 1020得到如下输出:

2014210***@cluster-3-1:~$ ps -ef|grep 1020

1020      7512  7471  0 Jan10 ?        00:00:00 sshd: 2014210***@pts/30

1020      7513  7512  0 Jan10 pts/30   00:00:00 -bash

1020      7574  7508  0 20:55 ?        00:00:00 sshd: 2014210***@pts/52

1020      7575  7574  0 20:55 pts/52   00:00:00 -bash

1020      8282  7575  0 21:04 pts/52   00:00:00 ps -ef

1020      8283  7575  0 21:04 pts/52   00:00:00 grep --color=auto 1020

1020      8994     1  0 13:20 ?        00:01:46 python streaming.py

1020     12260 12232  0 Jan10 ?        00:00:00 sshd: 2014210***@pts/35

1020     12261 12260  0 Jan10 pts/35   00:00:01 –bash

输入kill 8994

2014210***@cluster-3-1:~$ kill 8994

2014210***@cluster-3-1:~$ ps -ef|grep 1020

1020      7512  7471  0 Jan10 ?        00:00:00 sshd: 2014210***@pts/30

1020      7513  7512  0 Jan10 pts/30   00:00:00 -bash

1020      7574  7508  0 20:55 ?        00:00:00 sshd: 2014210***@pts/52

1020      7575  7574  0 20:55 pts/52   00:00:00 -bash

1020      8335  7575  0 21:05 pts/52   00:00:00 ps -ef

1020      8336  7575  0 21:05 pts/52   00:00:00 grep --color=auto 1020

1020     12260 12232  0 Jan10 ?        00:00:00 sshd: 2014210***@pts/35

1020     12261 12260  0 Jan10 pts/35   00:00:01 –bash

至此,streaming.py运行结束。

 

Question

How can your design scale when the streaming is large and the calculation is complicated?

答:首先确定每个程序周期需要的时间,然后确定这段时间内的流数据能够保存在一块足够大的缓存区域,等到下个程序周期处理前一个缓存的流数据即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Python中,单机流式数据处理通常指的是用于处理大量、快速生成的数据,而不需要一次性加载到内存中的框架。这类框架通常支持数据的分块读取和处理,以适应内存限制,并提供可扩展性和高性能。以下是几个流行的Python单机流式数据处理框架: 1. **Apache Beam(原Google Dataflow)**:这是一个开源的统一编程模型,支持多种语言(包括Python),可以在单机或分布式环境中处理数据流。它使用PCollection(类似DataFrame的概念)来表示数据,并提供了Transforms API进行处理。 2. **Dask**:Dask是一个并行计算库,特别适合大数据处理。它基于Python,可以处理比内存大的数据集,通过将数据划分为小块并在本地进程或分布式环境中执行任务来实现流式计算。 3. **pandas DataFrame API**:虽然pandas主要用于内存中的数据操作,但其本身也支持分块读取大型CSV文件,通过`read_csvchunked`或其他迭代器方法,实现流式数据处理。 4. **Fugue**:Fugue是一个通用的编排引擎,可以在多种数据源(如Hadoop、Spark、Pandas等)之间无缝切换,支持数据流式处理和转换。 5. **Pandarallel**:这是pandas的一个扩展,它可以在每个pandas DataFrame上并行运行多个函数,通过多进程或多线程实现流式计算。 相关问题--: 1. Dask如何处理大数据流? 2. Apache Beam的主要特点是什么? 3. Fugue支持哪些常见的数据处理操作? 4. Pandarallel如何利用多核资源提高处理性能?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值