Last active
February 12, 2016 19:31
-
-
Save Miguel000/f5917b8d6c386eb02f21 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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.math.BigInteger; | |
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 org.json.JSONArray; | |
import org.json.JSONObject; | |
public class ComplaintsZgz extends BaseSinfonierSpout { | |
private LinkedBlockingQueue<String> queue = null; | |
private int frequency = 1000; | |
private static int rows; | |
private static String sort; | |
private static String start_date; | |
private static String urlString; | |
private static URL url; | |
private List<String> emitted = new ArrayList<>(); | |
private SecureRandom random = new SecureRandom(); | |
public ComplaintsZgz (String spoutName, String xmlPath) { | |
super(spoutName, xmlPath); | |
} | |
public void useropen(){ | |
this.frequency = Integer.parseInt(getParam("frequency",true)); | |
// Parametros | |
this.start_date = this.getParam("start_date"); | |
this.sort = this.getParam("sort"); | |
this.rows = Integer.parseInt(getParam("rows",true)); | |
try{ | |
urlString ="http://www.zaragoza.es/api/recurso/open311/requests.json?sort="+URLEncoder.encode(sort, "UTF-8")+"&start_date=" +URLEncoder.encode(start_date, "UTF-8")+ "&rows="+rows; | |
}catch(IOException e){ | |
e.printStackTrace(); | |
} | |
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(); | |
jsonString = "{\"results\": "+jsonString+"}"; | |
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("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