-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathmap_cos_prefix.py
45 lines (33 loc) · 1.34 KB
/
map_cos_prefix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
Simple Lithops example using the map method which runs a wordcount
over all the objects inside the 'bucketname' COS bucket independently.
This example processes some objects from COS. Be sure you have
a bucket with some data objects in your COS account. Then change
the value of the 'bucketname' variable to point to your bucket.
As in this case you are processing objects from COS, the map() method
will first discover objects inside the buckets. Then, it will launch
one map function for each object. So In this case you will get one
result from the each object in the bucket.
In the reduce function there will be always one parameter
from where you can access to the partial results.
"""
import lithops
# Bucket with prefix
data_location = 'cos://lithops-sample-data/test/' # Change-me
def my_map_function(obj):
print(f'Bucket: {obj.bucket}')
print(f'Key: {obj.key}')
print(f'Partition num: {obj.part}')
counter = {}
data = obj.data_stream.read()
for line in data.splitlines():
for word in line.decode('utf-8').split():
if word not in counter:
counter[word] = 1
else:
counter[word] += 1
return counter
if __name__ == "__main__":
fexec = lithops.FunctionExecutor(log_level='DEBUG')
fexec.map(my_map_function, data_location)
print(fexec.get_result())