Search in sources :

Example 21 with Connection

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

the class ITNightlyBigQueryTest method testConnectionClose.

/*
  This tests interrupts the execution in between and checks if it has been interrupted successfully while using ReadAPI
   */
@Test
public void testConnectionClose() throws SQLException {
    Connection connection = bigquery.createConnection();
    assertNotNull("bigquery.createConnection() returned null", connection);
    BigQueryResult bigQueryResult = connection.executeSelect(QUERY);
    logger.log(Level.INFO, "Query used: {0}", QUERY);
    ResultSet rs = bigQueryResult.getResultSet();
    int cnt = 0;
    while (rs.next()) {
        ++cnt;
        if (cnt == 50000) {
            // interrupt at 50K
            assertTrue(connection.close());
        }
    }
    assertTrue(LIMIT_RECS > // we stopped at 50K but still we can expect additional records (typically ~100)
    cnt);
// to be retrieved
// as a number of records should have been already buffered. less than
// LIMIT_RECS should be retrieved
}
Also used : Connection(com.google.cloud.bigquery.Connection) ResultSet(java.sql.ResultSet) BigQueryResult(com.google.cloud.bigquery.BigQueryResult) Test(org.junit.Test)

Example 22 with Connection

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

the class ITNightlyBigQueryTest method testPositionalParams.

@Test
public void testPositionalParams() throws SQLException {
    // Bypasses Read API as it doesnt support Positional Params
    Connection connection = getConnection();
    Parameter dateParam = Parameter.newBuilder().setValue(QueryParameterValue.date("2022-01-01")).build();
    Parameter boolParam = Parameter.newBuilder().setValue(QueryParameterValue.bool(true)).build();
    Parameter intParam = Parameter.newBuilder().setValue(QueryParameterValue.int64(1)).build();
    Parameter numericParam = Parameter.newBuilder().setValue(QueryParameterValue.numeric(new BigDecimal(100))).build();
    List<Parameter> parameters = ImmutableList.of(dateParam, boolParam, intParam, numericParam);
    BigQueryResult bigQueryResult = connection.executeSelect(POSITIONAL_QUERY, parameters);
    logger.log(Level.INFO, "Query used: {0}", POSITIONAL_QUERY);
    ResultSet rs = bigQueryResult.getResultSet();
    int cnt = 0;
    while (rs.next()) {
        assertFalse(rs.getBoolean("BooleanField"));
        assertTrue(0.0d <= rs.getDouble("BigNumericField"));
        assertTrue(0 <= rs.getInt("IntegerField"));
        assertTrue(0L <= rs.getLong("NumericField"));
        assertNotNull(rs.getBytes("BytesField"));
        assertNotNull(rs.getTimestamp("TimestampField"));
        assertNotNull(rs.getTime("TimeField"));
        assertNotNull(rs.getDate("DateField"));
        assertNotNull(rs.getString("JSONField"));
        assertTrue(rs.getBoolean("BooleanField_1"));
        assertNotNull(rs.getString("StringField_1"));
        ++cnt;
    }
    connection.close();
    assertEquals(MULTI_LIMIT_RECS, cnt);
}
Also used : Connection(com.google.cloud.bigquery.Connection) ResultSet(java.sql.ResultSet) Parameter(com.google.cloud.bigquery.Parameter) BigQueryResult(com.google.cloud.bigquery.BigQueryResult) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 23 with Connection

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

the class ITNightlyBigQueryTest method testMultipleRuns.

