本文整理了Java中org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode
类的一些代码示例,展示了JsonNode
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JsonNode
类的具体详情如下:
包路径:org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode
类名称:JsonNode
暂无
代码示例来源: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
JsonNode idField = rootNode.get("jid");
JsonNode nameField = rootNode.get("name");
JsonNode arrayField = rootNode.get("nodes");
assertTrue(idField.isTextual());
assertTrue(nameField.isTextual());
assertTrue(arrayField.isArray());
JsonNode vertexIdField = vertex.get("id");
JsonNode parallelismField = vertex.get("parallelism");
JsonNode contentsFields = vertex.get("description");
JsonNode operatorField = vertex.get("operator");
assertTrue(vertexIdField.isTextual());
assertNotNull(parallelismField);
assertTrue(parallelismField.isNumber());
assertNotNull(contentsFields);
assertTrue(contentsFields.isTextual());
assertNotNull(operatorField);
assertTrue(operatorField.isTextual());
if (contentsFields.asText().startsWith("Sync")) {
assertEquals(1, parallelismField.asInt());
assertEquals(expectedParallelism, parallelismField.asInt());
idToNode.put(vertexIdField.asText(), vertex);
JsonNode inputsField = node.get("inputs");
代码示例来源:origin: apache/flink
private Object convertByteArray(JsonNode node) {
try {
return node.binaryValue();
} catch (IOException e) {
throw new RuntimeException("Unable to deserialize byte array.", e);
}
}
}
代码示例来源:origin: apache/flink
private static TypeInformation<?> convertStringFormat(String location, JsonNode node) {
if (!node.isTextual()) {
throw new IllegalArgumentException("Invalid '" + FORMAT + "' property in node: " + location);
}
switch (node.asText()) {
case FORMAT_DATE:
return Types.SQL_DATE;
case FORMAT_TIME:
return Types.SQL_TIME;
case FORMAT_DATE_TIME:
return Types.SQL_TIMESTAMP;
default:
return Types.STRING; // unlikely that we will support other formats in the future
}
}
代码示例来源: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<?>[] convertTypes(String location, JsonNode arrayNode, JsonNode root) {
final TypeInformation<?>[] types = new TypeInformation[arrayNode.size()];
final Iterator<JsonNode> elements = arrayNode.elements();
int i = 0;
while (elements.hasNext()) {
final TypeInformation<?> elementType = convertType(
location + '[' + i + ']',
elements.next(),
root);
types[i] = elementType;
i += 1;
}
return types;
}
}
代码示例来源: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
if (node.has(REF) && node.get(REF).isTextual()) {
ref = Optional.of(resolveReference(node.get(REF).asText(), node, root));
} else {
ref = Optional.empty();
if (node.has(TYPE)) {
final JsonNode typeNode = node.get(TYPE);
if (typeNode.isArray()) {
final Iterator<JsonNode> elements = typeNode.elements();
while (elements.hasNext()) {
types.add(elements.next().asText());
else if (typeNode.isTextual()) {
types.add(typeNode.asText());
break;
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);
代码示例来源: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 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
@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: org.apache.flink/flink-runtime
@Override
public JobDetails deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
JsonNode rootNode = jsonParser.readValueAsTree();
JobID jobId = JobID.fromHexString(rootNode.get(FIELD_NAME_JOB_ID).textValue());
String jobName = rootNode.get(FIELD_NAME_JOB_NAME).textValue();
long startTime = rootNode.get(FIELD_NAME_START_TIME).longValue();
long endTime = rootNode.get(FIELD_NAME_END_TIME).longValue();
long duration = rootNode.get(FIELD_NAME_DURATION).longValue();
JobStatus jobStatus = JobStatus.valueOf(rootNode.get(FIELD_NAME_STATUS).textValue());
long lastUpdateTime = rootNode.get(FIELD_NAME_LAST_MODIFICATION).longValue();
JsonNode tasksNode = rootNode.get("tasks");
int numTasks = tasksNode.get(FIELD_NAME_TOTAL_NUMBER_TASKS).intValue();
int[] numVerticesPerExecutionState = new int[ExecutionState.values().length];
for (ExecutionState executionState : ExecutionState.values()) {
numVerticesPerExecutionState[executionState.ordinal()] = tasksNode.get(executionState.name().toLowerCase()).intValue();
}
return new JobDetails(
jobId,
jobName,
startTime,
endTime,
duration,
jobStatus,
lastUpdateTime,
numVerticesPerExecutionState,
numTasks);
}
}
代码示例来源:origin: apache/flink
private Object convert(JsonNode node, TypeInformation<?> info) {
if (info == Types.VOID || node.isNull()) {
return null;
} else if (info == Types.BOOLEAN) {
return node.asBoolean();
} else if (info == Types.STRING) {
return node.asText();
} else if (info == Types.BIG_DEC) {
return node.decimalValue();
} else if (info == Types.BIG_INT) {
return node.bigIntegerValue();
} else if (info == Types.SQL_DATE) {
return Date.valueOf(node.asText());
} else if (info == Types.SQL_TIME) {
final String time = node.asText();
if (time.indexOf('Z') < 0 || time.indexOf('.') >= 0) {
throw new IllegalStateException(
final String timestamp = node.asText();
if (timestamp.indexOf('Z') < 0) {
throw new IllegalStateException(
代码示例来源: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: 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: com.alibaba.blink/flink-runtime
@Override
public ResourceProfile deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
JsonNode rootNode = jsonParser.readValueAsTree();
double cpuCores = rootNode.get(FIELD_NAME_RESOURCE_CPU_CORES).doubleValue();
int heapMemoryInMB = convertByteToMegabyte(rootNode.get(FIELD_NAME_RESOURCE_HEAP_MEMORY).longValue());
int directMemoryInMB = convertByteToMegabyte(rootNode.get(FIELD_NAME_RESOURCE_DIRECT_MEMORY).longValue());
int nativeMemoryInMB = convertByteToMegabyte(rootNode.get(FIELD_NAME_RESOURCE_NATIVE_MEMORY).longValue());
int networkMemoryInMB = convertByteToMegabyte(rootNode.get(FIELD_NAME_RESOURCE_NETWORK_MEMORY).longValue());
int managedMemoryInMB = convertByteToMegabyte(rootNode.get(FIELD_NAME_RESOURCE_MANAGED_MEMORY).longValue());
Map<String, Resource> extendedResources = new HashMap<>();
if (managedMemoryInMB != 0) {
extendedResources.put(ResourceSpec.MANAGED_MEMORY_NAME, new CommonExtendedResource(ResourceSpec.MANAGED_MEMORY_NAME, managedMemoryInMB));
}
return new ResourceProfile(
cpuCores, heapMemoryInMB, directMemoryInMB, nativeMemoryInMB, networkMemoryInMB, extendedResources);
}
代码示例来源: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: org.apache.flink/flink-json
if (node.has(REF) && node.get(REF).isTextual()) {
ref = Optional.of(resolveReference(node.get(REF).asText(), node, root));
} else {
ref = Optional.empty();
if (node.has(TYPE)) {
final JsonNode typeNode = node.get(TYPE);
if (typeNode.isArray()) {
final Iterator<JsonNode> elements = typeNode.elements();
while (elements.hasNext()) {
types.add(elements.next().asText());
else if (typeNode.isTextual()) {
types.add(typeNode.asText());
break;
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);
内容来源于网络,如有侵权,请联系作者删除!