Search in sources :

Example 6 with FeedClient

use of com.yahoo.vespa.http.client.FeedClient in project vespa by vespa-engine.

the class Runner method main.

public static void main(String[] args) throws IOException, InterruptedException {
    final CommandLineArguments commandLineArgs = CommandLineArguments.build(args);
    if (commandLineArgs == null) {
        return;
    }
    FormatInputStream formatInputStream = new FormatInputStream(System.in, Optional.ofNullable(commandLineArgs.getFile()), commandLineArgs.getAddRootElementToXml());
    int intervalOfLogging = commandLineArgs.getVerbose() ? commandLineArgs.getWhenVerboseEnabledPrintMessageForEveryXDocuments() : Integer.MAX_VALUE;
    final AtomicInteger numSent = new AtomicInteger(0);
    final SimpleLoggerResultCallback callback = new SimpleLoggerResultCallback(numSent, intervalOfLogging);
    final FeedClient feedClient = FeedClientFactory.create(commandLineArgs.createSessionParams(formatInputStream.getFormat() == FormatInputStream.Format.JSON), callback);
    long sendTotalTimeMs = send(feedClient, formatInputStream.getInputStream(), formatInputStream.getFormat() == FormatInputStream.Format.JSON, numSent, commandLineArgs.getVerbose());
    if (commandLineArgs.getVerbose()) {
        System.err.println(feedClient.getStatsAsJson());
        double fileSizeMb = ((double) new File(commandLineArgs.getFile()).length()) / 1024.0 / 1024.0;
        double transferTimeSec = ((double) sendTotalTimeMs) / 1000.0;
        System.err.println("Sent " + fileSizeMb + " MB in " + transferTimeSec + " seconds.");
        System.err.println("Speed: " + ((fileSizeMb / transferTimeSec) * 8.0) + " Mbits/sec, + HTTP overhead " + "(not taking compression into account)");
        if (transferTimeSec > 0) {
            System.err.printf("Docs/sec %.3f%n\n", numSent.get() / transferTimeSec);
        }
    }
    callback.printProgress();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SimpleLoggerResultCallback(com.yahoo.vespa.http.client.SimpleLoggerResultCallback) FeedClient(com.yahoo.vespa.http.client.FeedClient) File(java.io.File)

Example 7 with FeedClient

use of com.yahoo.vespa.http.client.FeedClient in project vespa by vespa-engine.

the class ExampleUsageFeedClientTest method exampleCode.

// Example usage of FeedClient
public static void exampleCode(String hostNameA, int portServerA, String hostNameB, int portServerB) {
    final boolean useSsl = false;
    final SessionParams sessionParams = new SessionParams.Builder().addCluster(new Cluster.Builder().addEndpoint(Endpoint.create(hostNameA, portServerA, useSsl)).build()).addCluster(new Cluster.Builder().addEndpoint(Endpoint.create(hostNameB, portServerB, useSsl)).build()).setFeedParams(new FeedParams.Builder().setDataFormat(FeedParams.DataFormat.JSON_UTF8).build()).build();
    final AtomicInteger resultsReceived = new AtomicInteger(0);
    final AtomicInteger errorsReceived = new AtomicInteger(0);
    FeedClient feedClient = FeedClientFactory.create(sessionParams, new FeedClient.ResultCallback() {

        @Override
        public void onCompletion(String docId, Result documentResult) {
            resultsReceived.incrementAndGet();
            if (!documentResult.getContext().equals(docId)) {
                System.err.println("Context does not work as expected.");
                errorsReceived.incrementAndGet();
            }
            if (!documentResult.isSuccess()) {
                System.err.println("Problems with docID " + docId + ":" + documentResult.toString());
                errorsReceived.incrementAndGet();
            }
        }
    });
    int sentCounter = 0;
    final List<String> docIds = Arrays.asList("1", "2", "3", "4");
    for (final String docId : docIds) {
        CharSequence docData = generateDocument(docId);
        feedClient.stream(docId, docData, docId);
        sentCounter++;
        System.out.println("Sent " + sentCounter + " received results from " + resultsReceived.get());
    }
    feedClient.close();
    System.out.println("Finished, got " + errorsReceived.get() + " errors from " + resultsReceived.get() + " results, sent " + sentCounter + " documents.");
}
Also used : SessionParams(com.yahoo.vespa.http.client.config.SessionParams) Cluster(com.yahoo.vespa.http.client.config.Cluster) Endpoint(com.yahoo.vespa.http.client.config.Endpoint) Result(com.yahoo.vespa.http.client.Result) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FeedClient(com.yahoo.vespa.http.client.FeedClient)

Example 8 with FeedClient

use of com.yahoo.vespa.http.client.FeedClient in project vespa by vespa-engine.

the class XmlFeedReaderTest method testInsert.

@Test
public void testInsert() throws Exception {
    InputStream stream = new ByteArrayInputStream(insertDocOperation.getBytes(StandardCharsets.UTF_8));
    AtomicInteger numSent = new AtomicInteger(0);
    FeedClient feedClient = mock(FeedClient.class);
    XmlFeedReader.read(stream, feedClient, numSent);
    assertThat(numSent.get(), is(2));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FeedClient(com.yahoo.vespa.http.client.FeedClient) Test(org.junit.Test)

Example 9 with FeedClient

use of com.yahoo.vespa.http.client.FeedClient in project vespa by vespa-engine.

the class XmlFeedReaderTest method testReadUpdate.

@Test
public void testReadUpdate() throws Exception {
    InputStream stream = new ByteArrayInputStream(updateDocUpdate.getBytes(StandardCharsets.UTF_8));
    AtomicInteger numSent = new AtomicInteger(0);
    FeedClient feedClient = mock(FeedClient.class);
    XmlFeedReader.read(stream, feedClient, numSent);
    assertThat(numSent.get(), is(1));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FeedClient(com.yahoo.vespa.http.client.FeedClient) Test(org.junit.Test)

Example 10 with FeedClient

use of com.yahoo.vespa.http.client.FeedClient in project vespa by vespa-engine.

the class XmlFeedReaderTest method testNonDocument.

@Test
public void testNonDocument() throws Exception {
    InputStream stream = new ByteArrayInputStream(badperation.getBytes(StandardCharsets.UTF_8));
    AtomicInteger numSent = new AtomicInteger(0);
    FeedClient feedClient = mock(FeedClient.class);
    XmlFeedReader.read(stream, feedClient, numSent);
    assertThat(numSent.get(), is(0));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FeedClient(com.yahoo.vespa.http.client.FeedClient) Test(org.junit.Test)

Aggregations

FeedClient (com.yahoo.vespa.http.client.FeedClient)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 BufferedInputStream (java.io.BufferedInputStream)8 ByteArrayInputStream (java.io.ByteArrayInputStream)8 InputStream (java.io.InputStream)8 Test (org.junit.Test)8 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 Matchers.anyObject (org.mockito.Matchers.anyObject)2 Matchers.anyString (org.mockito.Matchers.anyString)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Result (com.yahoo.vespa.http.client.Result)1 SimpleLoggerResultCallback (com.yahoo.vespa.http.client.SimpleLoggerResultCallback)1 Cluster (com.yahoo.vespa.http.client.config.Cluster)1 Endpoint (com.yahoo.vespa.http.client.config.Endpoint)1 SessionParams (com.yahoo.vespa.http.client.config.SessionParams)1 File (java.io.File)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1