private Response search(final String table, final String key) throws IOException { try (XContentBuilder builder = jsonBuilder()) { builder.startObject(); builder.startObject("query"); builder.startObject("term"); builder.field(KEY, key); builder.endObject(); builder.endObject(); builder.endObject(); return search(table, builder); } }
private static XContentBuilder source(Timestamp<?> time) throws IOException { XContentBuilder builder = jsonBuilder().startObject().startObject(TYPE); time.saveFields(builder); return builder.endObject().endObject(); }
private static String convertToJsType(Object value) throws PermanentBackendException { try { XContentBuilder builder = XContentFactory.jsonBuilder().startObject(); builder.field("value", convertToEsType(value)); String s = builder.string(); int prefixLength = "{\"value\":".length(); int suffixLength = "}".length(); String result = s.substring(prefixLength, s.length() - suffixLength); result = result.replace("$", "\\$"); return result; } catch (IOException e) { throw new PermanentBackendException("Could not write json"); } }
@Override public XContentBuilder getContentBuilder(Event event) throws IOException { XContentBuilder builder = jsonBuilder().startObject(); appendBody(builder, event); appendHeaders(builder, event); return builder; }
private XContentBuilder getGridFSMapping() throws IOException { XContentBuilder mapping = jsonBuilder() .startObject() .startObject(definition.getTypeName()) .startObject("properties") .startObject("content").field("type", "attachment").endObject() .startObject("filename").field("type", "string").endObject() .startObject("contentType").field("type", "string").endObject() .startObject("md5").field("type", "string").endObject() .startObject("length").field("type", "long").endObject() .startObject("chunkSize").field("type", "long").endObject() .endObject() .endObject() .endObject(); logger.info("GridFS Mapping: {}", mapping.string()); return mapping; }
@Override public String toString() { try { XContentBuilder builder = XContentFactory.jsonBuilder().startObject(); toXContent(builder, new ToXContent.MapParams(emptyMap())); return Strings.toString(builder.endObject()); } catch (IOException bogus) { throw new UncheckedIOException(bogus); } } }
protected XContentBuilder notEndedDocument(String messageId, long timestamp, String topicName, String status, String hostname) throws IOException { return jsonBuilder(new BytesStreamOutput(DOCUMENT_EXPECTED_SIZE)) .startObject() .field(MESSAGE_ID, messageId) .field(TIMESTAMP, timestamp) .field(TIMESTAMP_SECONDS, toSeconds(timestamp)) .field(TOPIC_NAME, topicName) .field(STATUS, status) .field(CLUSTER, clusterName) .field(SOURCE_HOSTNAME, this.hostname) .field(REMOTE_HOSTNAME, hostname); }
@Override public XContentBuilder getContentBuilder(Event event) throws IOException { XContentBuilder builder = jsonBuilder().startObject(); appendBody(builder, event); appendHeaders(builder, event); return builder; }
public static void setRiverStatus(Client client, String riverName, Status status) { logger.info("setRiverStatus called with {} - {}", riverName, status); XContentBuilder xb; try { xb = jsonBuilder().startObject().startObject(MongoDBRiver.TYPE).field(MongoDBRiver.STATUS_FIELD, status).endObject() .endObject(); client.prepareIndex("_river", riverName, MongoDBRiver.STATUS_ID).setSource(xb).get(); } catch (IOException ioEx) { logger.error("setRiverStatus failed for river {}", ioEx, riverName); } }
@Override public String toString() { try { final XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint(); builder.startObject(); toXContent(builder, EMPTY_PARAMS); builder.endObject(); return Strings.toString(builder); } catch (final IOException e) { return "{ \"error\" : \"" + e.getMessage() + "\"}"; } }
protected XContentBuilder notEndedDocument(MessageMetadata message, long timestamp, String status) throws IOException { return jsonBuilder(new BytesStreamOutput(DOCUMENT_EXPECTED_SIZE)) .startObject() .field(MESSAGE_ID, message.getMessageId()) .field(BATCH_ID, message.getBatchId()) .field(TIMESTAMP, timestamp) .field(TIMESTAMP_SECONDS, toSeconds(timestamp)) .field(PUBLISH_TIMESTAMP, message.getPublishingTimestamp()) .field(TOPIC_NAME, message.getTopic()) .field(SUBSCRIPTION, message.getSubscription()) .field(STATUS, status) .field(OFFSET, message.getOffset()) .field(PARTITION, message.getPartition()) .field(CLUSTER, clusterName) .field(SOURCE_HOSTNAME, hostname); }
/** * Insert a record in the database. Any field/value pairs in the specified * values HashMap will be written into the record with the specified record * key. * * @param table * The name of the table * @param key * The record key of the record to insert. * @param values * A HashMap of field/value pairs to insert in the record * @return Zero on success, a non-zero error code on error. See this class's * description for a discussion of error codes. */ @Override public Status insert(String table, String key, Map<String, ByteIterator> values) { try { final XContentBuilder doc = jsonBuilder().startObject(); for (Entry<String, String> entry : StringByteIterator.getStringMap(values).entrySet()) { doc.field(entry.getKey(), entry.getValue()); } doc.endObject(); client.prepareIndex(indexKey, table, key).setSource(doc).execute().actionGet(); return Status.OK; } catch (Exception e) { e.printStackTrace(); return Status.ERROR; } }
@Override public String toString() { try { XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint(); builder.startObject(); toXContent(builder, EMPTY_PARAMS); builder.endObject(); return Strings.toString(builder); } catch (IOException e) { return "{ \"error\" : \"" + e.getMessage() + "\"}"; } }
public XContentBuilder getNewDocument(final List<IndexEntry> additions, KeyInformation.StoreRetriever informations, int ttl) throws BackendException { Preconditions.checkArgument(ttl >= 0); try { XContentBuilder builder = XContentFactory.jsonBuilder().startObject(); builder.field(add.getKey(), value); if (hasDualStringMapping(informations.get(add.getKey())) && keyInformation.getDataType() == String.class) { builder.field(getDualMappingName(add.getKey()), value); if (ttl>0) builder.field(TTL_FIELD, TimeUnit.MILLISECONDS.convert(ttl,TimeUnit.SECONDS)); builder.endObject();
@Override public String toString() { try { XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint(); builder.startObject(); toXContent(builder, EMPTY_PARAMS); builder.endObject(); return Strings.toString(builder); } catch (IOException e) { return "{ \"error\" : \"" + e.getMessage() + "\"}"; } }
try { final Response response; try (XContentBuilder builder = jsonBuilder()) { builder.startObject(); builder.startObject("query"); builder.startObject("range"); builder.startObject(KEY); builder.field("gte", startkey); builder.endObject(); builder.endObject(); builder.endObject(); builder.field("size", recordcount); builder.endObject(); response = search(table, builder);
@Override public synchronized String toString() { try { XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint(); builder.startObject(); toXContent(builder, EMPTY_PARAMS); builder.endObject(); return Strings.toString(builder); } catch (IOException e) { return "{ \"error\" : \"" + e.getMessage() + "\"}"; } }
@Override public Status insert(String table, String key, Map<String, ByteIterator> values) { try (XContentBuilder doc = jsonBuilder()) { doc.startObject(); for (final Entry<String, String> entry : StringByteIterator.getStringMap(values).entrySet()) { doc.field(entry.getKey(), entry.getValue()); } doc.field(KEY, key); doc.endObject(); final IndexResponse indexResponse = client.prepareIndex(indexKey, table).setSource(doc).get(); if (indexResponse.getResult() != DocWriteResponse.Result.CREATED) { return Status.ERROR; } if (!isRefreshNeeded) { synchronized (this) { isRefreshNeeded = true; } } return Status.OK; } catch (final Exception e) { e.printStackTrace(); return Status.ERROR; } }
@Override public String toString() { try { XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint(); builder.startObject(); toXContent(builder, EMPTY_PARAMS); builder.endObject(); return Strings.toString(builder); } catch (IOException e) { return "{ \"error\" : \"" + e.getMessage() + "\"}"; } } }
/** * @param requestBuilder * @param rowMeta * @param row * @throws IOException */ private void addSourceFromRowFields( IndexRequestBuilder requestBuilder, RowMetaInterface rowMeta, Object[] row ) throws IOException { XContentBuilder jsonBuilder = XContentFactory.jsonBuilder().startObject(); for ( int i = 0; i < rowMeta.size(); i++ ) { if ( idFieldIndex != null && i == idFieldIndex ) { // skip id continue; } ValueMetaInterface valueMeta = rowMeta.getValueMeta( i ); String name = hasFields ? columnsToJson.get( valueMeta.getName() ) : valueMeta.getName(); Object value = row[i]; if ( value instanceof Date && value.getClass() != Date.class ) { Date subDate = (Date) value; // create a genuine Date object, or jsonBuilder will not recognize it value = new Date( subDate.getTime() ); } if ( StringUtils.isNotBlank( name ) ) { jsonBuilder.field( name, value ); } } jsonBuilder.endObject(); requestBuilder.setSource( jsonBuilder ); }