0

Spark/Hadoop 入力言語である Pyspark: データセットで "SJC" などのキーワードを検索し、キーワード "SJC" が見つかった行に対応する 2 番目の列からテキストを返したいと考えています。

たとえば、次のデータセットは次のようになります。

[年] [遅延] [目的地] [フライト番号]

|1987| |-5| |SJC| |500|

|1987| |-5| |SJC| |250|

|1987| |07| |SFO| |700|

|1987| |09| |SJC| |350|

|1987| |-5| |SJC| |650|

「SJC」をクエリして、[Delay] 値をリストまたは文字列として返すことができるようにしたいと考えています。

私はここまで来ましたが、運がありません:

import sys
from pyspark import SparkContext

logFile = "hdfs://<ec2 host address>:9000/<dataset folder (on ec2)>"
sc = SparkContext("local", "simple app")
logData = sc.textFile(logFile).cache()
numSJC = logData.filter(lambda line: 'SJC' in line).first()

print "Lines with SJC:" + ''.join(numSJC)

助けてくれてありがとう!

4

1 に答える 1

0

あなたはほとんどそれを自分でやった

パイプ区切りのファイル `/tmp/demo.txt' があると想像してください:

Year|Delay|Dest|Flight #
1987|-5|SJC|500
1987|-5|SJC|250
1987|07|SFO|700
1987|09|SJC|350
1987|-5|SJC|650

PySpark では、次のようにする必要があります。

# First, point Spark to the file
log = sc.textFile('file:///tmp/demo.txt')
# Second, replace each line with array of the values, thus string 
# '1987|-5|SJC|500' is replaced with ['1987', '-5', 'SJC', '500']
log = log.map(lambda line: line.split('|'))
# Now filter leaving only the lists with 3rd element equal to 'SJC'
log = log.filter(lambda x: x[2]=='SJC')
# Now leave only the second column, 'Delay'
log = log.map(lambda x: x[1])
# And here's the result
log.collect()
于 2015-01-13T15:39:52.467 に答える