org.elasticsearch.common.xcontent.XContentBuilder.humanReadable()方法的使用及代码示例

x33g5p2x  于2022-02-02 转载在 其他  
字(14.6k)|赞(0)|评价(0)|浏览(100)

本文整理了Java中org.elasticsearch.common.xcontent.XContentBuilder.humanReadable()方法的一些代码示例,展示了XContentBuilder.humanReadable()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。XContentBuilder.humanReadable()方法的具体详情如下:
包路径:org.elasticsearch.common.xcontent.XContentBuilder
类名称:XContentBuilder
方法名:humanReadable

XContentBuilder.humanReadable介绍

[英]When this flag is set to true, some types of values are written in a format easier to read for a human.
[中]当该标志设置为true时,某些类型的值会以更易于人类读取的格式写入。

代码示例

代码示例来源:origin: richardwilly98/elasticsearch-river-mongodb

public static XContentBuilder restContentBuilder(RestRequest request, @Nullable BytesReference autoDetectSource) throws IOException {
  XContentType contentType = XContentType.fromRestContentType(request.param("format", request.header("Content-Type")));
  if (contentType == null) {
    // try and guess it from the auto detect source
    if (autoDetectSource != null) {
      contentType = XContentFactory.xContentType(autoDetectSource);
    }
  }
  if (contentType == null) {
    // default to JSON
    contentType = XContentType.JSON;
  }
  XContentBuilder builder = new XContentBuilder(XContentFactory.xContent(contentType), new BytesStreamOutput());
  if (request.paramAsBoolean("pretty", false)) {
    builder.prettyPrint().lfAtEnd();
  }
  builder.humanReadable(request.paramAsBoolean("human", builder.humanReadable()));
  String casing = request.param("case");
  if (casing != null && "camelCase".equals(casing)) {
    builder.fieldCaseConversion(XContentBuilder.FieldCaseConversion.CAMELCASE);
  } else {
    // we expect all REST interfaces to write results in underscore casing, so
    // no need for double casing
    builder.fieldCaseConversion(XContentBuilder.FieldCaseConversion.NONE);
  }
  return builder;
}

代码示例来源:origin: org.elasticsearch/elasticsearch

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
  builder.startObject(Fields.MERGES);
  builder.field(Fields.CURRENT, current);
  builder.field(Fields.CURRENT_DOCS, currentNumDocs);
  builder.humanReadableField(Fields.CURRENT_SIZE_IN_BYTES, Fields.CURRENT_SIZE, getCurrentSize());
  builder.field(Fields.TOTAL, total);
  builder.humanReadableField(Fields.TOTAL_TIME_IN_MILLIS, Fields.TOTAL_TIME, getTotalTime());
  builder.field(Fields.TOTAL_DOCS, totalNumDocs);
  builder.humanReadableField(Fields.TOTAL_SIZE_IN_BYTES, Fields.TOTAL_SIZE, getTotalSize());
  builder.humanReadableField(Fields.TOTAL_STOPPED_TIME_IN_MILLIS, Fields.TOTAL_STOPPED_TIME, getTotalStoppedTime());
  builder.humanReadableField(Fields.TOTAL_THROTTLED_TIME_IN_MILLIS, Fields.TOTAL_THROTTLED_TIME, getTotalThrottledTime());
  if (builder.humanReadable() && totalBytesPerSecAutoThrottle != -1) {
    builder.field(Fields.TOTAL_THROTTLE_BYTES_PER_SEC).value(new ByteSizeValue(totalBytesPerSecAutoThrottle).toString());
  }
  builder.field(Fields.TOTAL_THROTTLE_BYTES_PER_SEC_IN_BYTES, totalBytesPerSecAutoThrottle);
  builder.endObject();
  return builder;
}

代码示例来源:origin: org.elasticsearch/elasticsearch

private static XContentBuilder createBuilder(boolean pretty, boolean human) throws IOException {
  XContentBuilder builder = JsonXContent.contentBuilder();
  if (pretty) {
    builder.prettyPrint();
  }
  if (human) {
    builder.humanReadable(true);
  }
  return builder;
}

