com.google.cloud.ByteArray Java Examples

The following examples show how to use com.google.cloud.ByteArray. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Queue.java    From spanner-event-exporter with Apache License 2.0 6 votes vote down vote up
/**
 * Sends a QueueMessage to the spanner queue table
 *
 * <p>Example of sending data to a queue.
 *
 * <pre>{@code
 * MyProto myProto = MyProto.newBuilder().setMessage("My-Message").build();
 *
 * try {
 *   Queue.send(dbClient, queueName, "myKey", ByteArray.copyFrom(myProto.toByteArray()));
 * } catch (SpannerException e) {
 *   log.error("Could not write message to Queue", e);
 * }
 * }</pre>
 *
 * @param dbClient the Spanner database client
 * @param queueName the name of the queue to be polled
 * @param key the name used to partition the passed value for storage and lookup
 * @param value the message payload
 * @return Timestamp the timestamp that the message was written
 */
public static Timestamp send(DatabaseClient dbClient, String queueName, String key, byte[] value)
    throws SpannerException {
  Preconditions.checkNotNull(dbClient);
  Preconditions.checkNotNull(queueName);
  Preconditions.checkNotNull(key);
  Preconditions.checkNotNull(value);

  final List<Mutation> mutations = new ArrayList<>();
  mutations.add(
      Mutation.newInsertBuilder(queueName)
          .set("MessageId")
          .to(UUID.randomUUID().toString())
          .set("Key")
          .to(key)
          .set("Payload")
          .to(ByteArray.copyFrom(value))
          .set("Ack")
          .to(false)
          .set("Timestamp")
          .to(Value.COMMIT_TIMESTAMP)
          .build());

  return dbClient.write(mutations);
}
 
Example #2
Source File: SpannerSample.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
static void queryWithBytes(DatabaseClient dbClient) {
  ByteArray exampleBytes =
      ByteArray.fromBase64(BaseEncoding.base64().encode("Hello World 1".getBytes()));
  Statement statement =
      Statement.newBuilder(
              "SELECT VenueId, VenueName FROM Venues " + "WHERE VenueInfo = @venueInfo")
          .bind("venueInfo")
          .to(exampleBytes)
          .build();
  try (ResultSet resultSet = dbClient.singleUse().executeQuery(statement)) {
    while (resultSet.next()) {
      System.out.printf(
          "%d %s\n", resultSet.getLong("VenueId"), resultSet.getString("VenueName"));
    }
  }
}
 
Example #3
Source File: MutationSizeEstimatorTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void bytes() throws Exception {
  Mutation empty =
      Mutation.newInsertOrUpdateBuilder("test").set("one").to(ByteArray.fromBase64("")).build();
  Mutation nullValue =
      Mutation.newInsertOrUpdateBuilder("test").set("one").to((ByteArray) null).build();
  Mutation sample =
      Mutation.newInsertOrUpdateBuilder("test")
          .set("one")
          .to(ByteArray.fromBase64("abcdabcd"))
          .build();
  Mutation nullArray =
      Mutation.newInsertOrUpdateBuilder("test").set("one").toBytesArray(null).build();

  assertThat(MutationSizeEstimator.sizeOf(empty), is(0L));
  assertThat(MutationSizeEstimator.sizeOf(nullValue), is(0L));
  assertThat(MutationSizeEstimator.sizeOf(sample), is(6L));
  assertThat(MutationSizeEstimator.sizeOf(nullArray), is(0L));
}
 
Example #4
Source File: MutationSizeEstimator.java    From beam with Apache License 2.0 6 votes vote down vote up
private static long sizeOf(Key k) {
  long result = 0;
  for (Object part : k.getParts()) {
    if (part == null) {
      continue;
    }
    if (part instanceof Boolean) {
      result += 1;
    } else if (part instanceof Long) {
      result += 8;
    } else if (part instanceof Double) {
      result += 8;
    } else if (part instanceof String) {
      result += ((String) part).length();
    } else if (part instanceof ByteArray) {
      result += ((ByteArray) part).length();
    } else if (part instanceof Timestamp) {
      result += 12;
    } else if (part instanceof Date) {
      result += 12;
    }
  }
  return result;
}
 
