org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode.get()方法的使用及代码示例

x33g5p2x  于2022-01-22 转载在 其他  
字(14.8k)|赞(0)|评价(0)|浏览(177)

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

JsonNode.get介绍

暂无

代码示例

代码示例来源:origin: apache/flink

/**
   * Select the language from the incoming JSON text.
   */
  @Override
  public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {
    if (jsonParser == null) {
      jsonParser = new ObjectMapper();
    }
    JsonNode jsonNode = jsonParser.readValue(value, JsonNode.class);
    boolean isEnglish = jsonNode.has("user") && jsonNode.get("user").has("lang") && jsonNode.get("user").get("lang").asText().equals("en");
    boolean hasText = jsonNode.has("text");
    if (isEnglish && hasText) {
      // message of tweet
      StringTokenizer tokenizer = new StringTokenizer(jsonNode.get("text").asText());
      // split the message
      while (tokenizer.hasMoreTokens()) {
        String result = tokenizer.nextToken().replaceAll("\\s*", "").toLowerCase();
        if (!result.equals("")) {
          out.collect(new Tuple2<>(result, 1));
        }
      }
    }
  }
}

代码示例来源:origin: apache/flink

final String json = response.body().string();
final JsonNode taskManagerList = OBJECT_MAPPER.readTree(json)
  .get("taskmanagers");

代码示例来源:origin: apache/flink

