You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{MD5}from'crypto-js';import{Bucket,Function}from"@plutolang/pluto";constinputBucket=newBucket("articles",{exists: true// Don't try to create this bucket when deploying this application.});constintermediateBucket=newBucket("intermediates");constoutputBucket=newBucket("words",{destroy: false// Don't destroy this bucket when destroying this application.});constmapperNum=10;constreducerNum=5;asyncfunctionmapFn(mapperId: number){constintermediateOutputs: {word: string,count: number}[][]=[];for(leti=0;i<reducerNum;i++){intermediateOutputs.push([]);}constarticleNum=awaitinputBucket.list();for(leti=mapperId;i<articleNum;i+=mapperId){letarticle=awaitinputBucket.get(`articles/${i}`);processOneArticle(article);}for(leti=0;i<reducerNum;i++){awaitintermediateBucket.set(`intermediates/${i}/${mapperId}`,JSON.stringify(intermediateOutputs[i]));}functionprocessOneArticle(article: string){// Replace all characters that aren't a letter or a hyphen with a spaceletprocessedArticle=article.replace(/[^a-zA-Z0-9\-]/g,' ');// Collapse all whitespaceprocessedArticle=processedArticle.replace(/s+/g,' ');// Split the line by space and iterate over each wordprocessedArticle.split(' ').forEach(function(word){consthash=MD5(word).toString();constpartition=parseInt(hash.substring(0,8),16)%reducerNum;// Emit the word and a 1intermediateOutputs[partition].push({word: word,count: 1});});}}asyncfunctionreduceFn(reducerId: number){// Read the intermediate outputs from each mapper and count the number of times each word appears.// The word counts are stored in a dictionary, where the key is the word and the value is the count.constwordCounts: {[word: string]: number}={};for(letmapperId=0;mapperId<mapperNum;mapperId++){constintermediateOutput=JSON.parse(awaitintermediateBucket.get(`intermediates/${reducerId}/${mapperId}`));intermediateOutput.forEach(function(wordCount: {word: string,count: number}){wordCounts[wordCount.word]=(wordCounts[wordCount.word]??0)+wordCount.count;})}// Convert the word counts to an array of objects, and save them to output bucket.// The output bucket will be a list of objects, each object contains a word and its count.constoutputs: {word: string,count: number}[]=[];for(constwordinwordCounts){outputs.push({word: word,count: wordCounts[word]});}awaitoutputBucket.set(`words/${reducerId}`,JSON.stringify(outputs));}// Sort the word counts by count and take the top k wordsasyncfunctiontopK(k: number){constwords: {word: string,count: number}[]=[];for(letreducerId=0;reducerId<reducerNum;reducerId++){constoutput=JSON.parse(awaitoutputBucket.get(`words/${reducerId}`));words.push(...output);}constsortedWords=words.sort((a,b)=>b.count-a.count);returnsortedWords.slice(0,k);}asyncfunctionmain(){// Create a list of mappers, and invoke them in parallelconstmappers=[]constmapFnResource=newFunction(mapFn);// Should be OK to create a resource object here.for(leti=0;i<mapperNum;i++){constmapperId=i;// Deep copymappers.push(mapFnResource.invoke(mapperId));// Invalid case.// Create the resource object using the data generated at runtime.// const fn = new Function(async () => { await mapFn(mapperId); }); // mapper.push(fn.invoke());}// Wait for all mappers to finishawaitPromise.all(mappers);// Create a list of reducers, and invoke them in parallelconstreducers=[];constreduceFnResource=newFunction(reduceFn);for(leti=0;i<reducerNum;i++){constreducerId=i;// Deep copyreducers.push(reduceFnResource.invoke(reducerId));}// Wait for all reducers to finishawaitPromise.all(reducers);// List the top 10 words based on their frequency in the resultconsttopKFnResource=newFunction(topK);consttopKWords=awaittopKFnResource.invoke(10);console.log(topKWords);}main();
Expected Behavior
During compilation, deduce from the code that the entire application contains 4 lambda and 2 bucket resource instances, and generate a pure IaC (Infrastructure as Code) code to create these resource instances. Among them, the 4 lambda resource instances correspond to four sections of code: mapper, reducer, topk, and the main code. The main code excludes Infra API-related code.
After deployment, the lambda resource instance corresponding to the main code is automatically triggered to execute, and the logs of the 4 lambda resource instances are output in chronological order. The output ends when the execution process of this resource instance is completed.
During execution in the cloud, the various resource objects that are created and invoked are Client class objects of the resource types. For example, the return values of new Function and new Bucket in the code are client class instances of Function and Bucket, respectively.
Additional Information
If, aside from the scope that includes Infra API statements, all other code does not involve Client API calls, it indicates that the main code, apart from the Infra API's Handler and resource instances, is unrelated to cloud resources and can be executed locally.
The text was updated successfully, but these errors were encountered:
Expected Behavior
During compilation, deduce from the code that the entire application contains 4 lambda and 2 bucket resource instances, and generate a pure IaC (Infrastructure as Code) code to create these resource instances. Among them, the 4 lambda resource instances correspond to four sections of code: mapper, reducer, topk, and the main code. The main code excludes Infra API-related code.
After deployment, the lambda resource instance corresponding to the main code is automatically triggered to execute, and the logs of the 4 lambda resource instances are output in chronological order. The output ends when the execution process of this resource instance is completed.
During execution in the cloud, the various resource objects that are created and invoked are Client class objects of the resource types. For example, the return values of
new Function
andnew Bucket
in the code are client class instances of Function and Bucket, respectively.Additional Information
If, aside from the scope that includes Infra API statements, all other code does not involve Client API calls, it indicates that the main code, apart from the Infra API's Handler and resource instances, is unrelated to cloud resources and can be executed locally.
The text was updated successfully, but these errors were encountered: