Search in sources :

Example 11 with Connection

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

the class ITBigQueryTest method testConnectionImplDryRun.

@Test
public void testConnectionImplDryRun() throws SQLException {
    String query = String.format("select StringField,  BigNumericField, BooleanField, BytesField, IntegerField, TimestampField, FloatField, NumericField, TimeField, DateField,  DateTimeField , GeographyField, RecordField.BytesField, RecordField.BooleanField, IntegerArrayField from %s where StringField = ? order by TimestampField", TABLE_ID_FASTQUERY_BQ_RESULTSET.getTable());
    ConnectionSettings connectionSettings = ConnectionSettings.newBuilder().setDefaultDataset(DatasetId.of(DATASET)).setCreateSession(true).build();
    Connection connection = bigquery.createConnection(connectionSettings);
    BigQueryDryRunResult bigQueryDryRunResultSet = connection.dryRun(query);
    assertNotNull(bigQueryDryRunResultSet.getSchema());
    assertEquals(BQ_RESULTSET_EXPECTED_SCHEMA, // match the schema
    bigQueryDryRunResultSet.getSchema());
    List<Parameter> queryParameters = bigQueryDryRunResultSet.getQueryParameters();
    assertEquals(StandardSQLTypeName.STRING, queryParameters.get(0).getValue().getType());
    QueryStatistics queryStatistics = bigQueryDryRunResultSet.getStatistics().getQueryStatistics();
    assertNotNull(queryStatistics);
    SessionInfo sessionInfo = bigQueryDryRunResultSet.getStatistics().getSessionInfo();
    assertNotNull(sessionInfo.getSessionId());
    assertEquals(StatementType.SELECT, queryStatistics.getStatementType());
}
Also used : QueryStatistics(com.google.cloud.bigquery.JobStatistics.QueryStatistics) Connection(com.google.cloud.bigquery.Connection) BigQueryDryRunResult(com.google.cloud.bigquery.BigQueryDryRunResult) Parameter(com.google.cloud.bigquery.Parameter) SessionInfo(com.google.cloud.bigquery.JobStatistics.SessionInfo) ConnectionSettings(com.google.cloud.bigquery.ConnectionSettings) Test(org.junit.Test)

Example 12 with Connection

use of com.google.cloud.bigquery.Connection 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)

Example 13 with Connection

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

the class ITBigQueryTest method testReadAPIConnectionMultiClose.

@Test
public void testReadAPIConnectionMultiClose() throws SQLException {
    // use read API to read 300K records, then closes the connection. This test
    // repeats it multiple times and assets if the connection was closed
    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 confirmed_cases asc limit 300000";
    ConnectionSettings connectionSettings = ConnectionSettings.newBuilder().setDefaultDataset(DatasetId.of(DATASET)).setPriority(QueryJobConfiguration.Priority.INTERACTIVE).build();
    int closeCnt = 0, runCnt = 3;
    for (int run = 0; run < runCnt; run++) {
        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));
            ++cnt;
        }
        // total 300000 rows should be read
        assertEquals(300000, cnt);
        // check if connection closed
        assertTrue(connection.close());
        closeCnt++;
    }
    assertEquals(closeCnt, // check if the connection closed for the required number of times
    runCnt);
}
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 14 with Connection

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

the class ITBigQueryTest method testExecuteSelectStruct.

@Test
public void testExecuteSelectStruct() throws SQLException {
    String query = "select (STRUCT(\"Vancouver\" as city, 5 as years)) as address";
    ConnectionSettings connectionSettings = ConnectionSettings.newBuilder().setDefaultDataset(DatasetId.of(DATASET)).build();
    Connection connection = bigquery.createConnection(connectionSettings);
    BigQueryResult bigQueryResult = connection.executeSelect(query);
    assertEquals(1, bigQueryResult.getTotalRows());
    Schema schema = bigQueryResult.getSchema();
    assertEquals("address", schema.getFields().get(0).getName());
    assertEquals(Field.Mode.NULLABLE, schema.getFields().get(0).getMode());
    // Backend is currently returning LegacySQLTypeName. Tracking bug: b/202977620
    assertEquals(LegacySQLTypeName.RECORD, schema.getFields().get(0).getType());
    assertEquals("city", schema.getFields().get(0).getSubFields().get(0).getName());
    assertEquals(LegacySQLTypeName.STRING, schema.getFields().get(0).getSubFields().get(0).getType());
    assertEquals(Field.Mode.NULLABLE, schema.getFields().get(0).getSubFields().get(0).getMode());
    assertEquals("years", schema.getFields().get(0).getSubFields().get(1).getName());
    assertEquals(LegacySQLTypeName.INTEGER, schema.getFields().get(0).getSubFields().get(1).getType());
    assertEquals(Field.Mode.NULLABLE, schema.getFields().get(0).getSubFields().get(1).getMode());
    ResultSet rs = bigQueryResult.getResultSet();
    assertTrue(rs.next());
    FieldValueList addressFieldValue = (com.google.cloud.bigquery.FieldValueList) rs.getObject("address");
    assertEquals(rs.getObject("address"), rs.getObject(0));
    assertEquals("Vancouver", addressFieldValue.get(0).getStringValue());
    assertEquals(5, addressFieldValue.get(1).getLongValue());
    // only 1 row of data
    assertFalse(rs.next());
}
Also used : Schema(com.google.cloud.bigquery.Schema) Connection(com.google.cloud.bigquery.Connection) ResultSet(java.sql.ResultSet) BigQueryResult(com.google.cloud.bigquery.BigQueryResult) FieldValueList(com.google.cloud.bigquery.FieldValueList) ConnectionSettings(com.google.cloud.bigquery.ConnectionSettings) Test(org.junit.Test)

Example 15 with Connection

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

the class ITBigQueryTest method testExecuteSelectWithNamedQueryParameters.

@Test
public void testExecuteSelectWithNamedQueryParameters() throws BigQuerySQLException {
    String query = "SELECT TimestampField, StringField, BooleanField FROM " + TABLE_ID.getTable() + " WHERE StringField = @stringParam" + " AND IntegerField IN UNNEST(@integerList)";
    QueryParameterValue stringParameter = QueryParameterValue.string("stringValue");
    QueryParameterValue intArrayParameter = QueryParameterValue.array(new Integer[] { 3, 4 }, Integer.class);
    Parameter stringParam = Parameter.newBuilder().setName("stringParam").setValue(stringParameter).build();
    Parameter intArrayParam = Parameter.newBuilder().setName("integerList").setValue(intArrayParameter).build();
    ConnectionSettings connectionSettings = ConnectionSettings.newBuilder().setDefaultDataset(DatasetId.of(DATASET)).build();
    Connection connection = bigquery.createConnection(connectionSettings);
    List<Parameter> parameters = ImmutableList.of(stringParam, intArrayParam);
    BigQueryResult rs = connection.executeSelect(query, parameters);
    assertEquals(2, rs.getTotalRows());
}
Also used : QueryParameterValue(com.google.cloud.bigquery.QueryParameterValue) Connection(com.google.cloud.bigquery.Connection) Parameter(com.google.cloud.bigquery.Parameter) BigQueryResult(com.google.cloud.bigquery.BigQueryResult) ConnectionSettings(com.google.cloud.bigquery.ConnectionSettings) Test(org.junit.Test)

Aggregations

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