Search in sources :

Example 1 with ObjectNode

use of com.fasterxml.jackson.databind.node.ObjectNode in project kafka by apache.

the class PageViewUntypedDemo method main.

public static void main(String[] args) throws Exception {
    Properties props = new Properties();
    props.put(StreamsConfig.APPLICATION_ID_CONFIG, "streams-pageview-untyped");
    props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
    props.put(StreamsConfig.TIMESTAMP_EXTRACTOR_CLASS_CONFIG, JsonTimestampExtractor.class);
    // setting offset reset to earliest so that we can re-run the demo code with the same pre-loaded data
    props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
    KStreamBuilder builder = new KStreamBuilder();
    final Serializer<JsonNode> jsonSerializer = new JsonSerializer();
    final Deserializer<JsonNode> jsonDeserializer = new JsonDeserializer();
    final Serde<JsonNode> jsonSerde = Serdes.serdeFrom(jsonSerializer, jsonDeserializer);
    KStream<String, JsonNode> views = builder.stream(Serdes.String(), jsonSerde, "streams-pageview-input");
    KTable<String, JsonNode> users = builder.table(Serdes.String(), jsonSerde, "streams-userprofile-input", "streams-userprofile-store-name");
    KTable<String, String> userRegions = users.mapValues(new ValueMapper<JsonNode, String>() {

        @Override
        public String apply(JsonNode record) {
            return record.get("region").textValue();
        }
    });
    KStream<JsonNode, JsonNode> regionCount = views.leftJoin(userRegions, new ValueJoiner<JsonNode, String, JsonNode>() {

        @Override
        public JsonNode apply(JsonNode view, String region) {
            ObjectNode jNode = JsonNodeFactory.instance.objectNode();
            return jNode.put("user", view.get("user").textValue()).put("page", view.get("page").textValue()).put("region", region == null ? "UNKNOWN" : region);
        }
    }).map(new KeyValueMapper<String, JsonNode, KeyValue<String, JsonNode>>() {

        @Override
        public KeyValue<String, JsonNode> apply(String user, JsonNode viewRegion) {
            return new KeyValue<>(viewRegion.get("region").textValue(), viewRegion);
        }
    }).groupByKey(Serdes.String(), jsonSerde).count(TimeWindows.of(7 * 24 * 60 * 60 * 1000L).advanceBy(1000), "RollingSevenDaysOfPageViewsByRegion").toStream().map(new KeyValueMapper<Windowed<String>, Long, KeyValue<JsonNode, JsonNode>>() {

        @Override
        public KeyValue<JsonNode, JsonNode> apply(Windowed<String> key, Long value) {
            ObjectNode keyNode = JsonNodeFactory.instance.objectNode();
            keyNode.put("window-start", key.window().start()).put("region", key.key());
            ObjectNode valueNode = JsonNodeFactory.instance.objectNode();
            valueNode.put("count", value);
            return new KeyValue<>((JsonNode) keyNode, (JsonNode) valueNode);
        }
    });
    // write to the result topic
    regionCount.to(jsonSerde, jsonSerde, "streams-pageviewstats-untyped-output");
    KafkaStreams streams = new KafkaStreams(builder, props);
    streams.start();
    // usually the stream application would be running forever,
    // in this example we just let it run for some time and stop since the input data is finite.
    Thread.sleep(5000L);
    streams.close();
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonSerializer(org.apache.kafka.connect.json.JsonSerializer) Properties(java.util.Properties) JsonDeserializer(org.apache.kafka.connect.json.JsonDeserializer) ValueJoiner(org.apache.kafka.streams.kstream.ValueJoiner) KStreamBuilder(org.apache.kafka.streams.kstream.KStreamBuilder) KafkaStreams(org.apache.kafka.streams.KafkaStreams) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Windowed(org.apache.kafka.streams.kstream.Windowed)

Example 2 with ObjectNode

use of com.fasterxml.jackson.databind.node.ObjectNode in project native-navigation by airbnb.

the class ConversionUtil method toJsonObject.

/** Converts a {@link ReadableMap} into an Json {@link ObjectNode} */
static ObjectNode toJsonObject(ReadableMap readableMap) {
    JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
    ObjectNode result = nodeFactory.objectNode();
    ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
    while (iterator.hasNextKey()) {
        String key = iterator.nextKey();
        ReadableType type = readableMap.getType(key);
        switch(type) {
            case Null:
                result.putNull(key);
                break;
            case Boolean:
                result.put(key, readableMap.getBoolean(key));
                break;
            case Number:
                result.put(key, readableMap.getDouble(key));
                break;
            case String:
                result.put(key, readableMap.getString(key));
                break;
            case Map:
                result.set(key, toJsonObject(readableMap.getMap(key)));
                break;
            case Array:
                result.set(key, toJsonArray(readableMap.getArray(key)));
                break;
            default:
                Log.e(TAG, "Could not convert object with key: " + key + ".");
        }
    }
    return result;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory)

Example 3 with ObjectNode

use of com.fasterxml.jackson.databind.node.ObjectNode in project native-navigation by airbnb.

the class ConversionUtil method toType.

/** Converts the provided {@code readableMap} into an object of the provided {@code targetType} */
static <T> T toType(ObjectMapper objectMapper, ReadableMap readableMap, Class<T> targetType) {
    ObjectNode jsonNode = toJsonObject(readableMap);
    ObjectReader objectReader = JacksonUtils.readerForType(objectMapper, targetType);
    //noinspection OverlyBroadCatchBlock
    try {
        return objectReader.readValue(jsonNode);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) IOException(java.io.IOException)

Example 4 with ObjectNode

use of com.fasterxml.jackson.databind.node.ObjectNode in project jsonschema2pojo by joelittlejohn.

the class TypeRuleTest method applyGeneratesIntegerUsingJavaTypeLongWhenMaximumGreaterThanIntegerMax.

@Test
public void applyGeneratesIntegerUsingJavaTypeLongWhenMaximumGreaterThanIntegerMax() {
    JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());
    ObjectNode objectNode = new ObjectMapper().createObjectNode();
    objectNode.put("type", "integer");
    objectNode.put("maximum", Integer.MAX_VALUE + 1L);
    when(config.isUsePrimitives()).thenReturn(false);
    JType result = rule.apply("fooBar", objectNode, jpackage, null);
    assertThat(result.fullName(), is(Long.class.getName()));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JPackage(com.sun.codemodel.JPackage) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JType(com.sun.codemodel.JType) Test(org.junit.Test)

Example 5 with ObjectNode

use of com.fasterxml.jackson.databind.node.ObjectNode in project jsonschema2pojo by joelittlejohn.

the class TypeRuleTest method applyGeneratesIntegerUsingJavaTypeBigInteger.

@Test
public void applyGeneratesIntegerUsingJavaTypeBigInteger() {
    JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());
    ObjectNode objectNode = new ObjectMapper().createObjectNode();
    objectNode.put("type", "integer");
    objectNode.put("javaType", "java.math.BigInteger");
    JType result = rule.apply("fooBar", objectNode, jpackage, null);
    assertThat(result.fullName(), is("java.math.BigInteger"));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JPackage(com.sun.codemodel.JPackage) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JType(com.sun.codemodel.JType) Test(org.junit.Test)

Aggregations

ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)702 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)174 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)157 JsonNode (com.fasterxml.jackson.databind.JsonNode)142 Test (org.junit.Test)111 StringEntity (org.apache.http.entity.StringEntity)88 Deployment (org.activiti.engine.test.Deployment)84 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)67 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)64 Task (org.activiti.engine.task.Task)49 HttpPost (org.apache.http.client.methods.HttpPost)49 JCodeModel (com.sun.codemodel.JCodeModel)45 JPackage (com.sun.codemodel.JPackage)44 HttpPut (org.apache.http.client.methods.HttpPut)40 JType (com.sun.codemodel.JType)39 Cluster (org.apache.geode.tools.pulse.internal.data.Cluster)39 IOException (java.io.IOException)38 HashMap (java.util.HashMap)38 ArrayList (java.util.ArrayList)28 Map (java.util.Map)28