private static String convertLegacyJobOverview(String legacyOverview) throws IOException {
  JsonNode root = mapper.readTree(legacyOverview);
  JsonNode finishedJobs = root.get("finished");
  JsonNode job = finishedJobs.get(0);
  JobID jobId = JobID.fromHexString(job.get("jid").asText());
  String name = job.get("name").asText();
  JobStatus state = JobStatus.valueOf(job.get("state").asText());
  long startTime = job.get("start-time").asLong();
  long endTime = job.get("end-time").asLong();
  long duration = job.get("duration").asLong();
  long lastMod = job.get("last-modification").asLong();
  JsonNode tasks = job.get("tasks");
  int numTasks = tasks.get("total").asInt();
  int pending = tasks.get("pending").asInt();
  int running = tasks.get("running").asInt();
  int finished = tasks.get("finished").asInt();
  int canceling = tasks.get("canceling").asInt();
  int canceled = tasks.get("canceled").asInt();
  int failed = tasks.get("failed").asInt();

代码示例来源:origin: apache/flink

private Object convertObjectArray(JsonNode node, TypeInformation<?> elementType) {
  final Object[] array = (Object[]) Array.newInstance(elementType.getTypeClass(), node.size());
  for (int i = 0; i < node.size(); i++) {
    array[i] = convert(node.get(i), elementType);
  }
  return array;
}

代码示例来源:origin: apache/flink

private static TypeInformation<Row> convertObject(String location, JsonNode node, JsonNode root) {
  // validate properties
  if (!node.has(PROPERTIES)) {
    return Types.ROW();
  }
  if (!node.isObject()) {
    throw new IllegalArgumentException(
      "Invalid '" + PROPERTIES + "' property for object type in node: " + location);
  }
  final JsonNode props = node.get(PROPERTIES);
  final String[] names = new String[props.size()];
  final TypeInformation<?>[] types = new TypeInformation[props.size()];
  final Iterator<Map.Entry<String, JsonNode>> fieldIter = props.fields();
  int i = 0;
  while (fieldIter.hasNext()) {
    final Map.Entry<String, JsonNode> subNode = fieldIter.next();
    // set field name
    names[i] = subNode.getKey();
    // set type
    types[i] = convertType(location + '/' + subNode.getKey(), subNode.getValue(), root);
    i++;
  }
  // validate that object does not contain additional properties
  if (node.has(ADDITIONAL_PROPERTIES) && node.get(ADDITIONAL_PROPERTIES).isBoolean() &&
      node.get(ADDITIONAL_PROPERTIES).asBoolean()) {
    throw new IllegalArgumentException(
      "An object must not allow additional properties in node: " + location);
  }
  return Types.ROW_NAMED(names, types);
}

代码示例来源:origin: apache/flink

private static TypeInformation<?> convertArray(String location, JsonNode node, JsonNode root) {
  // validate items
  if (!node.has(ITEMS)) {
    throw new IllegalArgumentException(
      "Arrays must specify an '" + ITEMS + "' property in node: " + location);
  }
  final JsonNode items = node.get(ITEMS);
  // list (translated to object array)
  if (items.isObject()) {
    final TypeInformation<?> elementType = convertType(
      location + '/' + ITEMS,
      items,
      root);
    // result type might either be ObjectArrayTypeInfo or BasicArrayTypeInfo for Strings
    return Types.OBJECT_ARRAY(elementType);
  }
  // tuple (translated to row)
  else if (items.isArray()) {
    final TypeInformation<?>[] types = convertTypes(location + '/' + ITEMS, items, root);
    // validate that array does not contain additional items
    if (node.has(ADDITIONAL_ITEMS) && node.get(ADDITIONAL_ITEMS).isBoolean() &&
        node.get(ADDITIONAL_ITEMS).asBoolean()) {
      throw new IllegalArgumentException(
        "An array tuple must not allow additional items in node: " + location);
    }
    return Types.ROW(types);
  }
  throw new IllegalArgumentException(
    "Invalid type for '" + ITEMS + "' property in node: " + location);
}

代码示例来源:origin: apache/flink

if (node.has(REF) && node.get(REF).isTextual()) {
  ref = Optional.of(resolveReference(node.get(REF).asText(), node, root));
} else {
  ref = Optional.empty();
  final JsonNode typeNode = node.get(TYPE);
      case TYPE_STRING:
        if (node.has(FORMAT)) {
          typeSet.add(convertStringFormat(location, node.get(FORMAT)));
        } else if (node.has(CONTENT_ENCODING)) {
          typeSet.add(convertStringEncoding(location, node.get(CONTENT_ENCODING)));
        } else {
          typeSet.add(Types.STRING);
      default:
        throw new IllegalArgumentException(
          "Unsupported type '" + node.get(TYPE).asText() + "' in node: " + location);
  ref.filter(r -> r.has(TYPE)).ifPresent(r -> typeSet.add(convertType(node.get(REF).asText(), r, root)));
if (node.has(ONE_OF) && node.get(ONE_OF).isArray()) {
  final TypeInformation<?>[] types = convertTypes(location + '/' + ONE_OF, node.get(ONE_OF), root);
  typeSet.addAll(Arrays.asList(types));
else if (ref.isPresent() && ref.get().has(ONE_OF) && ref.get().get(ONE_OF).isArray()) {
  final TypeInformation<?>[] types = convertTypes(node.get(REF).asText() + '/' + ONE_OF, ref.get().get(ONE_OF), root);
  typeSet.addAll(Arrays.asList(types));

代码示例来源:origin: apache/flink

private Row convertRow(JsonNode node, RowTypeInfo info) {
  final String[] names = info.getFieldNames();
  final TypeInformation<?>[] types = info.getFieldTypes();
  final Row row = new Row(names.length);
  for (int i = 0; i < names.length; i++) {
    final String name = names[i];
    final JsonNode subNode = node.get(name);
    if (subNode == null) {
      if (failOnMissingField) {
        throw new IllegalStateException(
          "Could not find field with name '" + name + "'.");
      } else {
        row.setField(i, null);
      }
    } else {
      row.setField(i, convert(subNode, types[i]));
    }
  }
  return row;
}

代码示例来源:origin: apache/flink

@Test
public void getTaskmanagers() throws Exception {
  String json = TestBaseUtils.getFromHTTP("http://localhost:" + getRestPort() + "/taskmanagers/");
  ObjectMapper mapper = new ObjectMapper();
  JsonNode parsed = mapper.readTree(json);
  ArrayNode taskManagers = (ArrayNode) parsed.get("taskmanagers");
  assertNotNull(taskManagers);
  assertEquals(NUM_TASK_MANAGERS, taskManagers.size());
  JsonNode taskManager = taskManagers.get(0);
  assertNotNull(taskManager);
  assertEquals(NUM_SLOTS, taskManager.get("slotsNumber").asInt());
  assertTrue(taskManager.get("freeSlots").asInt() <= NUM_SLOTS);
}

代码示例来源:origin: apache/flink

@Test
public void getTaskManagerLogAndStdoutFiles() {
  try {
    String json = TestBaseUtils.getFromHTTP("http://localhost:" + getRestPort() + "/taskmanagers/");
    ObjectMapper mapper = new ObjectMapper();
    JsonNode parsed = mapper.readTree(json);
    ArrayNode taskManagers = (ArrayNode) parsed.get("taskmanagers");
    JsonNode taskManager = taskManagers.get(0);
    String id = taskManager.get("id").asText();
    WebMonitorUtils.LogFileLocation logFiles = WebMonitorUtils.LogFileLocation.find(CLUSTER_CONFIGURATION);
    //we check for job manager log files, since no separate taskmanager logs exist
    FileUtils.writeStringToFile(logFiles.logFile, "job manager log");
    String logs = TestBaseUtils.getFromHTTP("http://localhost:" + getRestPort() + "/taskmanagers/" + id + "/log");
    assertTrue(logs.contains("job manager log"));
    FileUtils.writeStringToFile(logFiles.stdOutFile, "job manager out");
    logs = TestBaseUtils.getFromHTTP("http://localhost:" + getRestPort() + "/taskmanagers/" + id + "/stdout");
    assertTrue(logs.contains("job manager out"));
  } catch (Exception e) {
    e.printStackTrace();
    fail(e.getMessage());
  }
}

代码示例来源:origin: apache/flink

@Test
public void getNumberOfTaskManagers() {
  try {
    String json = TestBaseUtils.getFromHTTP("http://localhost:" + getRestPort() + "/taskmanagers/");
    ObjectMapper mapper = new ObjectMapper();
    JsonNode response = mapper.readTree(json);
    ArrayNode taskManagers = (ArrayNode) response.get("taskmanagers");
    assertNotNull(taskManagers);
    assertEquals(NUM_TASK_MANAGERS, taskManagers.size());
  } catch (Exception e) {
    e.printStackTrace();
    fail(e.getMessage());
  }
}

代码示例来源:origin: apache/flink

JsonNode rootNode = m.readTree(json);
JsonNode idField = rootNode.get("jid");
JsonNode nameField = rootNode.get("name");
JsonNode arrayField = rootNode.get("nodes");
  JsonNode vertex = iter.next();
  JsonNode vertexIdField = vertex.get("id");
  JsonNode parallelismField = vertex.get("parallelism");
  JsonNode contentsFields = vertex.get("description");
  JsonNode operatorField = vertex.get("operator");
  JsonNode inputsField = node.get("inputs");
  if (inputsField != null) {
    Iterator<JsonNode> inputsIter = inputsField.elements();
    while (inputsIter.hasNext()) {
      JsonNode inputNode = inputsIter.next();
      JsonNode inputIdField = inputNode.get("id");

代码示例来源:origin: apache/flink

@Test
  public void testDeserializeWithMetadata() throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode initialKey = mapper.createObjectNode();
    initialKey.put("index", 4);
    byte[] serializedKey = mapper.writeValueAsBytes(initialKey);

    ObjectNode initialValue = mapper.createObjectNode();
    initialValue.put("word", "world");
    byte[] serializedValue = mapper.writeValueAsBytes(initialValue);

    JSONKeyValueDeserializationSchema schema = new JSONKeyValueDeserializationSchema(true);
    ObjectNode deserializedValue = schema.deserialize(serializedKey, serializedValue, "topic#1", 3, 4);

    Assert.assertEquals(4, deserializedValue.get("key").get("index").asInt());
    Assert.assertEquals("world", deserializedValue.get("value").get("word").asText());
    Assert.assertEquals("topic#1", deserializedValue.get("metadata").get("topic").asText());
    Assert.assertEquals(4, deserializedValue.get("metadata").get("offset").asInt());
    Assert.assertEquals(3, deserializedValue.get("metadata").get("partition").asInt());
  }
}

代码示例来源:origin: apache/flink

@Test
public void testDeserializeWithoutMetadata() throws IOException {
  ObjectMapper mapper = new ObjectMapper();
  ObjectNode initialKey = mapper.createObjectNode();
  initialKey.put("index", 4);
  byte[] serializedKey = mapper.writeValueAsBytes(initialKey);
  ObjectNode initialValue = mapper.createObjectNode();
  initialValue.put("word", "world");
  byte[] serializedValue = mapper.writeValueAsBytes(initialValue);
  JSONKeyValueDeserializationSchema schema = new JSONKeyValueDeserializationSchema(false);
  ObjectNode deserializedValue = schema.deserialize(serializedKey, serializedValue, "", 0, 0);
  Assert.assertTrue(deserializedValue.get("metadata") == null);
  Assert.assertEquals(4, deserializedValue.get("key").get("index").asInt());
  Assert.assertEquals("world", deserializedValue.get("value").get("word").asText());
}

代码示例来源:origin: apache/flink

@Test
public void testDeserializeWithoutKey() throws IOException {
  ObjectMapper mapper = new ObjectMapper();
  byte[] serializedKey = null;
  ObjectNode initialValue = mapper.createObjectNode();
  initialValue.put("word", "world");
  byte[] serializedValue = mapper.writeValueAsBytes(initialValue);
  JSONKeyValueDeserializationSchema schema = new JSONKeyValueDeserializationSchema(false);
  ObjectNode deserializedValue = schema.deserialize(serializedKey, serializedValue, "", 0, 0);
  Assert.assertTrue(deserializedValue.get("metadata") == null);
  Assert.assertTrue(deserializedValue.get("key") == null);
  Assert.assertEquals("world", deserializedValue.get("value").get("word").asText());
}

代码示例来源:origin: apache/flink

@Test
public void testDeserializeWithoutValue() throws IOException {
  ObjectMapper mapper = new ObjectMapper();
  ObjectNode initialKey = mapper.createObjectNode();
  initialKey.put("index", 4);
  byte[] serializedKey = mapper.writeValueAsBytes(initialKey);
  byte[] serializedValue = null;
  JSONKeyValueDeserializationSchema schema = new JSONKeyValueDeserializationSchema(false);
  ObjectNode deserializedValue = schema.deserialize(serializedKey, serializedValue, "", 0, 0);
  Assert.assertTrue(deserializedValue.get("metadata") == null);
  Assert.assertEquals(4, deserializedValue.get("key").get("index").asInt());
  Assert.assertTrue(deserializedValue.get("value") == null);
}

代码示例来源:origin: com.alibaba.blink/flink-runtime

@Override
public SerializedThrowable deserialize(
    final JsonParser p,
    final DeserializationContext ctxt) throws IOException {
  final JsonNode root = p.readValueAsTree();
  final byte[] serializedException = root.get(FIELD_NAME_SERIALIZED_THROWABLE).binaryValue();
  try {
    return InstantiationUtil.deserializeObject(serializedException, ClassLoader.getSystemClassLoader());
  } catch (ClassNotFoundException e) {
    throw new IOException("Failed to deserialize " + SerializedThrowable.class.getCanonicalName(), e);
  }
}

代码示例来源:origin: org.apache.flink/flink-runtime_2.11

@Override
public SerializedThrowable deserialize(
    final JsonParser p,
    final DeserializationContext ctxt) throws IOException {
  final JsonNode root = p.readValueAsTree();
  final byte[] serializedException = root.get(FIELD_NAME_SERIALIZED_THROWABLE).binaryValue();
  try {
    return InstantiationUtil.deserializeObject(serializedException, ClassLoader.getSystemClassLoader());
  } catch (ClassNotFoundException e) {
    throw new IOException("Failed to deserialize " + SerializedThrowable.class.getCanonicalName(), e);
  }
}

代码示例来源:origin: org.apache.flink/flink-runtime

@Override
public SerializedThrowable deserialize(
    final JsonParser p,
    final DeserializationContext ctxt) throws IOException {
  final JsonNode root = p.readValueAsTree();
  final byte[] serializedException = root.get(FIELD_NAME_SERIALIZED_THROWABLE).binaryValue();
  try {
    return InstantiationUtil.deserializeObject(serializedException, ClassLoader.getSystemClassLoader());
  } catch (ClassNotFoundException e) {
    throw new IOException("Failed to deserialize " + SerializedThrowable.class.getCanonicalName(), e);
  }
}

代码示例来源:origin: org.apache.flink/flink-json

private Object convertObjectArray(JsonNode node, TypeInformation<?> elementType) {
  final Object[] array = (Object[]) Array.newInstance(elementType.getTypeClass(), node.size());
  for (int i = 0; i < node.size(); i++) {
    array[i] = convert(node.get(i), elementType);
  }
  return array;
}

相关文章