Refine search
assertEquals(FieldValue.Attribute.PRIMITIVE, timestampCell.getAttribute()); assertEquals(FieldValue.Attribute.PRIMITIVE, stringCell.getAttribute()); assertEquals(FieldValue.Attribute.REPEATED, integerArrayCell.getAttribute()); assertEquals(FieldValue.Attribute.PRIMITIVE, booleanCell.getAttribute()); assertEquals(FieldValue.Attribute.PRIMITIVE, bytesCell.getAttribute()); assertEquals(FieldValue.Attribute.RECORD, recordCell.getAttribute()); assertEquals(FieldValue.Attribute.PRIMITIVE, integerCell.getAttribute()); assertEquals(FieldValue.Attribute.PRIMITIVE, floatCell.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(false, booleanCell.getBooleanValue()); assertArrayEquals(BYTES, bytesCell.getBytesValue()); assertEquals(-14182916000000L, recordCell.getRecordValue().get(0).getTimestampValue()); assertTrue(recordCell.getRecordValue().get(1).isNull()); assertEquals(1, recordCell.getRecordValue().get(2).getRepeatedValue().get(0).getLongValue()); assertEquals(0, recordCell.getRecordValue().get(2).getRepeatedValue().get(1).getLongValue()); assertEquals(true, recordCell.getRecordValue().get(3).getBooleanValue()); assertEquals(3, integerCell.getLongValue()); assertEquals(1.2, floatCell.getDoubleValue(), 0.0001); assertEquals(new BigDecimal("123456.789012345"), numericCell.getNumericValue()); rowCount++;
@Test public void testFromPb() { FieldValue value = FieldValue.fromPb(BOOLEAN_FIELD); assertEquals(FieldValue.Attribute.PRIMITIVE, value.getAttribute()); assertFalse(value.getBooleanValue()); value = FieldValue.fromPb(INTEGER_FIELD); assertEquals(FieldValue.Attribute.PRIMITIVE, value.getAttribute()); assertEquals(1, value.getLongValue()); value = FieldValue.fromPb(FLOAT_FIELD); assertEquals(FieldValue.Attribute.PRIMITIVE, value.getAttribute()); assertEquals(1.5, value.getDoubleValue(), 0); value = FieldValue.fromPb(NUMERIC_FIELD); assertEquals(FieldValue.Attribute.PRIMITIVE, value.getAttribute()); assertEquals(new BigDecimal("123456789.123456789"), value.getNumericValue()); 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(42000000, value.getTimestampValue()); value = FieldValue.fromPb(BYTES_FIELD); assertEquals(FieldValue.Attribute.PRIMITIVE, value.getAttribute()); assertArrayEquals(BYTES, value.getBytesValue()); value = FieldValue.fromPb(NULL_FIELD); assertNull(value.getValue()); value = FieldValue.fromPb(REPEATED_FIELD); assertEquals(FieldValue.Attribute.REPEATED, value.getAttribute()); assertEquals(FieldValue.fromPb(INTEGER_FIELD), value.getRepeatedValue().get(0)); assertEquals(FieldValue.fromPb(INTEGER_FIELD), value.getRepeatedValue().get(1)); value = FieldValue.fromPb(RECORD_FIELD);
/** * Creates an instance of {@code FieldValue}, useful for testing. * * <p>If the {@code attribute} is {@link Attribute#PRIMITIVE}, the {@code value} should be the * string representation of the underlying value, eg {@code "123"} for number {@code 123}. * * <p>If the {@code attribute} is {@link Attribute#REPEATED} or {@link Attribute#RECORD}, the * {@code value} should be {@code List} of {@link FieldValue}s or {@link FieldValueList}, * respectively. * * <p>This method is unstable. See <a * href="https://github.com/googleapis/google-cloud-java/pull/2891">this discussion</a> for more * context. */ @BetaApi public static FieldValue of(Attribute attribute, Object value) { return new FieldValue(attribute, value); }
@SuppressWarnings("unchecked") static FieldValue fromPb(Object cellPb, Field recordSchema) { if (Data.isNull(cellPb)) { return FieldValue.of(Attribute.PRIMITIVE, null); } if (cellPb instanceof String) { return FieldValue.of(Attribute.PRIMITIVE, cellPb); } if (cellPb instanceof List) { return FieldValue.of(Attribute.REPEATED, FieldValueList.fromPb((List<Object>) cellPb, null)); } if (cellPb instanceof Map) { Map<String, Object> cellMapPb = (Map<String, Object>) cellPb; if (cellMapPb.containsKey("f")) { FieldList subFieldsSchema = recordSchema != null ? recordSchema.getSubFields() : null; return FieldValue.of( Attribute.RECORD, FieldValueList.fromPb((List<Object>) cellMapPb.get("f"), subFieldsSchema)); } // This should never be the case when we are processing a first level table field (i.e. a // row's field, not a record sub-field) if (cellMapPb.containsKey("v")) { return FieldValue.fromPb(cellMapPb.get("v"), recordSchema); } } throw new IllegalArgumentException("Unexpected table cell format"); } }
@Test public void testEquals() { FieldValue booleanValue = FieldValue.of(FieldValue.Attribute.PRIMITIVE, "false"); assertEquals(booleanValue, FieldValue.fromPb(BOOLEAN_FIELD)); assertEquals(booleanValue.hashCode(), FieldValue.fromPb(BOOLEAN_FIELD).hashCode()); FieldValue integerValue = FieldValue.of(FieldValue.Attribute.PRIMITIVE, "1"); assertEquals(integerValue, FieldValue.fromPb(INTEGER_FIELD)); assertEquals(integerValue.hashCode(), FieldValue.fromPb(INTEGER_FIELD).hashCode()); FieldValue floatValue = FieldValue.of(FieldValue.Attribute.PRIMITIVE, "1.5"); assertEquals(floatValue, FieldValue.fromPb(FLOAT_FIELD)); assertEquals(floatValue.hashCode(), FieldValue.fromPb(FLOAT_FIELD).hashCode()); FieldValue numericValue = FieldValue.of(FieldValue.Attribute.PRIMITIVE, "123456789.123456789"); assertEquals(numericValue, FieldValue.fromPb(NUMERIC_FIELD)); assertEquals(numericValue.hashCode(), FieldValue.fromPb(NUMERIC_FIELD).hashCode()); FieldValue stringValue = FieldValue.of(FieldValue.Attribute.PRIMITIVE, "string"); assertEquals(stringValue, FieldValue.fromPb(STRING_FIELD)); assertEquals(stringValue.hashCode(), FieldValue.fromPb(STRING_FIELD).hashCode()); FieldValue timestampValue = FieldValue.of(FieldValue.Attribute.PRIMITIVE, "42"); assertEquals(timestampValue, FieldValue.fromPb(TIMESTAMP_FIELD)); assertEquals(timestampValue.hashCode(), FieldValue.fromPb(TIMESTAMP_FIELD).hashCode()); FieldValue bytesValue = FieldValue.of(FieldValue.Attribute.PRIMITIVE, BYTES_BASE64); assertEquals(bytesValue, FieldValue.fromPb(BYTES_FIELD)); assertEquals(bytesValue.hashCode(), FieldValue.fromPb(BYTES_FIELD).hashCode());
@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()); }
private Object convertField(FieldValue fieldValue, org.apache.avro.Schema fieldSchema) { boolean nullable = AvroUtils.isNullable(fieldSchema); if (nullable && fieldValue.isNull()) { return null; } fieldSchema = AvroUtils.unwrapIfNullable(fieldSchema); switch (fieldValue.getAttribute()) { case PRIMITIVE: if (BigQueryType.TIMESTAMP.toString().equals(fieldSchema.getProp(TALEND_COLUMN_DB_TYPE))) { Double doubleValue = ((Long) fieldValue.getTimestampValue()) / 1000000.0; return formatTimestamp(doubleValue.toString()); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._double())) { return fieldValue.getDoubleValue(); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._boolean())) { return fieldValue.getBooleanValue(); } else { return fieldValue.getValue(); } case REPEATED: List<Object> listValue = new ArrayList<>(); List<FieldValue> repeatedChildValue = fieldValue.getRepeatedValue(); for (FieldValue childValue : repeatedChildValue) { listValue.add(convertField(childValue, fieldSchema.getElementType())); } return listValue; case RECORD: return convertFileds(fieldValue.getRecordValue(), fieldSchema); } throw TalendRuntimeException.build(CommonErrorCodes.UNEXPECTED_ARGUMENT).create(); }
/** * 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()); }
assertThat(result.getTotalRows()).isEqualTo(1); for (FieldValueList row : result.getValues()) { assertThat(row.get(0).getBooleanValue()).isFalse(); assertThat(row.get(1).getLongValue()).isEqualTo(1);
@Test public void testGetByName() { assertEquals(10, fieldValues.size()); assertEquals(booleanFv, fieldValues.get("first")); assertEquals(integerFv, fieldValues.get("second")); assertEquals(floatFv, fieldValues.get("third")); assertEquals(stringFv, fieldValues.get("fourth")); assertEquals(timestampFv, fieldValues.get("fifth")); assertEquals(bytesFv, fieldValues.get("sixth")); assertEquals(nullFv, fieldValues.get("seventh")); assertEquals(repeatedFv, fieldValues.get("eight")); assertEquals(2, fieldValues.get("eight").getRepeatedValue().size()); assertEquals(integerFv, fieldValues.get("eight").getRepeatedValue().get(0)); assertEquals(integerFv, fieldValues.get("eight").getRepeatedValue().get(1)); assertEquals(recordFv, fieldValues.get("ninth")); assertEquals(2, fieldValues.get("ninth").getRecordValue().size()); assertEquals(floatFv, fieldValues.get("ninth").getRecordValue().get("first")); assertEquals(timestampFv, fieldValues.get("ninth").getRecordValue().get("second")); assertEquals(numericFv, fieldValues.get("tenth")); }
/** 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; }
String url = row.get("url").getStringValue(); long viewCount = row.get("view_count").getLongValue(); System.out.printf("url: %s views: %d%n", url, viewCount);
/** Example of running a query with timestamp query parameters. */ public void runQueryWithTimestampParameters() throws InterruptedException { // [START bigquery_query_params_timestamps] // BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); DateTime timestamp = new DateTime(2016, 12, 7, 8, 0, 0, DateTimeZone.UTC); String query = "SELECT TIMESTAMP_ADD(@ts_value, INTERVAL 1 HOUR);"; // Note: Standard SQL is required to use query parameters. QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(query) .addNamedParameter( "ts_value", QueryParameterValue.timestamp( // Timestamp takes microseconds since 1970-01-01T00:00:00 UTC timestamp.getMillis() * 1000)) .build(); // Print the results. DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis().withZoneUTC(); for (FieldValueList row : bigquery.query(queryConfig).iterateAll()) { System.out.printf( "%s\n", formatter.print( new DateTime( // Timestamp values are returned in microseconds since 1970-01-01T00:00:00 UTC, // but org.joda.time.DateTime constructor accepts times in milliseconds. row.get(0).getTimestampValue() / 1000, DateTimeZone.UTC))); System.out.printf("\n"); } // [END bigquery_query_params_timestamps] }
static FieldValueList fromPb(List<?> rowPb, FieldList schema) { List<FieldValue> row = new ArrayList<>(rowPb.size()); if (schema != null) { if (schema.size() != rowPb.size()) { throw new IllegalArgumentException("Row size and fields schema sizes should match"); } Iterator<Field> schemaIter = schema.iterator(); Iterator<?> rowPbIter = rowPb.iterator(); while (rowPbIter.hasNext() && schemaIter.hasNext()) { row.add(FieldValue.fromPb(rowPbIter.next(), schemaIter.next())); } } else { for (Object cellPb : rowPb) { row.add(FieldValue.fromPb(cellPb, null)); } } return FieldValueList.of(row, schema); } }
@Test public void testBytesParameter() throws Exception { String query = "SELECT BYTE_LENGTH(@p) AS length"; QueryParameterValue bytesParameter = QueryParameterValue.bytes(new byte[] {1, 3}); QueryJobConfiguration config = QueryJobConfiguration.newBuilder(query) .setDefaultDataset(DatasetId.of(DATASET)) .setUseLegacySql(false) .addNamedParameter("p", bytesParameter) .build(); TableResult result = bigquery.query(config); int rowCount = 0; for (FieldValueList row : result.getValues()) { rowCount++; assertEquals(2, row.get(0).getLongValue()); assertEquals(2, row.get("length").getLongValue()); } assertEquals(1, rowCount); }
static FieldValue fromPb(Object cellPb) { return fromPb(cellPb, null); }
@SuppressWarnings("unchecked") static FieldValue fromPb(Object cellPb) { if (Data.isNull(cellPb)) { return new FieldValue(Attribute.PRIMITIVE, null); return new FieldValue(Attribute.PRIMITIVE, cellPb); List<FieldValue> repeatedCells = Lists.newArrayListWithCapacity(cellsListPb.size()); for (Object repeatedCellPb : cellsListPb) { repeatedCells.add(FieldValue.fromPb(repeatedCellPb)); return new FieldValue(Attribute.REPEATED, repeatedCells); List<FieldValue> recordCells = Lists.newArrayListWithCapacity(cellsListPb.size()); for (Object repeatedCellPb : cellsListPb) { recordCells.add(FieldValue.fromPb(repeatedCellPb)); return new FieldValue(Attribute.RECORD, recordCells); return FieldValue.fromPb(cellMapPb.get("v"));
int rowCount = 0; for (FieldValueList row : result.getValues()) { FieldValue timestampCell = row.get(0); FieldValue stringCell = row.get(1); FieldValue booleanCell = row.get(2); 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++;
private Object convertField(FieldValue fieldValue, org.apache.avro.Schema fieldSchema) { boolean nullable = AvroUtils.isNullable(fieldSchema); if (nullable && fieldValue.isNull()) { return null; } fieldSchema = AvroUtils.unwrapIfNullable(fieldSchema); switch (fieldValue.getAttribute()) { case PRIMITIVE: if (BigQueryType.TIMESTAMP.toString().equals(fieldSchema.getProp(TALEND_COLUMN_DB_TYPE))) { Double doubleValue = ((Long) fieldValue.getTimestampValue()) / 1000000.0; return formatTimestamp(doubleValue.toString()); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._double())) { return fieldValue.getDoubleValue(); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._boolean())) { return fieldValue.getBooleanValue(); } else { return fieldValue.getValue(); } case REPEATED: List<Object> listValue = new ArrayList<>(); List<FieldValue> repeatedChildValue = fieldValue.getRepeatedValue(); for (FieldValue childValue : repeatedChildValue) { listValue.add(convertField(childValue, fieldSchema.getElementType())); } return listValue; case RECORD: return convertFileds(fieldValue.getRecordValue(), fieldSchema); } throw TalendRuntimeException.build(CommonErrorCodes.UNEXPECTED_ARGUMENT).create(); }
/** * 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()); }