Search in sources :

Example 1 with ExploreClient

use of co.cask.cdap.explore.client.ExploreClient in project cdap by caskdata.

the class ExploreStatementTest method executeTest.

@Test
public void executeTest() throws Exception {
    List<ColumnDesc> columnDescriptions = Lists.newArrayList(new ColumnDesc("column1", "STRING", 1, ""));
    List<QueryResult> queryResults = Lists.newArrayList();
    ExploreClient exploreClient = new MockExploreClient(ImmutableMap.of("mock_query_1", columnDescriptions, "mock_query_2", columnDescriptions, "mock_query_3", columnDescriptions, "mock_query_4", columnDescriptions), ImmutableMap.of("mock_query_1", queryResults, "mock_query_2", queryResults, "mock_query_3", queryResults, "mock_query_4", queryResults));
    // Make sure an empty query still has a ResultSet associated to it
    ExploreStatement statement = new ExploreStatement(null, exploreClient, "ns1");
    Assert.assertTrue(statement.execute("mock_query_1"));
    ResultSet rs = statement.getResultSet();
    Assert.assertNotNull(rs);
    Assert.assertFalse(rs.isClosed());
    Assert.assertFalse(rs.next());
    rs = statement.executeQuery("mock_query_2");
    Assert.assertNotNull(rs);
    Assert.assertFalse(rs.isClosed());
    Assert.assertFalse(rs.next());
    // Make sure subsequent calls to an execute method close the previous results
    ResultSet rs2 = statement.executeQuery("mock_query_3");
    Assert.assertTrue(rs.isClosed());
    Assert.assertNotNull(rs2);
    Assert.assertFalse(rs2.isClosed());
    Assert.assertFalse(rs2.next());
    Assert.assertTrue(statement.execute("mock_query_4"));
    Assert.assertTrue(rs2.isClosed());
}
Also used : ExploreClient(co.cask.cdap.explore.client.ExploreClient) MockExploreClient(co.cask.cdap.explore.client.MockExploreClient) QueryResult(co.cask.cdap.proto.QueryResult) MockExploreClient(co.cask.cdap.explore.client.MockExploreClient) ResultSet(java.sql.ResultSet) ColumnDesc(co.cask.cdap.proto.ColumnDesc) Test(org.junit.Test)

Example 2 with ExploreClient

use of co.cask.cdap.explore.client.ExploreClient in project cdap by caskdata.

the class ExplorePreparedStatementTest method executeTest.

@Test
public void executeTest() throws Exception {
    ExploreClient exploreClient = new MockExploreClient(ImmutableMap.of("SELECT * FROM table WHERE id=100, name='foo'", (List<ColumnDesc>) Lists.newArrayList(new ColumnDesc("column1", "STRING", 1, ""), new ColumnDesc("column2", "int", 2, ""))), ImmutableMap.of("SELECT * FROM table WHERE id=100, name='foo'", (List<QueryResult>) Lists.<QueryResult>newArrayList()));
    String ns = "ns1";
    ExplorePreparedStatement statement = new ExplorePreparedStatement(null, exploreClient, "SELECT * FROM table WHERE id=?, name=?", ns);
    statement.setInt(1, 100);
    try {
        statement.execute();
        Assert.fail();
    } catch (SQLException e) {
    // Parameter 2 has not been set
    }
    statement.setString(2, "foo");
    Assert.assertEquals("SELECT * FROM table WHERE id=100, name='foo'", statement.updateSql());
    Assert.assertTrue(statement.execute());
    ResultSet rs = statement.getResultSet();
    Assert.assertNotNull(rs);
    Assert.assertFalse(rs.isClosed());
    Assert.assertFalse(rs.next());
    statement = new ExplorePreparedStatement(null, exploreClient, "SELECT * FROM table WHERE name='?'", ns);
    Assert.assertEquals("SELECT * FROM table WHERE name='?'", statement.updateSql());
    statement = new ExplorePreparedStatement(null, exploreClient, "SELECT * FROM table WHERE name='?', id=?", ns);
    statement.setInt(1, 100);
    Assert.assertEquals("SELECT * FROM table WHERE name='?', id=100", statement.updateSql());
    statement = new ExplorePreparedStatement(null, exploreClient, "SELECT * FROM table WHERE name=\"?\", id=?", ns);
    statement.setInt(1, 100);
    Assert.assertEquals("SELECT * FROM table WHERE name=\"?\", id=100", statement.updateSql());
    statement = new ExplorePreparedStatement(null, exploreClient, "SELECT * FROM table WHERE name=\"'?'\", id=?", ns);
    statement.setInt(1, 100);
    Assert.assertEquals("SELECT * FROM table WHERE name=\"'?'\", id=100", statement.updateSql());
    statement = new ExplorePreparedStatement(null, exploreClient, "SELECT * FROM table WHERE name=\"'?\", id=?", ns);
    statement.setInt(1, 100);
    Assert.assertEquals("SELECT * FROM table WHERE name=\"'?\", id=100", statement.updateSql());
    statement = new ExplorePreparedStatement(null, exploreClient, "SELECT * FROM table WHERE name=\"\\\"?\\\"\", id=?", ns);
    statement.setInt(1, 100);
    Assert.assertEquals("SELECT * FROM table WHERE name=\"\\\"?\\\"\", id=100", statement.updateSql());
}
Also used : ExploreClient(co.cask.cdap.explore.client.ExploreClient) MockExploreClient(co.cask.cdap.explore.client.MockExploreClient) SQLException(java.sql.SQLException) MockExploreClient(co.cask.cdap.explore.client.MockExploreClient) ResultSet(java.sql.ResultSet) List(java.util.List) ColumnDesc(co.cask.cdap.proto.ColumnDesc) Test(org.junit.Test)