代码示例来源:origin: apache/servicemix-bundles

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
  builder.startObject(Fields.MERGES);
  builder.field(Fields.CURRENT, current);
  builder.field(Fields.CURRENT_DOCS, currentNumDocs);
  builder.humanReadableField(Fields.CURRENT_SIZE_IN_BYTES, Fields.CURRENT_SIZE, getCurrentSize());
  builder.field(Fields.TOTAL, total);
  builder.humanReadableField(Fields.TOTAL_TIME_IN_MILLIS, Fields.TOTAL_TIME, getTotalTime());
  builder.field(Fields.TOTAL_DOCS, totalNumDocs);
  builder.humanReadableField(Fields.TOTAL_SIZE_IN_BYTES, Fields.TOTAL_SIZE, getTotalSize());
  builder.humanReadableField(Fields.TOTAL_STOPPED_TIME_IN_MILLIS, Fields.TOTAL_STOPPED_TIME, getTotalStoppedTime());
  builder.humanReadableField(Fields.TOTAL_THROTTLED_TIME_IN_MILLIS, Fields.TOTAL_THROTTLED_TIME, getTotalThrottledTime());
  if (builder.humanReadable() && totalBytesPerSecAutoThrottle != -1) {
    builder.field(Fields.TOTAL_THROTTLE_BYTES_PER_SEC).value(new ByteSizeValue(totalBytesPerSecAutoThrottle).toString());
  }
  builder.field(Fields.TOTAL_THROTTLE_BYTES_PER_SEC_IN_BYTES, totalBytesPerSecAutoThrottle);
  builder.endObject();
  return builder;
}

代码示例来源:origin: org.elasticsearch/elasticsearch

builder.humanReadable(human);
return builder;

代码示例来源:origin: org.elasticsearch/elasticsearch

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
  builder.startObject("adaptive_selection");
  Set<String> allNodeIds = Sets.union(clientOutgoingConnections.keySet(), nodeComputedStats.keySet());
  for (String nodeId : allNodeIds) {
    builder.startObject(nodeId);
    ResponseCollectorService.ComputedNodeStats stats = nodeComputedStats.get(nodeId);
    if (stats != null) {
      long outgoingSearches = clientOutgoingConnections.getOrDefault(nodeId, 0L);
      builder.field("outgoing_searches", outgoingSearches);
      builder.field("avg_queue_size", stats.queueSize);
      if (builder.humanReadable()) {
        builder.field("avg_service_time", new TimeValue((long) stats.serviceTime, TimeUnit.NANOSECONDS).toString());
      }
      builder.field("avg_service_time_ns", (long) stats.serviceTime);
      if (builder.humanReadable()) {
        builder.field("avg_response_time", new TimeValue((long) stats.responseTime, TimeUnit.NANOSECONDS).toString());
      }
      builder.field("avg_response_time_ns", (long) stats.responseTime);
      builder.field("rank", String.format(Locale.ROOT, "%.1f", stats.rank(outgoingSearches)));
    }
    builder.endObject();
  }
  builder.endObject();
  return builder;
}

代码示例来源:origin: org.elasticsearch/elasticsearch

/**
 * Returns the bytes that represent the XContent output of the provided {@link ToXContent} object, using the provided
 * {@link XContentType}. Wraps the output into a new anonymous object according to the value returned
 * by the {@link ToXContent#isFragment()} method returns.
 */
public static BytesReference toXContent(ToXContent toXContent, XContentType xContentType, Params params,
                    boolean humanReadable) throws IOException {
  try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) {
    builder.humanReadable(humanReadable);
    if (toXContent.isFragment()) {
      builder.startObject();
    }
    toXContent.toXContent(builder, params);
    if (toXContent.isFragment()) {
      builder.endObject();
    }
    return BytesReference.bytes(builder);
  }
}

代码示例来源:origin: org.elasticsearch/elasticsearch

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
  builder.field("node", taskId.getNodeId());
  builder.field("id", taskId.getId());
  builder.field("type", type);
  builder.field("action", action);
  if (status != null) {
    builder.field("status", status, params);
  }
  if (description != null) {
    builder.field("description", description);
  }
  builder.timeField("start_time_in_millis", "start_time", startTime);
  if (builder.humanReadable()) {
    builder.field("running_time", new TimeValue(runningTimeNanos, TimeUnit.NANOSECONDS).toString());
  }
  builder.field("running_time_in_nanos", runningTimeNanos);
  builder.field("cancellable", cancellable);
  if (parentTaskId.isSet()) {
    builder.field("parent_task_id", parentTaskId.toString());
  }
  builder.startObject("headers");
  for(Map.Entry<String, String> attribute : headers.entrySet()) {
    builder.field(attribute.getKey(), attribute.getValue());
  }
  builder.endObject();
  return builder;
}

