Skip to content

Instantly share code, notes, and snippets.

@Miguel000
Last active February 6, 2016 14:30
Show Gist options
  • Save Miguel000/a3931706d1cdc86e5c1d to your computer and use it in GitHub Desktop.
Save Miguel000/a3931706d1cdc86e5c1d to your computer and use it in GitHub Desktop.
Data to input type: Supported types: aragon, spain, 30days, 60days, 90days, diff
/*
The MIT License (MIT)
Copyright (c) 2015 sinfonier-project
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package com.sinfonier.spouts;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerContext;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import java.io.InputStream;
import java.io.IOException;
import java.util.HashMap;
import java.net.URLEncoder;
import java.net.URL;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import backtype.storm.utils.Utils;
import org.apache.commons.io.IOUtils;
import org.json.XML;
import org.json.JSONArray;
import org.json.JSONObject;
public class TrendingTopicsAra extends BaseSinfonierSpout {
private LinkedBlockingQueue<String> queue = null;
private int frequency = 1000;
private static String type;
private static URL url;
private static String urlString;
private List<String> emitted = new ArrayList<>();
private SecureRandom random = new SecureRandom();
public TrendingTopicsAra (String spoutName, String xmlPath) {
super(spoutName, xmlPath);
}
public void useropen(){
this.frequency = Integer.parseInt(getParam("frequency",true));
// Parametros
this.type = this.getParam("type");
urlString ="http://opendata.aragon.es/socialdata/trendings?type="+type;
queue = new LinkedBlockingQueue<String>(1000);
JobDetail job = JobBuilder.newJob(GetData.class)
.withIdentity("dummyJobName", "group1").build();
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("Retrieve Items")
.withSchedule(
SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(this.frequency)
.repeatForever()).build();
String schedulerName = new BigInteger(130, random).toString(32);
System.setProperty("org.quartz.scheduler.instanceName", schedulerName);
Scheduler scheduler;
try {
StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();
stdSchedulerFactory.initialize();
scheduler = stdSchedulerFactory.getScheduler();
scheduler.getContext().put("queue", queue);
scheduler.getContext().put("frequency", frequency);
scheduler.getContext().put("emitted", emitted);
scheduler.start();
scheduler.scheduleJob(job, trigger);
} catch (SchedulerException e) {
e.printStackTrace();
}
}
public void usernextTuple(){
// TO-DO: Write code here. This code reads an input tuple by each execution
// You can use the same functions as in the Bolts to process it.
// Tipically is to use this.addField to build the Tuple to emit.
if (!queue.isEmpty()) {
String json = queue.poll();
this.setJson(json);
this.emit();
} else {
Utils.sleep(50);
}
}
public void userclose() {
}
public static class GetData implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
SchedulerContext schedulerContext = null;
try {
schedulerContext = context.getScheduler().getContext();
@SuppressWarnings("unchecked")
LinkedBlockingQueue<String> queue = (LinkedBlockingQueue<String>) schedulerContext
.get("queue");
List<String> emitted = (List<String>) schedulerContext.get("emitted");
url = new URL (urlString);
InputStream is = url.openStream();
int ptr = 0;
StringBuilder builder = new StringBuilder();
while ((ptr = is.read()) != -1) {
builder.append((char) ptr);
}
String jsonString = builder.toString();
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> json = new HashMap<String, Object>();
json = mapper.readValue(jsonString,new TypeReference<Map<String, Object>>() {});
JSONObject ticker = new JSONObject(json);
if(!ticker.get("status").equals("OK")){
System.out.println("No se encuentran valores con estos parametros");
}else{
JSONArray jsonArray = ticker.getJSONArray("results");
for (int i = 0; i < jsonArray.length(); i++) {
if (emitted.isEmpty()
|| !emitted.contains(jsonArray.getJSONObject(i).toString())) {
queue.put(jsonArray.getJSONObject(i).toString());
}
}
emitted.clear();
for (int i = 0; i < jsonArray.length(); i++) {
emitted.add(jsonArray.getJSONObject(i).toString());
}
}
} catch (SchedulerException e1) {
e1.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment