Java Code Examples for org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper

The following examples show how to use org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test
public void testDeserializeWithoutValue() throws Exception {
	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(newConsumerRecord(serializedKey, serializedValue));

	Assert.assertTrue(deserializedValue.get("metadata") == null);
	Assert.assertEquals(4, deserializedValue.get("key").get("index").asInt());
	Assert.assertTrue(deserializedValue.get("value") == null);
}
 
Example 2
@Test
public void testDeserializeWithMetadata() throws Exception {
	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);
	final ConsumerRecord<byte[], byte[]> consumerRecord =
			newConsumerRecord("topic#1", 3, 4L, serializedKey, serializedValue);
	ObjectNode deserializedValue = schema.deserialize(consumerRecord);

	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());
}
 
Example 3
Source Project: Flink-CEPplus   Source File: WebFrontendITCase.java    License: Apache License 2.0 6 votes vote down vote up
@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());
	}
}
 
Example 4
Source Project: Flink-CEPplus   Source File: WebFrontendITCase.java    License: Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 5
Source Project: Flink-CEPplus   Source File: WebFrontendITCase.java    License: Apache License 2.0 6 votes vote down vote up
@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());
	}
}
 
Example 6
Source Project: Flink-CEPplus   Source File: WebMonitorUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static Map<String, String> fromKeyValueJsonArray(String jsonString) {
	try {
		Map<String, String> map = new HashMap<>();
		ObjectMapper m = new ObjectMapper();
		ArrayNode array = (ArrayNode) m.readTree(jsonString);

		Iterator<JsonNode> elements = array.elements();
		while (elements.hasNext()) {
			JsonNode node = elements.next();
			String key = node.get("key").asText();
			String value = node.get("value").asText();
			map.put(key, value);
		}

		return map;
	}
	catch (Exception e) {
		throw new RuntimeException(e.getMessage(), e);
	}
}
 
Example 7
Source Project: Flink-CEPplus   Source File: JobDetailsTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Tests that we can marshal and unmarshal JobDetails instances.
 */
@Test
public void testJobDetailsMarshalling() throws JsonProcessingException {
	final JobDetails expected = new JobDetails(
		new JobID(),
		"foobar",
		1L,
		10L,
		9L,
		JobStatus.RUNNING,
		8L,
		new int[]{1, 3, 3, 7, 4, 2, 7, 3, 3},
		42);

	final ObjectMapper objectMapper = RestMapperUtils.getStrictObjectMapper();

	final JsonNode marshalled = objectMapper.valueToTree(expected);

	final JobDetails unmarshalled = objectMapper.treeToValue(marshalled, JobDetails.class);

	assertEquals(expected, unmarshalled);
}
 
Example 8
/**
 * Tests that we can marshal and unmarshal the response.
 */
@Test
public void testJsonMarshalling() throws Exception {
	final R expected = getTestResponseInstance();

	ObjectMapper objectMapper = RestMapperUtils.getStrictObjectMapper();
	final String marshalled = objectMapper.writeValueAsString(expected);

	final Collection<Class<?>> typeParameters = getTypeParameters();
	final JavaType type;

	if (typeParameters.isEmpty()) {
		type = objectMapper.getTypeFactory().constructType(getTestResponseClass());
	} else {
		type = objectMapper.getTypeFactory().constructParametricType(
			getTestResponseClass(),
			typeParameters.toArray(new Class<?>[typeParameters.size()]));
	}

	final R unmarshalled = objectMapper.readValue(marshalled, type);
	assertOriginalEqualsToUnmarshalled(expected, unmarshalled);
}
 
Example 9
@Test
public void testDeserializeWithoutKey() throws Exception {
	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(newConsumerRecord(serializedKey, serializedValue));

	Assert.assertTrue(deserializedValue.get("metadata") == null);
	Assert.assertTrue(deserializedValue.get("key") == null);
	Assert.assertEquals("world", deserializedValue.get("value").get("word").asText());
}
 
Example 10
@Test
public void testDeserializeWithoutValue() throws Exception {
	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(newConsumerRecord(serializedKey, serializedValue));

	Assert.assertTrue(deserializedValue.get("metadata") == null);
	Assert.assertEquals(4, deserializedValue.get("key").get("index").asInt());
	Assert.assertTrue(deserializedValue.get("value") == null);
}
 
Example 11
Source Project: flink   Source File: JsonRowSchemaConverter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Converts a JSON schema into Flink's type information. Throws an exception if the schema
 * cannot converted because of loss of precision or too flexible schema.
 *
 * <p>The converter can resolve simple schema references to solve those cases where entities
 * are defined at the beginning and then used throughout a document.
 */
@SuppressWarnings("unchecked")
public static <T> TypeInformation<T> convert(String jsonSchema) {
	Preconditions.checkNotNull(jsonSchema, "JSON schema");
	final ObjectMapper mapper = new ObjectMapper();
	mapper.getFactory()
		.enable(JsonParser.Feature.ALLOW_COMMENTS)
		.enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES)
		.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
	final JsonNode node;
	try {
		node = mapper.readTree(jsonSchema);
	} catch (IOException e) {
		throw new IllegalArgumentException(
			"Invalid JSON schema.", e);
	}
	return (TypeInformation<T>) convertType("<root>", node, node);
}
 
Example 12
Source Project: flink   Source File: JsonRowDeserializationSchema.java    License: Apache License 2.0 6 votes vote down vote up
private Object convertField(
	ObjectMapper mapper,
	DeserializationRuntimeConverter fieldConverter,
	String fieldName,
	JsonNode field) {
	if (field == null) {
		if (failOnMissingField) {
			throw new IllegalStateException(
				"Could not find field with name '" + fieldName + "'.");
		} else {
			return null;
		}
	} else {
		return fieldConverter.convert(mapper, field);
	}
}
 
Example 13
Source Project: flink   Source File: WebFrontendITCase.java    License: Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 14
Source Project: flink   Source File: WebFrontendITCase.java    License: Apache License 2.0 6 votes vote down vote up
@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());
	}
}
 
Example 15
Source Project: flink   Source File: WebFrontendITCase.java    License: Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 16
Source Project: flink   Source File: WebFrontendITCase.java    License: Apache License 2.0 6 votes vote down vote up
@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());
	}
}
 
Example 17
Source Project: flink   Source File: WebMonitorUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static Map<String, String> fromKeyValueJsonArray(String jsonString) {
	try {
		Map<String, String> map = new HashMap<>();
		ObjectMapper m = new ObjectMapper();
		ArrayNode array = (ArrayNode) m.readTree(jsonString);

		Iterator<JsonNode> elements = array.elements();
		while (elements.hasNext()) {
			JsonNode node = elements.next();
			String key = node.get("key").asText();
			String value = node.get("value").asText();
			map.put(key, value);
		}

		return map;
	}
	catch (Exception e) {
		throw new RuntimeException(e.getMessage(), e);
	}
}
 
Example 18
Source Project: flink   Source File: RestResponseMarshallingTestBase.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Tests that we can marshal and unmarshal the response.
 */
@Test
public void testJsonMarshalling() throws Exception {
	final R expected = getTestResponseInstance();

	ObjectMapper objectMapper = RestMapperUtils.getStrictObjectMapper();
	final String marshalled = objectMapper.writeValueAsString(expected);

	final Collection<Class<?>> typeParameters = getTypeParameters();
	final JavaType type;

	if (typeParameters.isEmpty()) {
		type = objectMapper.getTypeFactory().constructType(getTestResponseClass());
	} else {
		type = objectMapper.getTypeFactory().constructParametricType(
			getTestResponseClass(),
			typeParameters.toArray(new Class<?>[typeParameters.size()]));
	}

	final R unmarshalled = objectMapper.readValue(marshalled, type);
	assertOriginalEqualsToUnmarshalled(expected, unmarshalled);
}
 
Example 19
Source Project: flink   Source File: JsonRowDeserializationSchema.java    License: Apache License 2.0 6 votes vote down vote up
private LocalDateTime convertToLocalDateTime(ObjectMapper mapper, JsonNode jsonNode) {
	// according to RFC 3339 every date-time must have a timezone;
	// until we have full timezone support, we only support UTC;
	// users can parse their time as string as a workaround
	TemporalAccessor parsedTimestamp = RFC3339_TIMESTAMP_FORMAT.parse(jsonNode.asText());

	ZoneOffset zoneOffset = parsedTimestamp.query(TemporalQueries.offset());

	if (zoneOffset != null && zoneOffset.getTotalSeconds() != 0) {
		throw new IllegalStateException(
			"Invalid timestamp format. Only a timestamp in UTC timezone is supported yet. " +
				"Format: yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
	}

	LocalTime localTime = parsedTimestamp.query(TemporalQueries.localTime());
	LocalDate localDate = parsedTimestamp.query(TemporalQueries.localDate());

	return LocalDateTime.of(localDate, localTime);
}
 
Example 20
Source Project: flink   Source File: JsonRowDeserializationSchema.java    License: Apache License 2.0 6 votes vote down vote up
private LocalTime convertToLocalTime(ObjectMapper mapper, JsonNode jsonNode) {
	// according to RFC 3339 every full-time must have a timezone;
	// until we have full timezone support, we only support UTC;
	// users can parse their time as string as a workaround

	TemporalAccessor parsedTime = RFC3339_TIME_FORMAT.parse(jsonNode.asText());

	ZoneOffset zoneOffset = parsedTime.query(TemporalQueries.offset());
	LocalTime localTime = parsedTime.query(TemporalQueries.localTime());

	if (zoneOffset != null && zoneOffset.getTotalSeconds() != 0 || localTime.getNano() != 0) {
		throw new IllegalStateException(
			"Invalid time format. Only a time in UTC timezone without milliseconds is supported yet.");
	}

	return localTime;
}
 
Example 21
Source Project: flink   Source File: JsonRowDataSerDeSchemaTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSerDeSQLTimestampFormat() throws Exception{
	RowType rowType = (RowType) ROW(
		FIELD("timestamp3", TIMESTAMP(3)),
		FIELD("timestamp9", TIMESTAMP(9))
	).getLogicalType();

	JsonRowDataDeserializationSchema deserializationSchema = new JsonRowDataDeserializationSchema(
		rowType, new RowDataTypeInfo(rowType), false, false, TimestampFormat.SQL);
	JsonRowDataSerializationSchema serializationSchema = new JsonRowDataSerializationSchema(rowType, TimestampFormat.SQL);

	ObjectMapper objectMapper = new ObjectMapper();

	ObjectNode root = objectMapper.createObjectNode();
	root.put("timestamp3", "1990-10-14 12:12:43.123");
	root.put("timestamp9", "1990-10-14 12:12:43.123456789");
	byte[] serializedJson = objectMapper.writeValueAsBytes(root);
	RowData rowData = deserializationSchema.deserialize(serializedJson);
	byte[] actual = serializationSchema.serialize(rowData);
	assertEquals(new String(serializedJson), new String(actual));
}
 
Example 22
@Test
public void testDeserializeWithoutKey() throws Exception {
	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(newConsumerRecord(serializedKey, serializedValue));

	Assert.assertTrue(deserializedValue.get("metadata") == null);
	Assert.assertTrue(deserializedValue.get("key") == null);
	Assert.assertEquals("world", deserializedValue.get("value").get("word").asText());
}
 
Example 23
@Test
public void testDeserializeWithoutValue() throws Exception {
	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(newConsumerRecord(serializedKey, serializedValue));

	Assert.assertTrue(deserializedValue.get("metadata") == null);
	Assert.assertEquals(4, deserializedValue.get("key").get("index").asInt());
	Assert.assertTrue(deserializedValue.get("value") == null);
}
 
Example 24
@Test
public void testDeserializeWithMetadata() throws Exception {
	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);
	final ConsumerRecord<byte[], byte[]> consumerRecord =
			newConsumerRecord("topic#1", 3, 4L, serializedKey, serializedValue);
	ObjectNode deserializedValue = schema.deserialize(consumerRecord);

	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());
}
 
Example 25
Source Project: flink   Source File: JsonRowSchemaConverter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Converts a JSON schema into Flink's type information. Throws an exception if the schema
 * cannot converted because of loss of precision or too flexible schema.
 *
 * <p>The converter can resolve simple schema references to solve those cases where entities
 * are defined at the beginning and then used throughout a document.
 */
@SuppressWarnings("unchecked")
public static <T> TypeInformation<T> convert(String jsonSchema) {
	Preconditions.checkNotNull(jsonSchema, "JSON schema");
	final ObjectMapper mapper = new ObjectMapper();
	mapper.getFactory()
		.enable(JsonParser.Feature.ALLOW_COMMENTS)
		.enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES)
		.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
	final JsonNode node;
	try {
		node = mapper.readTree(jsonSchema);
	} catch (IOException e) {
		throw new IllegalArgumentException(
			"Invalid JSON schema.", e);
	}
	return (TypeInformation<T>) convertType("<root>", node, node);
}
 
Example 26
/**
 * Tests simple deserialization using type information.
 */
@Test
public void testTypeInfoDeserialization() throws Exception {
	long id = 1238123899121L;
	String name = "asdlkjasjkdla998y1122";
	byte[] bytes = new byte[1024];
	ThreadLocalRandom.current().nextBytes(bytes);

	ObjectMapper objectMapper = new ObjectMapper();

	// Root
	ObjectNode root = objectMapper.createObjectNode();
	root.put("id", id);
	root.put("name", name);
	root.put("bytes", bytes);

	byte[] serializedJson = objectMapper.writeValueAsBytes(root);

	JsonRowDeserializationSchema deserializationSchema = new JsonRowDeserializationSchema(
		Types.ROW_NAMED(
			new String[] { "id", "name", "bytes" },
			Types.LONG, Types.STRING, Types.PRIMITIVE_ARRAY(Types.BYTE))
	);

	Row deserialized = deserializationSchema.deserialize(serializedJson);

	assertEquals(3, deserialized.getArity());
	assertEquals(id, deserialized.getField(0));
	assertEquals(name, deserialized.getField(1));
	assertArrayEquals(bytes, (byte[]) deserialized.getField(2));
}
 
Example 27
Source Project: flink   Source File: WebFrontendITCase.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void getNumberOfTaskManagers() throws Exception {
	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());
}
 
Example 28
/**
 * Tests deserialization with non-existing field name.
 */
@Test
public void testMissingNode() throws Exception {
	ObjectMapper objectMapper = new ObjectMapper();

	// Root
	ObjectNode root = objectMapper.createObjectNode();
	root.put("id", 123123123);
	byte[] serializedJson = objectMapper.writeValueAsBytes(root);

	JsonRowDeserializationSchema deserializationSchema = new JsonRowDeserializationSchema(
		Types.ROW_NAMED(
			new String[] { "name" },
			Types.STRING)
	);

	Row row = deserializationSchema.deserialize(serializedJson);

	assertEquals(1, row.getArity());
	Assert.assertNull("Missing field not null", row.getField(0));

	deserializationSchema.setFailOnMissingField(true);

	try {
		deserializationSchema.deserialize(serializedJson);
		Assert.fail("Did not throw expected Exception");
	} catch (IOException e) {
		Assert.assertTrue(e.getCause() instanceof IllegalStateException);
	}
}
 
Example 29
@Test
public void testDeserialize() throws IOException {
	ObjectMapper mapper = new ObjectMapper();
	ObjectNode initialValue = mapper.createObjectNode();
	initialValue.put("key", 4).put("value", "world");
	byte[] serializedValue = mapper.writeValueAsBytes(initialValue);

	JsonNodeDeserializationSchema schema = new JsonNodeDeserializationSchema();
	ObjectNode deserializedValue = schema.deserialize(serializedValue);

	assertEquals(4, deserializedValue.get("key").asInt());
	assertEquals("world", deserializedValue.get("value").asText());
}
 
Example 30
Source Project: Flink-CEPplus   Source File: HistoryServerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testHistoryServerIntegration() throws Exception {
	final int numJobs = 2;
	for (int x = 0; x < numJobs; x++) {
		runJob();
	}
	createLegacyArchive(jmDirectory.toPath());

	CountDownLatch numFinishedPolls = new CountDownLatch(1);

	Configuration historyServerConfig = new Configuration();
	historyServerConfig.setString(HistoryServerOptions.HISTORY_SERVER_ARCHIVE_DIRS, jmDirectory.toURI().toString());
	historyServerConfig.setString(HistoryServerOptions.HISTORY_SERVER_WEB_DIR, hsDirectory.getAbsolutePath());

	historyServerConfig.setInteger(HistoryServerOptions.HISTORY_SERVER_WEB_PORT, 0);

	// the job is archived asynchronously after env.execute() returns
	File[] archives = jmDirectory.listFiles();
	while (archives == null || archives.length != numJobs + 1) {
		Thread.sleep(50);
		archives = jmDirectory.listFiles();
	}

	HistoryServer hs = new HistoryServer(historyServerConfig, numFinishedPolls);
	try {
		hs.start();
		String baseUrl = "http://localhost:" + hs.getWebPort();
		numFinishedPolls.await(10L, TimeUnit.SECONDS);

		ObjectMapper mapper = new ObjectMapper();
		String response = getFromHTTP(baseUrl + JobsOverviewHeaders.URL);
		MultipleJobsDetails overview = mapper.readValue(response, MultipleJobsDetails.class);

		Assert.assertEquals(numJobs + 1, overview.getJobs().size());
	} finally {
		hs.stop();
	}
}