代码示例来源:origin: org.elasticsearch/elasticsearch

@Override
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
  builder = builder.startObject();
  builder.field(NAME.getPreferredName(), getName());
  builder.field(REASON.getPreferredName(), getReason());
  if (builder.humanReadable()) {
    builder.field(TIME.getPreferredName(), new TimeValue(getTime(), TimeUnit.NANOSECONDS).toString());
  }
  builder.field(TIME_NANOS.getPreferredName(), getTime());
  if (!children.isEmpty()) {
    builder = builder.startArray(CHILDREN.getPreferredName());
    for (CollectorResult child : children) {
      builder = child.toXContent(builder, params);
    }
    builder = builder.endArray();
  }
  builder = builder.endObject();
  return builder;
}

代码示例来源:origin: org.elasticsearch/elasticsearch

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
  builder.startObject();
  builder.field(TYPE.getPreferredName(), type);
  builder.field(DESCRIPTION.getPreferredName(), description);
  if (builder.humanReadable()) {
    builder.field(NODE_TIME.getPreferredName(), new TimeValue(getTime(), TimeUnit.NANOSECONDS).toString());
  }
  builder.field(NODE_TIME_RAW.getPreferredName(), getTime());
  builder.field(BREAKDOWN.getPreferredName(), timings);
  if (!children.isEmpty()) {
    builder = builder.startArray(CHILDREN.getPreferredName());
    for (ProfileResult child : children) {
      builder = child.toXContent(builder, params);
    }
    builder = builder.endArray();
  }
  builder = builder.endObject();
  return builder;
}

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.elasticsearch

private static XContentBuilder createBuilder(boolean pretty, boolean human) throws IOException {
  XContentBuilder builder = JsonXContent.contentBuilder();
  if (pretty) {
    builder.prettyPrint();
  }
  if (human) {
    builder.humanReadable(true);
  }
  return builder;
}

代码示例来源:origin: apache/servicemix-bundles

private static XContentBuilder createBuilder(boolean pretty, boolean human) throws IOException {
  XContentBuilder builder = JsonXContent.contentBuilder();
  if (pretty) {
    builder.prettyPrint();
  }
  if (human) {
    builder.humanReadable(true);
  }
  return builder;
}

代码示例来源:origin: com.strapdata.elasticsearch/elasticsearch

public BytesReference source(IndexService indexService, DocumentMapper docMapper, Map sourceAsMap, Uid uid) throws JsonParseException, JsonMappingException, IOException {
  if (docMapper.sourceMapper().enabled()) {
    // retreive from _source columns stored as blob in cassandra if available.
    ByteBuffer bb = (ByteBuffer) sourceAsMap.get(SourceFieldMapper.NAME);
    if (bb != null)
      return new BytesArray(bb.array(), bb.position(), bb.limit() - bb.position());
  } 
  // rebuild _source from all cassandra columns.
  XContentBuilder builder = buildDocument(docMapper, sourceAsMap, true, isStaticDocument(indexService, uid));
  builder.humanReadable(true);
  return builder.bytes();
}

代码示例来源:origin: com.scireum/sirius-search

private Map<String, String> getIndicesStatus(IndicesStatsResponse indicesStatsResponse) {
  Map<String, String> indexStatusMap = new HashMap<>();
  for (Map.Entry<String, IndexStats> indexStat : indicesStatsResponse.getIndices().entrySet()) {
    try (XContentBuilder indexStatsBuilder = XContentFactory.jsonBuilder()) {
      indexStatsBuilder.humanReadable(true).prettyPrint().startObject();
      indicesStatsResponse.getIndex(indexStat.getKey())
                .getTotal()
                .toXContent(indexStatsBuilder, ToXContent.EMPTY_PARAMS);
      indexStatsBuilder.endObject();
      indexStatusMap.put(indexStat.getKey(), indexStatsBuilder.string());
    } catch (IOException e) {
      Exceptions.handle(e);
    }
  }
  return indexStatusMap;
}

代码示例来源:origin: com.scireum/sirius-search

