/** * Returns this field's value as a {@link Double}. This method should only be used if the * corresponding field has {@link LegacySQLTypeName#FLOAT} type. * * @throws ClassCastException if the field is not a primitive type * @throws NumberFormatException if the field's value could not be converted to {@link Double} * @throws NullPointerException if {@link #isNull()} returns {@code true} */ @SuppressWarnings("unchecked") public double getDoubleValue() { return Double.parseDouble(getStringValue()); }
/** * Returns this field's value as a {@code long}. This method should only be used if the * corresponding field has {@link LegacySQLTypeName#INTEGER} type. * * @throws ClassCastException if the field is not a primitive type * @throws NumberFormatException if the field's value could not be converted to {@link Integer} * @throws NullPointerException if {@link #isNull()} returns {@code true} */ @SuppressWarnings("unchecked") public long getLongValue() { return Long.parseLong(getStringValue()); }
/** * Returns this field's value as a {@link java.math.BigDecimal}. This method should only be used * if the corresponding field has {@link LegacySQLTypeName#NUMERIC} type. * * @throws ClassCastException if the field is not a primitive type * @throws NumberFormatException if the field's value could not be converted to {@link * java.math.BigDecimal} * @throws NullPointerException if {@link #isNull()} returns {@code true} */ @SuppressWarnings("unchecked") public BigDecimal getNumericValue() { return new BigDecimal(getStringValue()); }
/** * Returns this field's value as a byte array. This method should only be used if the * corresponding field has primitive type ({@link LegacySQLTypeName#BYTES}. * * @throws ClassCastException if the field is not a primitive type * @throws NullPointerException if {@link #isNull()} returns {@code true} * @throws IllegalStateException if the field value is not encoded in base64 */ public byte[] getBytesValue() { try { return BaseEncoding.base64().decode(getStringValue()); } catch (IllegalArgumentException ex) { throw new IllegalStateException(ex); } }
/** * Returns this field's value as a {@code long}, representing a timestamp in microseconds since * epoch (UNIX time). This method should only be used if the corresponding field has {@link * LegacySQLTypeName#TIMESTAMP} type. * * @throws ClassCastException if the field is not a primitive type * @throws NumberFormatException if the field's value could not be converted to {@link Long} * @throws NullPointerException if {@link #isNull()} returns {@code true} */ @SuppressWarnings("unchecked") public long getTimestampValue() { // timestamps are encoded in the format 1408452095.22 where the integer part is seconds since // epoch (e.g. 1408452095.22 == 2014-08-19 07:41:35.220 -05:00) return new Double(Double.valueOf(getStringValue()) * MICROSECONDS).longValue(); }
/** * Returns this field's value as a {@link Boolean}. This method should only be used if the * corresponding field has {@link LegacySQLTypeName#BOOLEAN} type. * * @throws ClassCastException if the field is not a primitive type * @throws IllegalStateException if the field's value could not be converted to {@link Boolean} * @throws NullPointerException if {@link #isNull()} returns {@code true} */ @SuppressWarnings("unchecked") public boolean getBooleanValue() { String stringValue = getStringValue(); checkState( stringValue.equalsIgnoreCase("true") || stringValue.equalsIgnoreCase("false"), "Field value is not of boolean type"); return Boolean.parseBoolean(stringValue); }
/** Example of listing table rows with schema. */ // [TARGET listTableData(TableId, Schema, TableDataListOption...)] public FieldValueList listTableDataSchemaId() { // [START ] Schema schema = Schema.of( Field.of("word", LegacySQLTypeName.STRING), Field.of("word_count", LegacySQLTypeName.STRING), Field.of("corpus", LegacySQLTypeName.STRING), Field.of("corpus_date", LegacySQLTypeName.STRING)); TableResult tableData = bigquery.listTableData( TableId.of("bigquery-public-data", "samples", "shakespeare"), schema); FieldValueList row = tableData.getValues().iterator().next(); System.out.println(row.get("word").getStringValue()); // [END ] return row; }
value = FieldValue.fromPb(STRING_FIELD); assertEquals(FieldValue.Attribute.PRIMITIVE, value.getAttribute()); assertEquals("string", value.getStringValue()); value = FieldValue.fromPb(TIMESTAMP_FIELD); assertEquals(FieldValue.Attribute.PRIMITIVE, value.getAttribute());
assertEquals(FieldValue.Attribute.PRIMITIVE, numericCell.getAttribute()); assertEquals(1408452095220000L, timestampCell.getTimestampValue()); assertEquals("stringValue", stringCell.getStringValue()); assertEquals(0, integerArrayCell.getRepeatedValue().get(0).getLongValue()); assertEquals(1, integerArrayCell.getRepeatedValue().get(1).getLongValue());
assertEquals(FieldValue.Attribute.PRIMITIVE, numericCell.getAttribute()); assertEquals(1408452095220000L, timestampCell.getTimestampValue()); assertEquals("stringValue", stringCell.getStringValue()); assertEquals(0, integerArrayCell.getRepeatedValue().get(0).getLongValue()); assertEquals(1, integerArrayCell.getRepeatedValue().get(1).getLongValue());
@Test public void testQuery() throws InterruptedException { String query = "SELECT TimestampField, StringField, BooleanField FROM " + TABLE_ID.getTable(); QueryJobConfiguration config = QueryJobConfiguration.newBuilder(query).setDefaultDataset(DatasetId.of(DATASET)).build(); Job job = bigquery.create(JobInfo.of(JobId.of(), config)); TableResult result = job.getQueryResults(); assertEquals(QUERY_RESULT_SCHEMA, result.getSchema()); int rowCount = 0; for (FieldValueList row : result.getValues()) { FieldValue timestampCell = row.get(0); assertEquals(timestampCell, row.get("TimestampField")); FieldValue stringCell = row.get(1); assertEquals(stringCell, row.get("StringField")); FieldValue booleanCell = row.get(2); assertEquals(booleanCell, row.get("BooleanField")); assertEquals(FieldValue.Attribute.PRIMITIVE, timestampCell.getAttribute()); assertEquals(FieldValue.Attribute.PRIMITIVE, stringCell.getAttribute()); assertEquals(FieldValue.Attribute.PRIMITIVE, booleanCell.getAttribute()); assertEquals(1408452095220000L, timestampCell.getTimestampValue()); assertEquals("stringValue", stringCell.getStringValue()); assertEquals(false, booleanCell.getBooleanValue()); rowCount++; } assertEquals(2, rowCount); Job job2 = bigquery.getJob(job.getJobId()); JobStatistics.QueryStatistics statistics = job2.getStatistics(); assertNotNull(statistics.getQueryPlan()); }
assertEquals(FieldValue.Attribute.PRIMITIVE, booleanCell.getAttribute()); assertEquals(1408452095220000L, timestampCell.getTimestampValue()); assertEquals("stringValue", stringCell.getStringValue()); assertEquals(false, booleanCell.getBooleanValue()); rowCount++;
String url = row.get("url").getStringValue(); long viewCount = row.get("view_count").getLongValue(); System.out.printf("url: %s views: %d%n", url, viewCount);
/** * Returns this field's value as a {@link Double}. This method should only be used if the * corresponding field has {@link LegacySQLTypeName#FLOAT} type. * * @throws ClassCastException if the field is not a primitive type * @throws NumberFormatException if the field's value could not be converted to {@link Double} * @throws NullPointerException if {@link #isNull()} returns {@code true} */ @SuppressWarnings("unchecked") public double getDoubleValue() { return Double.parseDouble(getStringValue()); }
/** * Returns this field's value as a {@code long}. This method should only be used if the * corresponding field has {@link LegacySQLTypeName#INTEGER} type. * * @throws ClassCastException if the field is not a primitive type * @throws NumberFormatException if the field's value could not be converted to {@link Integer} * @throws NullPointerException if {@link #isNull()} returns {@code true} */ @SuppressWarnings("unchecked") public long getLongValue() { return Long.parseLong(getStringValue()); }
/** * Returns this field's value as a {@link java.math.BigDecimal}. This method should only be used * if the corresponding field has {@link LegacySQLTypeName#NUMERIC} type. * * @throws ClassCastException if the field is not a primitive type * @throws NumberFormatException if the field's value could not be converted to {@link * java.math.BigDecimal} * @throws NullPointerException if {@link #isNull()} returns {@code true} */ @SuppressWarnings("unchecked") public BigDecimal getNumericValue() { return new BigDecimal(getStringValue()); }
assertEquals(FieldValue.Attribute.PRIMITIVE, booleanCell.getAttribute()); assertEquals(1408452095220000L, timestampCell.getTimestampValue()); assertEquals("stringValue", stringCell.getStringValue()); assertEquals(false, booleanCell.getBooleanValue()); rowCount++;
/** * Returns this field's value as a {@code long}, representing a timestamp in microseconds since * epoch (UNIX time). This method should only be used if the corresponding field has {@link * LegacySQLTypeName#TIMESTAMP} type. * * @throws ClassCastException if the field is not a primitive type * @throws NumberFormatException if the field's value could not be converted to {@link Long} * @throws NullPointerException if {@link #isNull()} returns {@code true} */ @SuppressWarnings("unchecked") public long getTimestampValue() { // timestamps are encoded in the format 1408452095.22 where the integer part is seconds since // epoch (e.g. 1408452095.22 == 2014-08-19 07:41:35.220 -05:00) return new Double(Double.valueOf(getStringValue()) * MICROSECONDS).longValue(); }
/** * Returns this field's value as a byte array. This method should only be used if the * corresponding field has primitive type ({@link LegacySQLTypeName#BYTES}. * * @throws ClassCastException if the field is not a primitive type * @throws NullPointerException if {@link #isNull()} returns {@code true} * @throws IllegalStateException if the field value is not encoded in base64 */ public byte[] getBytesValue() { try { return BaseEncoding.base64().decode(getStringValue()); } catch (IllegalArgumentException ex) { throw new IllegalStateException(ex); } }
assertEquals(FieldValue.Attribute.PRIMITIVE, booleanCell.getAttribute()); assertEquals(1408452095220000L, timestampCell.getTimestampValue()); assertEquals("stringValue", stringCell.getStringValue()); assertEquals(integerValue, integerCell.getLongValue()); assertEquals(false, booleanCell.getBooleanValue());