Example #5
Source File: ConverterAwareMappingSpannerEntityReaderTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Test
public void readUnconvertableValueTest() {
	this.expectedEx.expect(ConversionFailedException.class);
	this.expectedEx.expectMessage("Failed to convert from type [java.lang.String] to type " +
			"[java.lang.Double] for value 'UNCONVERTABLE VALUE'; nested exception is " +
			"java.lang.NumberFormatException: For input string: \"UNCONVERTABLEVALUE\"");
	Struct struct = Struct.newBuilder().set("id").to(Value.string("key1")).set("id2")
			.to(Value.string("key2")).set("id3").to(Value.string("key3")).set("id4")
			.to(Value.string("key4")).set("intField2").to(Value.int64(333L))
			.set("custom_col").to(Value.string("WHITE")).set("booleanField")
			.to(Value.bool(true)).set("longField").to(Value.int64(3L))
			.set("doubleField").to(Value.string("UNCONVERTABLE VALUE"))
			.set("doubleArray")
			.to(Value.float64Array(new double[] { 3.33, 3.33, 3.33 }))
			.set("dateField").to(Value.date(Date.fromYearMonthDay(2018, 11, 22)))
			.set("timestampField")
			.to(Value.timestamp(Timestamp.ofTimeMicroseconds(333))).set("bytes")
			.to(Value.bytes(ByteArray.copyFrom("string1"))).build();

	this.spannerEntityReader.read(TestEntity.class, struct);
}
 
Example #6
Source File: ConverterAwareMappingSpannerEntityReaderTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Test
public void readNotFoundColumnTest() {
	this.expectedEx.expect(SpannerDataException.class);
	this.expectedEx.expectMessage("Unable to read column from Cloud Spanner results: id4");
	Struct struct = Struct.newBuilder().set("id").to(Value.string("key1"))
			.set("custom_col").to(Value.string("string1")).set("booleanField")
			.to(Value.bool(true)).set("longField").to(Value.int64(3L))
			.set("doubleArray")
			.to(Value.float64Array(new double[] { 3.33, 3.33, 3.33 }))
			.set("dateField").to(Value.date(Date.fromYearMonthDay(2018, 11, 22)))
			.set("timestampField")
			.to(Value.timestamp(Timestamp.ofTimeMicroseconds(333))).set("bytes")
			.to(Value.bytes(ByteArray.copyFrom("string1"))).build();

	this.spannerEntityReader.read(TestEntity.class, struct);
}
 
Example #7
Source File: KeyConversionTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Parameterized.Parameters(name = "{index}: {0}")
public static Collection<Object[]> types() {
	return Arrays.asList(new Object[][] {
			{ "single boolean", true, Key.of(true) },
			{ "single int", 1, Key.of(1) },
			{ "single long", 23123123L, Key.of(23123123L) },
			{ "single float", .223f, Key.of(.223f) },
			{ "single double", 3.14, Key.of(3.14) },
			{ "single string", "hello", Key.of("hello") },
			{ "single bytearray", ByteArray.copyFrom("world"), Key.of(ByteArray.copyFrom("world")) },
			{ "single timestamp", Timestamp.ofTimeMicroseconds(123132),
					Key.of(Timestamp.ofTimeMicroseconds(123132)) },
			{ "single date", Date.parseDate("2018-04-20"), Key.of(Date.parseDate("2018-04-20")) },
			{ "mixed array", new Object[] { 1, true, false, "hello", ByteArray.copyFrom("world") },
					Key.of(1, true, false, "hello", ByteArray.copyFrom("world")) },
			{ "mixed list", Arrays.asList(1, true, false, "hello", ByteArray.copyFrom("world")),
					Key.of(1, true, false, "hello", ByteArray.copyFrom("world")) },
			{ "converted default type (date)",
					java.util.Date.from(Instant.ofEpochSecond(123)),
					Key.of(SpannerConverters.JAVA_TO_SPANNER_TIMESTAMP_CONVERTER
							.convert(java.sql.Timestamp.from(Instant.ofEpochSecond(123)))) },
			{ "unsupported type (TestEntity)", new TestEntities.TestEntity(), SpannerDataException.class },
			{ "empty key (Object[])", new Object[] {}, SpannerDataException.class },
			{ "empty key (List{})", Collections.emptyList(), SpannerDataException.class },
	});
}
 
Example #8
Source File: ConverterAwareMappingSpannerEntityWriter.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Override
public Key convertToKey(Object key) {
	Assert.notNull(key, "Key of an entity to be written cannot be null!");

	Key k;
	boolean isIterable = Iterable.class.isAssignableFrom(key.getClass());
	boolean isArray = Object[].class.isAssignableFrom(key.getClass());
	if ((isIterable || isArray) && !ByteArray.class.isAssignableFrom(key.getClass())) {
		Key.Builder kb = Key.newBuilder();
		for (Object keyPart : (isArray ? (Arrays.asList((Object[]) key))
				: ((Iterable) key))) {
			kb.appendObject(convertKeyPart(keyPart));
		}
		k = kb.build();
		if (k.size() == 0) {
			throw new SpannerDataException(
					"A key must have at least one component, but 0 were given.");
		}
	}
	else {
		k = Key.class.isAssignableFrom(key.getClass()) ? (Key) key
				: Key.of(convertKeyPart(key));
	}
	return k;
}
 
Example #9
Source File: CloudSpannerResultSetTest.java    From spanner-jdbc with MIT License 5 votes vote down vote up
@Test
public void testGetBinaryStreamLabel() throws SQLException, IOException {
  assertNotNull(subject.getBinaryStream(BYTES_COL_NOT_NULL));
  InputStream actual = subject.getBinaryStream(BYTES_COL_NOT_NULL);
  byte[] cbuf = new byte[3];
  int len = actual.read(cbuf, 0, cbuf.length);
  assertArrayEquals(ByteArray.copyFrom("FOO").toByteArray(), cbuf);
  assertEquals(3, len);
  assertEquals(false, subject.wasNull());
  assertNull(subject.getUnicodeStream(BYTES_COL_NULL));
  assertTrue(subject.wasNull());
}
 
Example #10
Source File: CloudSpannerConversionUtilTest.java    From spanner-jdbc with MIT License 5 votes vote down vote up
@Test
public void testToJavaByteArrays() {
  ByteArray inp1 = ByteArray.copyFrom("AA".getBytes());
  ByteArray inp2 = ByteArray.copyFrom("BB".getBytes());
  List<byte[]> output = CloudSpannerConversionUtil.toJavaByteArrays(Arrays.asList(inp1, inp2));

  List<byte[]> list = Arrays.asList("AA".getBytes(), "BB".getBytes());
  assertArrayEquals(list.toArray(), output.toArray());
}
 
Example #11
Source File: CloudSpannerPreparedStatementTest.java    From spanner-jdbc with MIT License 5 votes vote down vote up
private void assertSingleInsert(Mutation mutation, Mutation.Op operation) {
  Assert.assertNotNull(mutation);
  Assert.assertEquals(operation, mutation.getOperation());
  Assert.assertEquals("FOO", mutation.getTable());
  List<String> columns = Lists.newArrayList(mutation.getColumns());
  Assert.assertArrayEquals(new String[] {"COL1", "COL2", "COL3"}, columns.toArray());
  Assert.assertArrayEquals(
      new String[] {"1", "two",
          ByteArray.copyFrom(DatatypeConverter.parseHexBinary("aa")).toString()},
      getValues(mutation.getValues()));
}
 
Example #12
Source File: CloudSpannerConversionUtilTest.java    From spanner-jdbc with MIT License 5 votes vote down vote up
@Test
public void testToCloudSpannerBytes() {
  byte[][] input = new byte[][] {"AA".getBytes(), "BB".getBytes()};
  List<ByteArray> output = CloudSpannerConversionUtil.toCloudSpannerBytes(input);
  ByteArray inp1 = ByteArray.copyFrom("AA".getBytes());
  ByteArray inp2 = ByteArray.copyFrom("BB".getBytes());
  assertArrayEquals(new ByteArray[] {inp1, inp2}, output.toArray());
}
 
Example #13
Source File: ConverterAwareMappingSpannerEntityWriter.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static Map<Class<?>, BiConsumer<ValueBinder<?>, Iterable>> createIterableTypeMapping() {
	Map<Class<?>, BiConsumer<ValueBinder<?>, Iterable>> map = new LinkedHashMap<>();
	map.put(Boolean.class, ValueBinder::toBoolArray);
	map.put(Long.class, ValueBinder::toInt64Array);
	map.put(Double.class, ValueBinder::toFloat64Array);
	map.put(Timestamp.class, ValueBinder::toTimestampArray);
	map.put(Date.class, ValueBinder::toDateArray);
	map.put(ByteArray.class, ValueBinder::toBytesArray);
	map.put(String.class, ValueBinder::toStringArray);

	return Collections.unmodifiableMap(map);
}
 
Example #14
Source File: SpannerConvertersTest.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void bytesConversionTest() {
	ByteArray byteArray = ByteArray.copyFrom("some bytes");
	assertThat(SpannerConverters.JAVA_TO_SPANNER_BYTE_ARRAY_CONVERTER
			.convert(SpannerConverters.SPANNER_TO_JAVA_BYTE_ARRAY_CONVERTER.convert(byteArray)))
					.isEqualTo(byteArray);
}
 
Example #15
Source File: MutationSizeEstimator.java    From beam with Apache License 2.0 5 votes vote down vote up
private static long estimateArrayValue(Value v) {
  if (v.isNull()) {
    return 0;
  }
  switch (v.getType().getArrayElementType().getCode()) {
    case BOOL:
      return v.getBoolArray().size();
    case INT64:
      return 8L * v.getInt64Array().size();
    case FLOAT64:
      return 8L * v.getFloat64Array().size();
    case STRING:
      long totalLength = 0;
      for (String s : v.getStringArray()) {
        if (s == null) {
          continue;
        }
        totalLength += s.length();
      }
      return totalLength;
    case BYTES:
      totalLength = 0;
      for (ByteArray bytes : v.getBytesArray()) {
        if (bytes == null) {
          continue;
        }
        totalLength += bytes.length();
      }
      return totalLength;
    case DATE:
      return 12L * v.getDateArray().size();
    case TIMESTAMP:
      return 12L * v.getTimestampArray().size();
    default:
      throw new IllegalArgumentException("Unsupported type " + v.getType());
  }
}
 
Example #16
Source File: MutationKeyEncoder.java    From beam with Apache License 2.0 5 votes vote down vote up
private void encodeKey(OrderedCode orderedCode, String tableName, Key key) {
  List<SpannerSchema.KeyPart> parts = schema.getKeyParts(tableName);
  Iterator<Object> it = key.getParts().iterator();
  for (SpannerSchema.KeyPart part : parts) {
    Object value = it.next();
    if (value == null) {
      if (part.isDesc()) {
        orderedCode.writeInfinityDecreasing();
      } else {
        orderedCode.writeInfinity();
      }
    } else {
      if (value instanceof Boolean) {
        writeNumber(orderedCode, part, (long) ((Boolean) value ? 0 : 1));
      } else if (value instanceof Long) {
        writeNumber(orderedCode, part, (long) value);
      } else if (value instanceof Double) {
        writeNumber(orderedCode, part, Double.doubleToLongBits((double) value));
      } else if (value instanceof String) {
        writeString(orderedCode, part, (String) value);
      } else if (value instanceof ByteArray) {
        writeBytes(orderedCode, part, (ByteArray) value);
      } else if (value instanceof Timestamp) {
        writeTimestamp(orderedCode, part, (Timestamp) value);
      } else if (value instanceof Date) {
        writeNumber(orderedCode, part, encodeDate((Date) value));
      } else {
        throw new IllegalArgumentException("Unknown key part " + value);
      }
    }
  }
}
 
Example #17
Source File: MutationKeyEncoder.java    From beam with Apache License 2.0 5 votes vote down vote up
private void writeBytes(OrderedCode orderedCode, KeyPart part, ByteArray bytes) {
  if (part.isDesc()) {
    orderedCode.writeBytesDecreasing(bytes.toByteArray());
  } else {
    orderedCode.writeBytes(bytes.toByteArray());
  }
}
 
Example #18
Source File: GooglePubSubSender.java    From helios with Apache License 2.0 5 votes vote down vote up
@Override
public void send(final String topic, final byte[] message) {
  final String combinedTopic = topicPrefix + topic;

  if (!healthchecker.isHealthy()) {
    log.warn("will not publish message to pubsub topic={} as the pubsub client "
             + "appears to be unhealthy", combinedTopic);
    return;
  }

  try {
    Futures.addCallback(
        JdkFutureAdapters.listenInPoolThread(
            pubsub.publishAsync(combinedTopic, Message.of(ByteArray.copyFrom(message)))),
        new FutureCallback<String>() {
          @Override
          public void onSuccess(@Nullable final String ackId) {
            log.debug("Sent an event to Google PubSub, topic: {}, ack: {}", combinedTopic, ackId);
          }

          @Override
          public void onFailure(final Throwable th) {
            log.warn("Unable to send an event to Google PubSub, topic: {}", combinedTopic, th);
          }
        }, MoreExecutors.directExecutor());
  } catch (Exception e) {
    log.warn("Failed to publish Google PubSub message, topic: {}", combinedTopic, e);
  }
}
 
Example #19
Source File: CloudSpannerResultSetTest.java    From spanner-jdbc with MIT License 5 votes vote down vote up
@Test
public void testGetBytesLabel() throws SQLException {
  assertNotNull(subject.getBytes(BYTES_COL_NOT_NULL));
  assertArrayEquals(ByteArray.copyFrom("FOO").toByteArray(),
      subject.getBytes(BYTES_COL_NOT_NULL));
  assertEquals(false, subject.wasNull());
  assertNull(subject.getBytes(BYTES_COL_NULL));
  assertTrue(subject.wasNull());
}
 
Example #20
Source File: CloudSpannerResultSetTest.java    From spanner-jdbc with MIT License 5 votes vote down vote up
@Test
public void testGetBytesIndex() throws SQLException {
  assertNotNull(subject.getBytes(BYTES_COLINDEX_NOTNULL));
  assertArrayEquals(ByteArray.copyFrom("BAR").toByteArray(),
      subject.getBytes(BYTES_COLINDEX_NOTNULL));
  assertEquals(false, subject.wasNull());
  assertNull(subject.getBytes(BYTES_COLINDEX_NULL));
  assertTrue(subject.wasNull());
}
 
Example #21
Source File: CloudSpannerResultSetTest.java    From spanner-jdbc with MIT License 5 votes vote down vote up
@Test
public void testGetBinaryStreamIndex() throws SQLException, IOException {
  assertNotNull(subject.getBinaryStream(BYTES_COLINDEX_NOTNULL));
  InputStream actual = subject.getBinaryStream(BYTES_COLINDEX_NOTNULL);
  byte[] cbuf = new byte[3];
  int len = actual.read(cbuf, 0, cbuf.length);
  assertArrayEquals(ByteArray.copyFrom("BAR").toByteArray(), cbuf);
  assertEquals(3, len);
  assertEquals(false, subject.wasNull());
  assertNull(subject.getUnicodeStream(BYTES_COLINDEX_NULL));
  assertTrue(subject.wasNull());
}
 
Example #22
Source File: AvroRecordConverter.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
private static Optional<ByteArray> readBytes(
    GenericRecord record, Schema.Type avroType, String fieldName) {
  switch (avroType) {
    case BYTES:
      return Optional.ofNullable((ByteBuffer) record.get(fieldName)).map(ByteArray::copyFrom);
    case STRING:
      return Optional.ofNullable((Utf8) record.get(fieldName))
          .map(Utf8::toString)
          .map(ByteArray::copyFrom);
    default:
      throw new IllegalArgumentException("Cannot interpret " + avroType + " as BYTES");
  }
}
 
Example #23
Source File: CloudSpannerResultSetMetaDataTest.java    From spanner-jdbc with MIT License 5 votes vote down vote up
private Value getDefaultValue(Type type, int row) {
  if (type == Type.bool())
    return Value.bool(Boolean.TRUE);
  if (type == Type.bytes())
    return Value.bytes(ByteArray.copyFrom("test byte array " + row));
  if (type == Type.date())
    return Value.date(com.google.cloud.Date.fromYearMonthDay(2018, 4, 1));
  if (type == Type.float64())
    return Value.float64(123.45D);
  if (type == Type.int64())
    return Value.int64(12345L);
  if (type == Type.string())
    return Value.string("test value " + row);
  if (type == Type.timestamp())
    return Value.timestamp(com.google.cloud.Timestamp.now());

  if (type.getCode() == Code.ARRAY && type.getArrayElementType() == Type.bool())
    return Value.boolArray(Arrays.asList(Boolean.TRUE, Boolean.FALSE));
  if (type.getCode() == Code.ARRAY && type.getArrayElementType() == Type.bytes())
    return Value.bytesArray(Arrays.asList(ByteArray.copyFrom("test byte array " + row),
        ByteArray.copyFrom("test byte array " + row)));
  if (type.getCode() == Code.ARRAY && type.getArrayElementType() == Type.date())
    return Value.dateArray(Arrays.asList(com.google.cloud.Date.fromYearMonthDay(2018, 4, 1),
        com.google.cloud.Date.fromYearMonthDay(2018, 4, 2)));
  if (type.getCode() == Code.ARRAY && type.getArrayElementType() == Type.float64())
    return Value.float64Array(Arrays.asList(123.45D, 543.21D));
  if (type.getCode() == Code.ARRAY && type.getArrayElementType() == Type.int64())
    return Value.int64Array(Arrays.asList(12345L, 54321L));
  if (type.getCode() == Code.ARRAY && type.getArrayElementType() == Type.string())
    return Value.stringArray(Arrays.asList("test value " + row, "test value " + row));
  if (type.getCode() == Code.ARRAY && type.getArrayElementType() == Type.timestamp())
    return Value.timestampArray(
        Arrays.asList(com.google.cloud.Timestamp.now(), com.google.cloud.Timestamp.now()));
  return null;
}
 
Example #24
Source File: SpannerConverterTest.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
@Test
public void testBytes() {
  assertEquals(
      "\"dGVzdA==\"",
      structCsvPrinter.print(
          Struct.newBuilder().set("col").to(Value.bytes(ByteArray.copyFrom("test"))).build()));
}
 
Example #25
Source File: SpannerConverterTest.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
@Test
public void testBytesArray() {
  assertEquals(
      "\"[\"\"dGVzdA==\"\"]\"",
      structCsvPrinter.print(
          Struct.newBuilder()
              .set("col")
              .to(Value.bytesArray(Collections.singletonList(ByteArray.copyFrom("test"))))
              .build()));
}
 
Example #26
Source File: SpannerRecordConverterTest.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
@Test
public void nulls() {
  Ddl ddl = Ddl.builder()
      .createTable("users")
      .column("id").int64().notNull().endColumn()
      .column("age").int64().endColumn()
      .column("name").string().max().endColumn()
      .column("bytes").bytes().max().endColumn()
      .column("date").date().endColumn()
      .column("ts").timestamp().endColumn()
      .primaryKey().asc("id").end()
      .endTable()
      .build();
  Schema schema = converter.convert(ddl).iterator().next();
  SpannerRecordConverter recordConverter = new SpannerRecordConverter(schema);
  Struct struct = Struct.newBuilder()
      .set("id").to(1L)
      .set("age").to((Long) null)
      .set("name").to((String) null)
      .set("bytes").to((ByteArray) null)
      .set("date").to((Date) null)
      .set("ts").to((Timestamp) null)
      .build();

  GenericRecord avroRecord = recordConverter.convert(struct);

  assertThat(avroRecord.get("id"), equalTo(1L));
  assertThat(avroRecord.get("age"), is((Long) null));
  assertThat(avroRecord.get("name"), is((String) null));
  assertThat(avroRecord.get("bytes"), is((ByteArray) null));
  assertThat(avroRecord.get("date"), is((String) null));
  assertThat(avroRecord.get("ts"), is((String) null));
}
 
Example #27
Source File: QueueMessage.java    From spanner-event-exporter with Apache License 2.0 4 votes vote down vote up
static QueueMessage create(
    String queueName, String id, String key, ByteArray value, Timestamp timestamp) {
  return new AutoValue_QueueMessage(queueName, id, key, value, timestamp);
}
 
