Skip to content

Instantly share code, notes, and snippets.

@Miguel000
Last active February 4, 2016 23:46
Show Gist options
  • Save Miguel000/58682eb8150cec6ace00 to your computer and use it in GitHub Desktop.
Save Miguel000/58682eb8150cec6ace00 to your computer and use it in GitHub Desktop.
/*
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.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 java.math.BigInteger;
import org.json.JSONArray;
import org.json.JSONObject;
public class SantanderCameras extends BaseSinfonierSpout {
private LinkedBlockingQueue<String> queue = null;
private int frequency = 1000;
private static String location;
private static String end_url;
private static URL url;
private static String urlString;
private List<String> emitted = new ArrayList<>();
private SecureRandom random = new SecureRandom();
public SantanderCameras (String spoutName, String xmlPath) {
super(spoutName, xmlPath);
}
public void useropen(){
this.frequency = Integer.parseInt(getParam("frequency",true));
// Parametros
this.location = this.getParam("location");
switch (this.location.toLowerCase()) {
case "cuatro caminos" :
end_url = "camara_cuatro_caminos.json";
break;
case "calvo sotelo" :
end_url = "camara_calvo_sotelo.json";
break;
case "antonio lopez" :
end_url = "camara_antonio_lopez.json";
break;
case "castilla" :
end_url = "camara_castilla.json";
break;
default:
end_url = "camara_cuatro_caminos.json";
break;
}
urlString ="http://datos.santander.es/api/rest/datasets/" + end_url ;
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);
JSONArray jsonArray = ticker.getJSONArray("resources");
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