Search in sources :

Example 6 with BigQueryResult

use of com.google.cloud.bigquery.BigQueryResult in project java-bigquery by googleapis.

the class ITBigQueryTest method testBQResultSetPagination.

@Test
public void testBQResultSetPagination() throws SQLException {
    String query = "SELECT date, county, state_name, confirmed_cases, deaths FROM " + TABLE_ID_LARGE.getTable() + " where date is not null and county is not null and state_name is not null order by date limit 300000";
    ConnectionSettings connectionSettings = ConnectionSettings.newBuilder().setDefaultDataset(DatasetId.of(DATASET)).setNumBufferedRows(// page size
    10000).build();
    Connection connection = bigquery.createConnection(connectionSettings);
    BigQueryResult bigQueryResult = connection.executeSelect(query);
    ResultSet rs = bigQueryResult.getResultSet();
    int cnt = 0;
    while (rs.next()) {
        // pagination starts after approx 120,000 records
        assertNotNull(rs.getDate(0));
        assertNotNull(rs.getString(1));
        assertNotNull(rs.getString(2));
        assertTrue(rs.getInt(3) >= 0);
        assertTrue(rs.getInt(4) >= 0);
        ++cnt;
    }
    // total 300000 rows should be read
    assertEquals(300000, cnt);
}
Also used : Connection(com.google.cloud.bigquery.Connection) ResultSet(java.sql.ResultSet) BigQueryResult(com.google.cloud.bigquery.BigQueryResult) ConnectionSettings(com.google.cloud.bigquery.ConnectionSettings) Test(org.junit.Test)

Example 7 with BigQueryResult

use of com.google.cloud.bigquery.BigQueryResult in project java-bigquery by googleapis.

the class ITBigQueryTest method testBQResultSetMultiThreadedOrder.

@Test
public // to the multithreaded BigQueryResult implementation
void testBQResultSetMultiThreadedOrder() throws SQLException {
    String query = "SELECT date FROM " + TABLE_ID_LARGE.getTable() + " where date is not null order by date asc limit 300000";
    ConnectionSettings connectionSettings = ConnectionSettings.newBuilder().setDefaultDataset(DatasetId.of(DATASET)).setNumBufferedRows(// page size
    10000).build();
    Connection connection = bigquery.createConnection(connectionSettings);
    BigQueryResult bigQueryResult = connection.executeSelect(query);
    ResultSet rs = bigQueryResult.getResultSet();
    int cnt = 0;
    assertTrue(rs.next());
    ++cnt;
    java.sql.Date lastDate = rs.getDate(0);
    while (rs.next()) {
        assertNotNull(rs.getDate(0));
        // sorted order is maintained
        assertTrue(rs.getDate(0).getTime() >= lastDate.getTime());
        lastDate = rs.getDate(0);
        ++cnt;
    }
    // total 300000 rows should be read
    assertEquals(300000, cnt);
}
Also used : Connection(com.google.cloud.bigquery.Connection) ResultSet(java.sql.ResultSet) BigQueryResult(com.google.cloud.bigquery.BigQueryResult) ConnectionSettings(com.google.cloud.bigquery.ConnectionSettings) Test(org.junit.Test)

Example 8 with BigQueryResult

use of com.google.cloud.bigquery.BigQueryResult in project java-bigquery by googleapis.

the class ITBigQueryTest method testExecuteSelectDefaultConnectionSettings.

@Test
public void testExecuteSelectDefaultConnectionSettings() throws SQLException {
    // Use the default connection settings
    Connection connection = bigquery.createConnection();
    String query = "SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;";
    BigQueryResult bigQueryResult = connection.executeSelect(query);
    assertEquals(42, bigQueryResult.getTotalRows());
}
Also used : Connection(com.google.cloud.bigquery.Connection) BigQueryResult(com.google.cloud.bigquery.BigQueryResult) Test(org.junit.Test)

Example 9 with BigQueryResult

use of com.google.cloud.bigquery.BigQueryResult in project java-bigquery by googleapis.

the class ITBigQueryTest method testBQResultSetPaginationSlowQuery.

@Test
public void testBQResultSetPaginationSlowQuery() throws SQLException {
    String query = "SELECT date, county, state_name, confirmed_cases, deaths FROM " + TABLE_ID_LARGE.getTable() + " where date is not null and county is not null and state_name is not null order by date limit 300000";
    ConnectionSettings connectionSettings = ConnectionSettings.newBuilder().setDefaultDataset(DatasetId.of(DATASET)).setNumBufferedRows(// page size
    10000).setJobTimeoutMs(// So that ConnectionImpl.isFastQuerySupported returns false, and the slow
    15000L).build();
    Connection connection = bigquery.createConnection(connectionSettings);
    BigQueryResult bigQueryResult = connection.executeSelect(query);
    ResultSet rs = bigQueryResult.getResultSet();
    int cnt = 0;
    while (rs.next()) {
        // pagination starts after approx 120,000 records
        assertNotNull(rs.getDate(0));
        assertNotNull(rs.getString(1));
        assertNotNull(rs.getString(2));
        assertTrue(rs.getInt(3) >= 0);
        assertTrue(rs.getInt(4) >= 0);
        ++cnt;
    }
    // total 300000 rows should be read
    assertEquals(300000, cnt);
}
Also used : Connection(com.google.cloud.bigquery.Connection) ResultSet(java.sql.ResultSet) BigQueryResult(com.google.cloud.bigquery.BigQueryResult) ConnectionSettings(com.google.cloud.bigquery.ConnectionSettings) Test(org.junit.Test)

Example 10 with BigQueryResult

use of com.google.cloud.bigquery.BigQueryResult in project java-bigquery by googleapis.

the class ITNightlyBigQueryTest method testInvalidQuery.

@Test
public void testInvalidQuery() throws BigQuerySQLException {
    Connection connection = getConnection();
    try {
        BigQueryResult bigQueryResult = connection.executeSelect(INVALID_QUERY);
        fail("BigQuerySQLException was expected");
    } catch (BigQuerySQLException ex) {
        assertNotNull(ex.getMessage());
        assertTrue(ex.getMessage().toLowerCase().contains("unexpected keyword into"));
    } finally {
        connection.close();
    }
}
Also used : BigQuerySQLException(com.google.cloud.bigquery.BigQuerySQLException) Connection(com.google.cloud.bigquery.Connection) BigQueryResult(com.google.cloud.bigquery.BigQueryResult) Test(org.junit.Test)

Aggregations

BigQueryResult (com.google.cloud.bigquery.BigQueryResult)22 Connection (com.google.cloud.bigquery.Connection)22 Test (org.junit.Test)22 ResultSet (java.sql.ResultSet)17 ConnectionSettings (com.google.cloud.bigquery.ConnectionSettings)15 Schema (com.google.cloud.bigquery.Schema)6 FieldValueList (com.google.cloud.bigquery.FieldValueList)5 Parameter (com.google.cloud.bigquery.Parameter)3 JsonStringArrayList (org.apache.arrow.vector.util.JsonStringArrayList)3 QueryParameterValue (com.google.cloud.bigquery.QueryParameterValue)2 BigQuerySQLException (com.google.cloud.bigquery.BigQuerySQLException)1 BigDecimal (java.math.BigDecimal)1