List of usage examples for com.mongodb BasicDBList get
public Object get(final String key)
From source file:org.envirocar.server.mongo.util.GeoBSON.java
License:Open Source License
@Override public GeometryCollection decodeGeometryCollection(BSONObject bson) throws GeometryConverterException { if (!bson.containsField(GEOMETRIES_KEY)) { throw new GeometryConverterException("missing 'geometries' field"); }/*w w w . jav a2s.c o m*/ BasicDBList geometries = toList(bson.get(GEOMETRIES_KEY)); Geometry[] geoms = new Geometry[geometries.size()]; for (int i = 0; i < geometries.size(); ++i) { geoms[i] = decodeGeometry(geometries.get(i)); } return factory.createGeometryCollection(geoms); }
From source file:org.geotools.data.mongodb.MongoResultSet.java
License:LGPL
/** * Create a Coordinate from given coordinates list * //from www. jav a 2s .c o m * @param coords list of coords * @return Coordinate, may be null if coords invalid */ private Coordinate createCoordinate(BasicDBList coords) { double x = 0.0; double y = 0.0; boolean success = true; Coordinate coord = null; try { x = Double.parseDouble(coords.get(0).toString()); y = Double.parseDouble(coords.get(1).toString()); if ((x < -180) || (x > 180)) success = false; if ((y < -90) || (y > 90)) success = false; if (success) { if (x < minX) minX = x; if (x > maxX) maxX = x; if (y < minY) minY = y; if (y > maxY) maxY = y; } coord = new Coordinate(x, y); } catch (Throwable t) { log.log(Level.SEVERE, t.getLocalizedMessage(), t); coord = null; } return coord; }
From source file:org.graylog2.hipchatalarmcallback.callback.GraylogElasticSearchClient.java
License:Open Source License
/** * Get the last GrayLog message in a stream. * @param streamId The GrayLog2 stream id * @param graylogUrl The URL to your GrayLog instance. * @param elasticSearchUrl The URL of the elasticsearch instance. * @return GraylogMessage object of the last message in the stream. *///from www . j a va 2 s .com public GraylogMessage getLastMessageInStream(String streamId, String graylogUrl, String elasticSearchUrl) { GraylogMessage message = new GraylogMessage(); Writer writer = null; try { // query elasticsearch for last message in stream URL url = new URL(elasticSearchUrl + "/_search"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); writer = new OutputStreamWriter(conn.getOutputStream()); writer.write("{\"query\": {\"term\": {\"streams\": \"" + streamId + "\"}}, \"sort\":[{\"created_at\": \"desc\"}], \"size\":1}"); writer.flush(); if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { // read elasticsearch API response into StringBuilder BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder responseData = new StringBuilder(); String line = null; while ((line = br.readLine()) != null) { responseData.append(line); } // create JSON Object from elasticsearch result String DBObject json = (DBObject) JSON.parse(responseData.toString()); DBObject hits = (DBObject) json.get("hits"); BasicDBList hitsList = (BasicDBList) hits.get("hits"); DBObject hit = (DBObject) hitsList.get(0); DBObject source = (DBObject) hit.get("_source"); message.setLevel(((Integer) source.get("level")).intValue()); String id = (String) hit.get("_id"); // message content String text = (String) source.get("message"); // append deep link to message on GrayLog2 text = text + "\n" + graylogUrl + "/messages/" + id; message.setText(text); } else { LOG.error("unable to query elasticsearch. response code: " + conn.getResponseCode()); } } catch (IOException e) { LOG.error("unable to query elasticsearch", e); message = null; } catch (RuntimeException rte) { LOG.error("unable to query elasticsearch", rte); message = null; } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { } } } return message; }
From source file:org.hibernate.ogm.datastore.mongodb.utils.MongoDBTestHelper.java
License:LGPL
private static boolean isDBObjectAndContentEqual(Object left, Object right) { if (left instanceof BasicDBList) { if (!(right instanceof BasicDBList)) { return false; }/* w w w . jav a2 s. c o m*/ // we don't care about the order here for now BasicDBList leftAsList = (BasicDBList) left; BasicDBList rightAsList = (BasicDBList) right; // check that the fields names are the same Set<String> leftFields = new HashSet<String>(leftAsList.keySet()); Set<String> rightFields = new HashSet<String>(rightAsList.keySet()); if (!leftFields.equals(rightFields)) { return false; } // check that all left field values are in right for (String field : leftFields) { // fall back to native equals via the contains. It is out of our current tests if (!rightAsList.contains(leftAsList.get(field))) { return false; } } } else if (left instanceof DBObject) { if (!(right instanceof DBObject)) { return false; } DBObject leftAsDBObject = (DBObject) left; DBObject rightAsDBObject = (DBObject) right; // check that the fields names are the same Set<String> leftFields = new HashSet<String>(leftAsDBObject.keySet()); Set<String> rightFields = new HashSet<String>(rightAsDBObject.keySet()); if (!leftFields.equals(rightFields)) { return false; } // check that all left fields are in right and equal for (String field : leftFields) { boolean matches = isDBObjectAndContentEqual(leftAsDBObject.get(field), rightAsDBObject.get(field)); if (!matches) { return false; } } } else { return left == right || (left != null && left.equals(right)); } return true; }
From source file:org.jeo.mongo.DefaultMapper.java
License:Open Source License
@Override public Envelope bbox(DBCollection dbcol, MongoDataset data) { MapReduceCommand mr = new MapReduceCommand(dbcol, bboxMap, bboxReduce, null, OutputType.INLINE, new BasicDBObject()); BasicDBList list = (BasicDBList) dbcol.mapReduce(mr).getCommandResult().get("results"); DBObject bbox = (DBObject) ((DBObject) list.get(0)).get("value"); return new Envelope((Double) bbox.get("x1"), (Double) bbox.get("x2"), (Double) bbox.get("y1"), (Double) bbox.get("y2")); }
From source file:org.jeo.mongo.GeoJSON.java
License:Open Source License
static MultiLineString toMultiLineString(DBObject obj) { GeometryFactory gf = new GeometryFactory(); BasicDBList list = (BasicDBList) obj.get("coordinates"); LineString[] lines = new LineString[list.size()]; for (int i = 0; i < list.size(); i++) { lines[i] = gf.createLineString(toCoordinates((BasicDBList) list.get(i))); }/* w w w . ja va2 s . c o m*/ return gf.createMultiLineString(lines); }
From source file:org.jeo.mongo.GeoJSON.java
License:Open Source License
static MultiPolygon toMultiPolygon(DBObject obj) { GeometryFactory gf = new GeometryFactory(); BasicDBList list = (BasicDBList) obj.get("coordinates"); Polygon[] polys = new Polygon[list.size()]; for (int i = 0; i < list.size(); i++) { polys[i] = toPolygon((BasicDBList) list.get(i)); }//from w w w . j a v a 2s.c om return gf.createMultiPolygon(polys); }
From source file:org.jeo.mongo.GeoJSON.java
License:Open Source License
static GeometryCollection toCollection(DBObject obj) { BasicDBList list = (BasicDBList) obj.get("geometries"); Geometry[] geoms = new Geometry[list.size()]; for (int i = 0; i < list.size(); i++) { geoms[i] = toGeometry((DBObject) list.get(i)); }/*w w w . j a v a 2s. co m*/ return new GeometryFactory().createGeometryCollection(geoms); }
From source file:org.jeo.mongo.GeoJSON.java
License:Open Source License
static Coordinate toCoordinate(BasicDBList list) { return new Coordinate(((Number) list.get(0)).doubleValue(), ((Number) list.get(1)).doubleValue()); }
From source file:org.jeo.mongo.GeoJSON.java
License:Open Source License
static Coordinate[] toCoordinates(BasicDBList list) { Coordinate[] coords = new Coordinate[list.size()]; for (int i = 0; i < list.size(); i++) { coords[i] = toCoordinate((BasicDBList) list.get(i)); }/*from ww w.ja v a 2s. c o m*/ return coords; }