Example #28
Source File: MutationKeyEncoderTest.java    From beam with Apache License 2.0 4 votes vote down vote up
@Test
public void bytesKeys() throws Exception {
  SpannerSchema.Builder builder = SpannerSchema.builder();

  builder.addColumn("test", "key", "BYTES");
  builder.addKeyPart("test", "key", false);

  builder.addColumn("test", "keydesc", "BYTES");
  builder.addKeyPart("test", "keydesc", true);

  SpannerSchema schema = builder.build();

  List<Mutation> sortedMutations =
      Arrays.asList(
          Mutation.newInsertOrUpdateBuilder("test")
              .set("key")
              .to(ByteArray.fromBase64("abc"))
              .set("keydesc")
              .to(ByteArray.fromBase64("zzz"))
              .build(),
          Mutation.newInsertOrUpdateBuilder("test")
              .set("key")
              .to(ByteArray.fromBase64("xxx"))
              .set("keydesc")
              .to((ByteArray) null)
              .build(),
          Mutation.newInsertOrUpdateBuilder("test")
              .set("key")
              .to(ByteArray.fromBase64("xxx"))
              .set("keydesc")
              .to(ByteArray.fromBase64("zzzz"))
              .build(),
          Mutation.newInsertOrUpdateBuilder("test")
              .set("key")
              .to(ByteArray.fromBase64("xxx"))
              .set("keydesc")
              .to(ByteArray.fromBase64("ssss"))
              .build(),
          Mutation.newInsertOrUpdateBuilder("test")
              .set("key")
              .to(ByteArray.fromBase64("xxx"))
              .set("keydesc")
              .to(ByteArray.fromBase64("aaa"))
              .build());

  verifyEncodedOrdering(schema, sortedMutations);
}
 
Example #29
Source File: RandomValueGenerator.java    From DataflowTemplates with Apache License 2.0 4 votes vote down vote up
private ByteArray randomByteArray(Integer size) {
  size = size == -1 ? 20 : size;
  byte[] bytes = new byte[size];
  random.nextBytes(bytes);
  return ByteArray.copyFrom(bytes);
}
 
Example #30
Source File: RandomInsertMutationGenerator.java    From DataflowTemplates with Apache License 2.0 4 votes vote down vote up
public Mutation generateMutation(Map<String, Value> overrides) {
  Mutation.WriteBuilder builder = Mutation.newInsertOrUpdateBuilder(randomCase(table.name()));
  for (Map.Entry<String, Iterator<Value>> values : valueGenerators.entrySet()) {
    String columnName = values.getKey();
    Value value = overrides.get(columnName);
    if (value == null) {
      value = values.getValue().next();
    }
    switch (value.getType().getCode()) {
      case BOOL:
        Boolean bool = value.isNull() ? null : value.getBool();
        builder.set(columnName).to(bool);
        break;
      case INT64:
        Long l = value.isNull() ? null : value.getInt64();
        builder.set(columnName).to(l);
        break;
      case FLOAT64:
        Double f = value.isNull() ? null : value.getFloat64();
        builder.set(columnName).to(f);
        break;
      case BYTES:
        ByteArray bytes = value.isNull() ? null : value.getBytes();
        builder.set(columnName).to(bytes);
        break;
      case STRING:
        String string = value.isNull() ? null : value.getString();
        builder.set(columnName).to(string);
        break;
      case TIMESTAMP:
        Timestamp timestamp = value.isNull() ? null : value.getTimestamp();
        builder.set(columnName).to(timestamp);
        break;
      case DATE:
        Date date = value.isNull() ? null : value.getDate();
        builder.set(columnName).to(date);
        break;
      case ARRAY:
        switch (value.getType().getArrayElementType().getCode()) {
          case BOOL:
            List<Boolean> bools = value.isNull() ? null : value.getBoolArray();
            builder.set(columnName).toBoolArray(bools);
            break;
          case INT64:
            List<Long> longs = value.isNull() ? null : value.getInt64Array();
            builder.set(columnName).toInt64Array(longs);
            break;
          case FLOAT64:
            List<Double> doubles = value.isNull() ? null : value.getFloat64Array();
            builder.set(columnName).toFloat64Array(doubles);
            break;
          case BYTES:
            List<ByteArray> bytesArray = value.isNull() ? null : value.getBytesArray();
            builder.set(columnName).toBytesArray(bytesArray);
            break;
          case STRING:
            List<String> strings = value.isNull() ? null : value.getStringArray();
            builder.set(columnName).toStringArray(strings);
            break;
          case TIMESTAMP:
            List<Timestamp> timestamps = value.isNull() ? null : value.getTimestampArray();
            builder.set(columnName).toTimestampArray(timestamps);
            break;
          case DATE:
            List<Date> dates = value.isNull() ? null : value.getDateArray();
            builder.set(columnName).toDateArray(dates);
            break;
        }
        break;
      default:
        throw new IllegalArgumentException("Unknown toValue " + value);
    }
  }
  return builder.build();
}