Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Datetime 如何使用mapreduce和pyspark查找某一年中某一天的频率_Datetime_Hadoop_Mapreduce_Pyspark - Fatal编程技术网

Datetime 如何使用mapreduce和pyspark查找某一年中某一天的频率

Datetime 如何使用mapreduce和pyspark查找某一年中某一天的频率,datetime,hadoop,mapreduce,pyspark,Datetime,Hadoop,Mapreduce,Pyspark,我有一个文本文件(61Gb),每行包含一个表示日期的字符串,例如Thu Dec 16 18:53:32+0000 2010 在单个内核上迭代文件会花费太长时间,因此我想使用Pyspark和Mapreduce技术快速查找某一年中某一天的行频率 我认为这是一个良好的开端: import dateutil.parser text_file = sc.textFile('dates.txt') date_freqs = text_file.map(lambda line: dateutil.parser

我有一个文本文件(61Gb),每行包含一个表示日期的字符串,例如Thu Dec 16 18:53:32+0000 2010

在单个内核上迭代文件会花费太长时间,因此我想使用Pyspark和Mapreduce技术快速查找某一年中某一天的行频率

我认为这是一个良好的开端:

import dateutil.parser
text_file = sc.textFile('dates.txt')
date_freqs = text_file.map(lambda line: dateutil.parser.parse(line)) \
        .map(lambda date: date + 1) \
        .reduceByKey(lambda a, b: a + b)
import dateutil.parser
text_file = sc.textFile('dates.txt')
date_freqs = text_file.map(lambda line: dateutil.parser.parse(line))
    .keyBy((_.year, _.month, _.day)) // somehow get the year, month, day to key by
    .countByKey()
不幸的是,我无法理解如何在某一年进行筛选并按键进行缩减。关键是今天

示例输出:

十二月十六日星期四26543

12月17日星期四345
等等。

类似这样的事情可能是一个好的开始:

import dateutil.parser
text_file = sc.textFile('dates.txt')
date_freqs = text_file.map(lambda line: dateutil.parser.parse(line)) \
        .map(lambda date: date + 1) \
        .reduceByKey(lambda a, b: a + b)
import dateutil.parser
text_file = sc.textFile('dates.txt')
date_freqs = text_file.map(lambda line: dateutil.parser.parse(line))
    .keyBy((_.year, _.month, _.day)) // somehow get the year, month, day to key by
    .countByKey()

类似这样的事情可能是一个好的开始:

import dateutil.parser
text_file = sc.textFile('dates.txt')
date_freqs = text_file.map(lambda line: dateutil.parser.parse(line)) \
        .map(lambda date: date + 1) \
        .reduceByKey(lambda a, b: a + b)
import dateutil.parser
text_file = sc.textFile('dates.txt')
date_freqs = text_file.map(lambda line: dateutil.parser.parse(line))
    .keyBy((_.year, _.month, _.day)) // somehow get the year, month, day to key by
    .countByKey()
如中所述,
dateutil.parser.parse
返回具有
属性的日期:

>>> dt = dateutil.parser.parse('Thu Dec 16 18:53:32 +0000 2010')
>>> dt.year
2010
>>> dt.month
12
>>> dt.day
16
从这个RDD开始:

>>> rdd = sc.parallelize([
...     'Thu Oct 21 5:12:38 +0000 2010',
...     'Thu Oct 21 4:12:38 +0000 2010',
...     'Wed Sep 22 15:46:40 +0000 2010',
...     'Sun Sep 4 22:28:48 +0000 2011',
...     'Sun Sep 4 21:28:48 +0000 2011'])
以下是如何获得所有年-月-日组合的计数:

>>> from operator import attrgetter
>>> counts = rdd.map(dateutil.parser.parse).map(
...     attrgetter('year', 'month', 'day')).countByValue()
>>> counts
defaultdict(<type 'int'>, {(2010, 9, 22): 1, (2010, 10, 21): 2, (2011, 9, 4): 2})
如果您只想对某一年进行计数,可以在进行计数之前过滤RDD:

>>> counts = rdd.map(dateutil.parser.parse).map(
...    attrgetter('year', 'month', 'day')).filter(
...    lambda (y, m, d): y == 2010).countByValue()
>>> counts
defaultdict(<type 'int'>, {(2010, 9, 22): 1, (2010, 10, 21): 2})
counts=rdd.map(dateutil.parser.parse.map)( …attrgetter('年'、'月'、'日')。筛选器( …lambda(y,m,d):y==2010.countByValue() >>>计数 defaultdict(,{(2010,9,22):1,(2010,10,21):2}) 如中所述,
dateutil.parser.parse
返回具有
属性的日期:

>>> dt = dateutil.parser.parse('Thu Dec 16 18:53:32 +0000 2010')
>>> dt.year
2010
>>> dt.month
12
>>> dt.day
16
从这个RDD开始:

>>> rdd = sc.parallelize([
...     'Thu Oct 21 5:12:38 +0000 2010',
...     'Thu Oct 21 4:12:38 +0000 2010',
...     'Wed Sep 22 15:46:40 +0000 2010',
...     'Sun Sep 4 22:28:48 +0000 2011',
...     'Sun Sep 4 21:28:48 +0000 2011'])
以下是如何获得所有年-月-日组合的计数:

>>> from operator import attrgetter
>>> counts = rdd.map(dateutil.parser.parse).map(
...     attrgetter('year', 'month', 'day')).countByValue()
>>> counts
defaultdict(<type 'int'>, {(2010, 9, 22): 1, (2010, 10, 21): 2, (2011, 9, 4): 2})
如果您只想对某一年进行计数,可以在进行计数之前过滤RDD:

>>> counts = rdd.map(dateutil.parser.parse).map(
...    attrgetter('year', 'month', 'day')).filter(
...    lambda (y, m, d): y == 2010).countByValue()
>>> counts
defaultdict(<type 'int'>, {(2010, 9, 22): 1, (2010, 10, 21): 2})
counts=rdd.map(dateutil.parser.parse.map)( …attrgetter('年'、'月'、'日')。筛选器( …lambda(y,m,d):y==2010.countByValue() >>>计数 defaultdict(,{(2010,9,22):1,(2010,10,21):2})
我应该补充一点,dateutil在Python中不是标准的。如果集群上没有正确的sudo,这可能会造成问题。作为解决方案,我建议使用datetime:

import datetime
def parse_line(d):
    f = "%a %b %d %X %Y"
    date_list = d.split()
    date = date_list[:4]
    date.append(date_list[5])
    date = ' '.join(date)
    return datetime.datetime.strptime(date, f)

counts = rdd.map(parse_line)\
    .map(attrgetter('year', 'month', 'day'))\
    .filter(lambda (y, m, d): y == 2015)\
    .countByValue()

我对使用拼花地板、行/列等更好的解决方案感兴趣。

我应该补充一点,dateutil在Python中不是标准的。如果集群上没有正确的sudo,这可能会造成问题。作为解决方案,我建议使用datetime:

import datetime
def parse_line(d):
    f = "%a %b %d %X %Y"
    date_list = d.split()
    date = date_list[:4]
    date.append(date_list[5])
    date = ' '.join(date)
    return datetime.datetime.strptime(date, f)

counts = rdd.map(parse_line)\
    .map(attrgetter('year', 'month', 'day'))\
    .filter(lambda (y, m, d): y == 2015)\
    .countByValue()
我对使用拼花地板、行/列等更好的解决方案感兴趣