Search in sources :

Example 36 with HttpResponse

use of co.cask.common.http.HttpResponse in project cdap by caskdata.

the class ExploreHttpClient method getSchemas.

@Override
public QueryHandle getSchemas(@Nullable String catalog, @Nullable String schemaPattern) throws ExploreException, SQLException {
    String body = GSON.toJson(new SchemasArgs(catalog, schemaPattern));
    String resource = String.format("namespaces/%s/data/explore/jdbc/schemas", schemaPattern);
    HttpResponse response = doPost(resource, body, null);
    if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
        return QueryHandle.fromId(parseResponseAsMap(response, "handle"));
    }
    throw new ExploreException("Cannot get the schemas. Reason: " + response);
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) SchemasArgs(co.cask.cdap.explore.utils.SchemasArgs) ExploreException(co.cask.cdap.explore.service.ExploreException)

Example 37 with HttpResponse

use of co.cask.common.http.HttpResponse in project cdap by caskdata.

the class ExploreHttpClient method getColumns.

@Override
public QueryHandle getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws ExploreException, SQLException {
    String body = GSON.toJson(new ColumnsArgs(catalog, schemaPattern, tableNamePattern, columnNamePattern));
    String resource = String.format("namespaces/%s/data/explore/jdbc/columns", schemaPattern);
    HttpResponse response = doPost(resource, body, null);
    if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
        return QueryHandle.fromId(parseResponseAsMap(response, "handle"));
    }
    throw new ExploreException("Cannot get the columns. Reason: " + response);
}
Also used : ColumnsArgs(co.cask.cdap.explore.utils.ColumnsArgs) HttpResponse(co.cask.common.http.HttpResponse) ExploreException(co.cask.cdap.explore.service.ExploreException)

Example 38 with HttpResponse

use of co.cask.common.http.HttpResponse in project cdap by caskdata.

the class ExploreHttpClient method getTableInfo.

@Override
public TableInfo getTableInfo(String namespace, @Nullable String databaseName, String table) throws ExploreException, TableNotFoundException {
    String url = String.format("namespaces/%s/data/explore/tables/%s/info", namespace, table);
    if (databaseName != null) {
        url += "?database=" + databaseName;
    }
    HttpResponse response = doGet(url);
    if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
        return parseJson(response, TableInfo.class);
    } else if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new TableNotFoundException(String.format("Namespace %s, table %s not found.", namespace, table));
    }
    throw new ExploreException(String.format("Cannot get the schema of namespace %s, table %s. Reason: %s", namespace, table, response));
}
Also used : TableNotFoundException(co.cask.cdap.explore.service.TableNotFoundException) HttpResponse(co.cask.common.http.HttpResponse) ExploreException(co.cask.cdap.explore.service.ExploreException)

Example 39 with HttpResponse

use of co.cask.common.http.HttpResponse in project cdap by caskdata.

the class SpamClassifierTest method testClassification.

private boolean testClassification(ServiceManager serviceManager, String messageId, String expected) throws IOException {
    URL url = new URL(serviceManager.getServiceURL(15, TimeUnit.SECONDS), SpamClassifier.SpamClassifierServiceHandler.CLASSIFICATION_PATH + "/" + messageId);
    HttpResponse response = HttpRequests.execute(HttpRequest.get(url).build());
    return (HttpURLConnection.HTTP_OK == response.getResponseCode() && expected.equalsIgnoreCase(response.getResponseBodyAsString()));
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) URL(java.net.URL)

Example 40 with HttpResponse

use of co.cask.common.http.HttpResponse in project cdap by caskdata.

the class SparkPageRankAppTest method test.

@Test
public void test() throws Exception {
    // Deploy the SparkPageRankApp
    ApplicationManager appManager = deployApplication(SparkPageRankApp.class);
    // Send a stream events to the Stream
    StreamManager streamManager = getStreamManager(SparkPageRankApp.BACKLINK_URL_STREAM);
    streamManager.send(Joiner.on(" ").join(URL_1, URL_2));
    streamManager.send(Joiner.on(" ").join(URL_1, URL_3));
    streamManager.send(Joiner.on(" ").join(URL_2, URL_1));
    streamManager.send(Joiner.on(" ").join(URL_3, URL_1));
    // Start service
    ServiceManager serviceManager = appManager.getServiceManager(SparkPageRankApp.SERVICE_HANDLERS).start();
    // Wait for service to start since the Spark program needs it
    serviceManager.waitForStatus(true);
    // Start the SparkPageRankProgram
    SparkManager sparkManager = appManager.getSparkManager(SparkPageRankApp.PageRankSpark.class.getSimpleName()).start();
    sparkManager.waitForRun(ProgramRunStatus.COMPLETED, 60, TimeUnit.SECONDS);
    // Run RanksCounter which will count the number of pages for a pr
    MapReduceManager mapReduceManager = appManager.getMapReduceManager(SparkPageRankApp.RanksCounter.class.getSimpleName()).start();
    mapReduceManager.waitForRun(ProgramRunStatus.COMPLETED, 3, TimeUnit.MINUTES);
    // Query for rank
    URL url = new URL(serviceManager.getServiceURL(15, TimeUnit.SECONDS), SparkPageRankApp.SparkPageRankServiceHandler.RANKS_PATH);
    HttpRequest request = HttpRequest.post(url).withBody(("{\"" + SparkPageRankApp.SparkPageRankServiceHandler.URL_KEY + "\":\"" + URL_1 + "\"}")).build();
    HttpResponse response = HttpRequests.execute(request);
    Assert.assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    Assert.assertEquals(RANK, response.getResponseBodyAsString());
    // Request total pages for a page rank and verify it
    url = new URL(serviceManager.getServiceURL(15, TimeUnit.SECONDS), SparkPageRankApp.SparkPageRankServiceHandler.TOTAL_PAGES_PATH + "/" + RANK);
    response = HttpRequests.execute(HttpRequest.get(url).build());
    Assert.assertEquals(TOTAL_PAGES, response.getResponseBodyAsString());
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) ApplicationManager(co.cask.cdap.test.ApplicationManager) SparkManager(co.cask.cdap.test.SparkManager) MapReduceManager(co.cask.cdap.test.MapReduceManager) StreamManager(co.cask.cdap.test.StreamManager) ServiceManager(co.cask.cdap.test.ServiceManager) HttpResponse(co.cask.common.http.HttpResponse) URL(java.net.URL) Test(org.junit.Test)

Aggregations

HttpResponse (co.cask.common.http.HttpResponse)216 URL (java.net.URL)147 HttpRequest (co.cask.common.http.HttpRequest)80 NotFoundException (co.cask.cdap.common.NotFoundException)42 TypeToken (com.google.common.reflect.TypeToken)26 Test (org.junit.Test)26 ProgramNotFoundException (co.cask.cdap.common.ProgramNotFoundException)24 ApplicationNotFoundException (co.cask.cdap.common.ApplicationNotFoundException)21 BadRequestException (co.cask.cdap.common.BadRequestException)20 IOException (java.io.IOException)16 ExploreException (co.cask.cdap.explore.service.ExploreException)13 ServiceManager (co.cask.cdap.test.ServiceManager)12 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)10 ApplicationManager (co.cask.cdap.test.ApplicationManager)10 StreamNotFoundException (co.cask.cdap.common.StreamNotFoundException)8 HashMap (java.util.HashMap)7 List (java.util.List)7 AccessToken (co.cask.cdap.security.authentication.client.AccessToken)6 TypeToken (com.google.gson.reflect.TypeToken)6 TopicNotFoundException (co.cask.cdap.api.messaging.TopicNotFoundException)5