@Test
public void testMultipleRuns() throws SQLException {
    Connection connection = getConnection();
    BigQueryResult bigQueryResult = connection.executeSelect(MULTI_QUERY);
    logger.log(Level.INFO, "Query used: {0}", MULTI_QUERY);
    ResultSet rs = bigQueryResult.getResultSet();
    int cnt = 0;
    int totalCnt = 0;
    int prevIntegerFieldVal = 0;
    while (rs.next()) {
        if (cnt == 0) {
            // first row is supposed to be null
            assertNull(rs.getString("StringField"));
            assertNull(rs.getString("GeographyField"));
            Object intAryField = rs.getObject("IntegerArrayField");
            if (intAryField instanceof JsonStringArrayList) {
                assertEquals(new JsonStringArrayList(), // null array is returned as an empty array
                ((JsonStringArrayList) intAryField));
            }
            assertFalse(rs.getBoolean("BooleanField"));
            assertTrue(0.0d == rs.getDouble("BigNumericField"));
            assertTrue(0 == rs.getInt("IntegerField"));
            assertTrue(0L == rs.getLong("NumericField"));
            assertNull(rs.getBytes("BytesField"));
            assertNull(rs.getTimestamp("TimestampField"));
            assertNull(rs.getTime("TimeField"));
            assertNull(rs.getDate("DateField"));
            assertNull(rs.getString("JSONField"));
            assertFalse(rs.getBoolean("BooleanField_1"));
            assertNull(rs.getString("StringField_1"));
            // equivalent of testJsonType
            assertNull(rs.getString("hello"));
            assertEquals(0, rs.getInt("id"));
        } else {
            // remaining rows are supposed to be non null
            // check the order of the records
            assertTrue(prevIntegerFieldVal < rs.getInt("IntegerField"));
            prevIntegerFieldVal = rs.getInt("IntegerField");
            // asserts the value of each row
            testForAllDataTypeValues(rs, cnt);
        }
        ++cnt;
    }
    connection.close();
    totalCnt += cnt;
    // Repeat the same run
    connection = getConnection();
    bigQueryResult = connection.executeSelect(MULTI_QUERY);
    rs = bigQueryResult.getResultSet();
    cnt = 0;
    prevIntegerFieldVal = 0;
    while (rs.next()) {
        if (cnt == 0) {
            // first row is supposed to be null
            assertNull(rs.getString("StringField"));
            assertNull(rs.getString("GeographyField"));
            Object intAryField = rs.getObject("IntegerArrayField");
            if (intAryField instanceof JsonStringArrayList) {
                assertEquals(new JsonStringArrayList(), // null array is returned as an empty array
                ((JsonStringArrayList) intAryField));
            }
            assertFalse(rs.getBoolean("BooleanField"));
            assertTrue(0.0d == rs.getDouble("BigNumericField"));
            assertTrue(0 == rs.getInt("IntegerField"));
            assertTrue(0L == rs.getLong("NumericField"));
            assertNull(rs.getBytes("BytesField"));
            assertNull(rs.getTimestamp("TimestampField"));
            assertNull(rs.getTime("TimeField"));
            assertNull(rs.getDate("DateField"));
            assertNull(rs.getString("JSONField"));
            assertFalse(rs.getBoolean("BooleanField_1"));
            assertNull(rs.getString("StringField_1"));
            // equivalent of testJsonType
            assertNull(rs.getString("hello"));
            assertEquals(0, rs.getInt("id"));
        } else {
            // remaining rows are supposed to be non null
            // check the order of the records
            assertTrue(prevIntegerFieldVal < rs.getInt("IntegerField"));
            prevIntegerFieldVal = rs.getInt("IntegerField");
            // asserts the value of each row
            testForAllDataTypeValues(rs, cnt);
        }
        ++cnt;
    }
    connection.close();
    totalCnt += cnt;
    assertEquals(MULTI_LIMIT_RECS * 2, totalCnt);
}
Also used : Connection(com.google.cloud.bigquery.Connection) ResultSet(java.sql.ResultSet) BigQueryResult(com.google.cloud.bigquery.BigQueryResult) JsonStringArrayList(org.apache.arrow.vector.util.JsonStringArrayList) Test(org.junit.Test)

Example 24 with Connection

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

the class ITNightlyBigQueryTest method testIterateAndOrderDefaultConnSettings.

/*
  This tests for the order of the records using default connection settings as well as the value of the records using testForAllDataTypeValues
   */
@Test
public void testIterateAndOrderDefaultConnSettings() throws SQLException {
    Connection connection = bigquery.createConnection();
    BigQueryResult bigQueryResult = connection.executeSelect(QUERY);
    logger.log(Level.INFO, "Query used: {0}", QUERY);
    ResultSet rs = bigQueryResult.getResultSet();
    int cnt = 0;
    int prevIntegerFieldVal = 0;
    while (rs.next()) {
        if (cnt == 0) {
            // first row is supposed to be null
            assertNull(rs.getString("StringField"));
            assertNull(rs.getString("GeographyField"));
            Object intAryField = rs.getObject("IntegerArrayField");
            if (intAryField instanceof JsonStringArrayList) {
                assertEquals(new JsonStringArrayList(), // null array is returned as an empty array
                ((JsonStringArrayList) intAryField));
            }
            assertFalse(rs.getBoolean("BooleanField"));
            assertTrue(0.0d == rs.getDouble("BigNumericField"));
            assertTrue(0 == rs.getInt("IntegerField"));
            assertTrue(0L == rs.getLong("NumericField"));
            assertNull(rs.getBytes("BytesField"));
            assertNull(rs.getTimestamp("TimestampField"));
            assertNull(rs.getTime("TimeField"));
            assertNull(rs.getDate("DateField"));
            assertNull(rs.getString("JSONField"));
            assertFalse(rs.getBoolean("BooleanField_1"));
            assertNull(rs.getString("StringField_1"));
            // equivalent of testJsonType
            assertNull(rs.getString("hello"));
            assertEquals(0, rs.getInt("id"));
        } else {
            // remaining rows are supposed to be non null
            assertNotNull(rs.getString("StringField"));
            assertNotNull(rs.getString("GeographyField"));
            assertNotNull(rs.getObject("IntegerArrayField"));
            assertTrue(rs.getBoolean("BooleanField"));
            assertTrue(0.0d < rs.getDouble("BigNumericField"));
            assertTrue(0 < rs.getInt("IntegerField"));
            assertTrue(0L < rs.getLong("NumericField"));
            assertNotNull(rs.getBytes("BytesField"));
            assertNotNull(rs.getTimestamp("TimestampField"));
            assertNotNull(rs.getTime("TimeField"));
            assertNotNull(rs.getDate("DateField"));
            assertNotNull(rs.getString("JSONField"));
            assertFalse(rs.getBoolean("BooleanField_1"));
            assertNotNull(rs.getString("StringField_1"));
            // check the order of the records
            assertTrue(prevIntegerFieldVal < rs.getInt("IntegerField"));
            prevIntegerFieldVal = rs.getInt("IntegerField");
            // asserts the value of each row
            testForAllDataTypeValues(rs, cnt);
        }
        ++cnt;
    }
    // all the records were retrieved
    assertEquals(LIMIT_RECS, cnt);
    assertTrue(connection.close());
}
Also used : Connection(com.google.cloud.bigquery.Connection) ResultSet(java.sql.ResultSet) BigQueryResult(com.google.cloud.bigquery.BigQueryResult) JsonStringArrayList(org.apache.arrow.vector.util.JsonStringArrayList) 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