aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-feed-client-api/src/test/java/ai/vespa/feed/client/examples/JsonFileFeederExample.java
blob: b951fb62fb589f51df45e9bf2e5a89106a32ff2d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.feed.client.examples;

import ai.vespa.feed.client.DocumentId;
import ai.vespa.feed.client.FeedClient;
import ai.vespa.feed.client.FeedClientBuilder;
import ai.vespa.feed.client.FeedException;
import ai.vespa.feed.client.JsonFeeder;
import ai.vespa.feed.client.Result;

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;

/**
 * Sample feeder demonstrating how to programmatically feed to a Vespa cluster.
 */
class JsonFileFeederExample implements Closeable {

    private final static Logger log = Logger.getLogger(JsonFileFeederExample.class.getName());

    private final JsonFeeder jsonFeeder;
    private final URI endpoint;

    static class ResultCallBack implements JsonFeeder.ResultCallback {

        final AtomicInteger resultsReceived = new AtomicInteger(0);
        final AtomicInteger errorsReceived = new AtomicInteger(0);
        final long startTimeMillis = System.currentTimeMillis();;

        @Override
        public void onNextResult(Result result, FeedException error) {
            resultsReceived.incrementAndGet();
            if (error != null) {
                log.warning("Problems with feeding document "
                        + error.documentId().map(DocumentId::toString).orElse("<unknown>")
                        + ": " + error);
                errorsReceived.incrementAndGet();
            }
        }

        @Override
        public void onError(FeedException error) {
            log.severe("Feeding failed fatally: " + error.getMessage());
        }

        @Override
        public void onComplete() {
            log.info("Feeding completed");
        }

        void dumpStatsToLog() {
            log.info("Received in total " + resultsReceived.get() + ", " + errorsReceived.get() + " errors.");
            log.info("Time spent receiving is " + (System.currentTimeMillis() - startTimeMillis) + " ms.");
        }

    }

    JsonFileFeederExample(URI endpoint) {
        this.endpoint = endpoint;
        FeedClient feedClient = FeedClientBuilder.create(endpoint)
                .build();
        this.jsonFeeder = JsonFeeder.builder(feedClient)
                .withTimeout(Duration.ofSeconds(30))
                .build();
    }

    /**
     * Feed all operations from a stream.
     *
     * @param stream The input stream to read operations from (JSON array containing one or more document operations).
     */
    void batchFeed(InputStream stream, String batchId) {
        ResultCallBack callback = new ResultCallBack();
        log.info("Starting feed to " + endpoint + " for batch '" + batchId + "'");
        CompletableFuture<Void> promise = jsonFeeder.feedMany(stream, callback);
        promise.join(); // wait for feeding to complete
        callback.dumpStatsToLog();
    }

    @Override
    public void close() throws IOException {
        jsonFeeder.close();
    }
}