private String getClusterState(ClusterStateResponse clusterStateResponse) {
  String state = "";
  try (XContentBuilder clusterStateBuilder = XContentFactory.jsonBuilder()) {
    clusterStateBuilder.humanReadable(true).prettyPrint().startObject();
    clusterStateResponse.getState().toXContent(clusterStateBuilder, ToXContent.EMPTY_PARAMS);
    clusterStateBuilder.endObject();
    state = clusterStateBuilder.string();
  } catch (IOException e) {
    Exceptions.handle(e);
  }
  return state;
}

代码示例来源:origin: com.scireum/sirius-search

private Map<String, String> getNodesStats(NodesStatsResponse nodesStatsResponse) {
  Map<String, String> nodeStatsMap = new HashMap<>();
  for (NodeStats stat : nodesStatsResponse.getNodes()) {
    try (XContentBuilder nodesStatsBuilder = XContentFactory.jsonBuilder()) {
      nodesStatsBuilder.humanReadable(true).prettyPrint().startObject();
      stat.toXContent(nodesStatsBuilder, ToXContent.EMPTY_PARAMS);
      nodesStatsBuilder.endObject();
      nodeStatsMap.put(stat.getNode().getName(), nodesStatsBuilder.string());
    } catch (IOException e) {
      Exceptions.handle(e);
    }
  }
  return nodeStatsMap;
}

代码示例来源:origin: com.strapdata.elasticsearch/elasticsearch

/**
   * Returns the bytes that represent the XContent output of the provided {@link ToXContent} object, using the provided
   * {@link XContentType}. Wraps the output into a new anonymous object according to the value returned
   * by the {@link ToXContent#isFragment()} method returns.
   */
  public static BytesReference toXContent(ToXContent toXContent, XContentType xContentType, Params params, boolean humanReadable) throws IOException {
    try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) {
      builder.humanReadable(humanReadable);
      if (toXContent.isFragment()) {
        builder.startObject();
      }
      toXContent.toXContent(builder, params);
      if (toXContent.isFragment()) {
        builder.endObject();
      }
      return builder.bytes();
    }
  }
}

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.elasticsearch

/**
 * Returns the bytes that represent the XContent output of the provided {@link ToXContent} object, using the provided
 * {@link XContentType}. Wraps the output into a new anonymous object according to the value returned
 * by the {@link ToXContent#isFragment()} method returns.
 */
public static BytesReference toXContent(ToXContent toXContent, XContentType xContentType, Params params, boolean humanReadable) throws IOException {
  try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) {
    builder.humanReadable(humanReadable);
    if (toXContent.isFragment()) {
      builder.startObject();
    }
    toXContent.toXContent(builder, params);
    if (toXContent.isFragment()) {
      builder.endObject();
    }
    return BytesReference.bytes(builder);
  }
}

代码示例来源:origin: apache/servicemix-bundles

/**
 * Returns the bytes that represent the XContent output of the provided {@link ToXContent} object, using the provided
 * {@link XContentType}. Wraps the output into a new anonymous object according to the value returned
 * by the {@link ToXContent#isFragment()} method returns.
 */
public static BytesReference toXContent(ToXContent toXContent, XContentType xContentType, Params params, boolean humanReadable) throws IOException {
  try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) {
    builder.humanReadable(humanReadable);
    if (toXContent.isFragment()) {
      builder.startObject();
    }
    toXContent.toXContent(builder, params);
    if (toXContent.isFragment()) {
      builder.endObject();
    }
    return BytesReference.bytes(builder);
  }
}

代码示例来源:origin: com.scireum/sirius-search

private Map<String, String> getIndicesMetadata(ClusterStateResponse clusterStateResponse) {
  Map<String, String> metaDataMap = new HashMap<>();
  for (ObjectObjectCursor<String, IndexMetaData> metadata : clusterStateResponse.getState()
                                         .getMetaData()
                                         .getIndices()) {
    try (XContentBuilder metaDataBuilder = XContentFactory.jsonBuilder()) {
      metaDataBuilder.humanReadable(true).prettyPrint().startObject();
      clusterStateResponse.getState()
                .getMetaData()
                .index(metadata.key)
                .toXContent(metaDataBuilder, ToXContent.EMPTY_PARAMS);
      metaDataBuilder.endObject();
      metaDataMap.put(metadata.key, metaDataBuilder.string());
    } catch (IOException e) {
      Exceptions.handle(e);
    }
  }
  return metaDataMap;
}

相关文章

微信公众号

最新文章

更多