Example 3 with ExploreClient

use of co.cask.cdap.explore.client.ExploreClient in project cdap by caskdata.

the class ExploreResultSetTest method sameNamedColumns.

@Test
public void sameNamedColumns() throws Exception {
    ExploreClient exploreClient = new MockExploreClient(ImmutableMap.of("mock_query", (List<ColumnDesc>) Lists.newArrayList(new ColumnDesc("column1", "STRING", 2, ""), new ColumnDesc("column1", "int", 1, ""))), ImmutableMap.of("mock_query", (List<QueryResult>) Lists.newArrayList(new QueryResult(ImmutableList.<Object>of(1, "value1")))));
    ResultSet resultSet = new ExploreResultSet(exploreClient.submit(new NamespaceId(ns), "mock_query").get(), new ExploreStatement(null, exploreClient, ns), 0);
    Assert.assertTrue(resultSet.next());
    Assert.assertEquals(1, resultSet.findColumn("column1"));
    Assert.assertEquals(1, resultSet.getObject("column1"));
    // Can't call get... after resultSet is closed, nor findColumn
    resultSet.close();
    try {
        resultSet.getObject(1);
    } catch (SQLException e) {
    // Expected
    }
    try {
        resultSet.findColumn("column1");
    } catch (SQLException e) {
    // Expected
    }
}
Also used : MockExploreClient(co.cask.cdap.explore.client.MockExploreClient) ExploreClient(co.cask.cdap.explore.client.ExploreClient) QueryResult(co.cask.cdap.proto.QueryResult) SQLException(java.sql.SQLException) MockExploreClient(co.cask.cdap.explore.client.MockExploreClient) ResultSet(java.sql.ResultSet) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ColumnDesc(co.cask.cdap.proto.ColumnDesc) Test(org.junit.Test)

Example 4 with ExploreClient

use of co.cask.cdap.explore.client.ExploreClient in project cdap by caskdata.

the class ExploreResultSetTest method testResultSet.

@Test
public void testResultSet() throws Exception {
    ExploreClient exploreClient = new MockExploreClient(ImmutableMap.of("mock_query", (List<ColumnDesc>) Lists.newArrayList(new ColumnDesc("column1", "STRING", 1, ""), new ColumnDesc("column2", "int", 2, ""), new ColumnDesc("column3", "char", 3, ""), new ColumnDesc("column4", "float", 4, ""), new ColumnDesc("column5", "double", 5, ""), new ColumnDesc("column6", "boolean", 6, ""), new ColumnDesc("column7", "tinyint", 7, ""), new ColumnDesc("column8", "smallint", 8, ""), new ColumnDesc("column9", "bigint", 9, ""), new ColumnDesc("column10", "date", 10, ""), new ColumnDesc("column11", "timestamp", 11, ""), new ColumnDesc("column12", "decimal", 12, ""), new ColumnDesc("column14", "map<string,string>", 13, ""), new ColumnDesc("column15", "array<string>", 14, ""), new ColumnDesc("column16", "struct<name:string,attr:string>", 15, ""))), ImmutableMap.of("mock_query", (List<QueryResult>) Lists.newArrayList(new QueryResult(ImmutableList.<Object>of("value1", 1, "c", 0.1f, 0.2d, true, 0x1, (short) 2, (long) 10, "2014-06-20", "2014-06-20 07:37:00", "1000000000", "\"{\"key1\":\"value1\"}", "[\"a\",\"b\",\"c\"]", "{\"name\":\"first\",\"attr\":\"second\"}")))));
    ResultSet resultSet = new ExploreResultSet(exploreClient.submit(new NamespaceId(ns), "mock_query").get(), new ExploreStatement(null, exploreClient, ns), 0);
    Assert.assertTrue(resultSet.next());
    Assert.assertEquals(resultSet.getObject(1), resultSet.getObject("column1"));
    Assert.assertEquals("value1", resultSet.getString(1));
    Assert.assertEquals(1, resultSet.getInt(2));
    Assert.assertEquals("c", resultSet.getString(3));
    Assert.assertEquals(0.1f, resultSet.getFloat(4), 0.01);
    Assert.assertEquals(0.2d, resultSet.getDouble(5), 0.01);
    Assert.assertEquals(true, resultSet.getBoolean(6));
    Assert.assertEquals(0x1, resultSet.getByte(7));
    Assert.assertEquals(2, resultSet.getShort(8));
    Assert.assertEquals(10, resultSet.getLong(9));
    Assert.assertEquals(Date.valueOf("2014-06-20"), resultSet.getDate(10));
    Assert.assertEquals(Timestamp.valueOf("2014-06-20 07:37:00"), resultSet.getTimestamp(11));
    Assert.assertEquals(new BigDecimal("1000000000"), resultSet.getBigDecimal(12));
    Assert.assertEquals("\"{\"key1\":\"value1\"}", resultSet.getString(13));
    Assert.assertEquals("[\"a\",\"b\",\"c\"]", resultSet.getString(14));
    Assert.assertEquals("{\"name\":\"first\",\"attr\":\"second\"}", resultSet.getString(15));
    Assert.assertFalse(resultSet.next());
    Assert.assertFalse(resultSet.next());
    try {
        resultSet.getObject(1);
    } catch (SQLException e) {
    // Expected: no more rows
    }
}
Also used : MockExploreClient(co.cask.cdap.explore.client.MockExploreClient) ExploreClient(co.cask.cdap.explore.client.ExploreClient) QueryResult(co.cask.cdap.proto.QueryResult) SQLException(java.sql.SQLException) MockExploreClient(co.cask.cdap.explore.client.MockExploreClient) ResultSet(java.sql.ResultSet) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ColumnDesc(co.cask.cdap.proto.ColumnDesc) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 5 with ExploreClient

