Exemplo n.º 1
0
  @Override
  protected void internalWrite(Server server, Query query, ImmutableList<Result> results)
      throws Exception {

    for (Result result : results) {
      log.debug("Query result: [{}]", result);
      Map<String, Object> resultValues = result.getValues();
      for (Entry<String, Object> values : resultValues.entrySet()) {
        Object value = values.getValue();
        if (isNumeric(value)) {

          Map<String, Object> map = new HashMap<String, Object>();
          map.put("serverAlias", server.getAlias());
          map.put("server", server.getHost());
          map.put("port", server.getPort());
          map.put("objDomain", result.getObjDomain());
          map.put("className", result.getClassName());
          map.put("typeName", result.getTypeName());
          map.put("attributeName", result.getAttributeName());
          map.put("key", values.getKey());
          map.put("keyAlias", result.getKeyAlias());
          map.put("value", Double.parseDouble(value.toString()));
          map.put("timestamp", result.getEpoch());

          log.debug(
              "Insert into Elastic: Index: [{}] Type: [{}] Map: [{}]",
              indexName,
              ELASTIC_TYPE_NAME,
              map);
          Index index = new Index.Builder(map).index(indexName).type(ELASTIC_TYPE_NAME).build();
          JestResult addToIndex = jestClient.execute(index);
          if (!addToIndex.isSucceeded()) {
            throw new ElasticWriterException(
                String.format(
                    "Unable to write entry to elastic: %s", addToIndex.getErrorMessage()));
          }
        } else {
          log.warn(
              "Unable to submit non-numeric value to Elastic: [{}] from result [{}]",
              value,
              result);
        }
      }
    }
  }