use of co.cask.cdap.explore.client.ExploreClient in project cdap by caskdata.

the class ExploreDriver method connect.

@Override
public Connection connect(String url, Properties info) throws SQLException {
    if (!acceptsURL(url)) {
        return null;
    }
    ExploreConnectionParams params = ExploreConnectionParams.parseConnectionUrl(url);
    String authToken = getString(params, ExploreConnectionParams.Info.EXPLORE_AUTH_TOKEN, null);
    String namespace = getString(params, ExploreConnectionParams.Info.NAMESPACE, NamespaceId.DEFAULT.getNamespace());
    boolean sslEnabled = getBoolean(params, ExploreConnectionParams.Info.SSL_ENABLED, false);
    boolean verifySSLCert = getBoolean(params, ExploreConnectionParams.Info.VERIFY_SSL_CERT, true);
    ExploreClient exploreClient = new FixedAddressExploreClient(params.getHost(), params.getPort(), authToken, sslEnabled, verifySSLCert);
    try {
        exploreClient.ping();
    } catch (UnauthenticatedException e) {
        throw new SQLException("Cannot connect to " + url + ", not authenticated.");
    } catch (ServiceUnavailableException | ExploreException e) {
        throw new SQLException("Cannot connect to " + url + ", service not available.");
    }
    return new ExploreConnection(exploreClient, namespace, params);
}
Also used : ExploreClient(co.cask.cdap.explore.client.ExploreClient) FixedAddressExploreClient(co.cask.cdap.explore.client.FixedAddressExploreClient) UnauthenticatedException(co.cask.cdap.common.UnauthenticatedException) SQLException(java.sql.SQLException) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) FixedAddressExploreClient(co.cask.cdap.explore.client.FixedAddressExploreClient) ExploreException(co.cask.cdap.explore.service.ExploreException)

Aggregations

ExploreClient (co.cask.cdap.explore.client.ExploreClient)6 MockExploreClient (co.cask.cdap.explore.client.MockExploreClient)4 SQLException (java.sql.SQLException)4 ColumnDesc (co.cask.cdap.proto.ColumnDesc)3 ResultSet (java.sql.ResultSet)3 Test (org.junit.Test)3 QueryResult (co.cask.cdap.proto.QueryResult)2 List (java.util.List)2 MetricsCollectionService (co.cask.cdap.api.metrics.MetricsCollectionService)1 SecureStore (co.cask.cdap.api.security.store.SecureStore)1 SecureStoreManager (co.cask.cdap.api.security.store.SecureStoreManager)1 AppFabricServiceRuntimeModule (co.cask.cdap.app.guice.AppFabricServiceRuntimeModule)1 AuthorizationModule (co.cask.cdap.app.guice.AuthorizationModule)1 InMemoryProgramRunnerModule (co.cask.cdap.app.guice.InMemoryProgramRunnerModule)1 ServiceStoreModules (co.cask.cdap.app.guice.ServiceStoreModules)1 PreviewHttpModule (co.cask.cdap.app.preview.PreviewHttpModule)1 PreviewManager (co.cask.cdap.app.preview.PreviewManager)1 ServiceUnavailableException (co.cask.cdap.common.ServiceUnavailableException)1 UnauthenticatedException (co.cask.cdap.common.UnauthenticatedException)1 CConfiguration (co.cask.cdap.common.conf.CConfiguration)1