Java Code Examples for com.google.cloud.spanner.Type#Code

The following examples show how to use com.google.cloud.spanner.Type#Code . 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: RandomDdlGenerator.java    From DataflowTemplates with Apache License 2.0 6 votes vote down vote up
private Type typeOf(Type.Code code) {
  switch (code) {
    case BOOL:
      return Type.bool();
    case FLOAT64:
      return Type.float64();
    case STRING:
      return Type.string();
    case BYTES:
      return Type.bytes();
    case TIMESTAMP:
      return Type.timestamp();
    case DATE:
      return Type.date();
    case INT64:
      return Type.int64();
  }
  throw new IllegalArgumentException("Arrays and Structs are not supported");
}
 
Example 2
Source File: SpannerConverters.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
/**
 * Function with a series of switch cases to determine the parsing function for a specific column
 * type:
 *
 * <p>- Primitive types such as Boolean, Long, Int, and String are converted using toString
 * parser for primitive types.
 * - Date and Timestamp use toString method, for example 2018-03-26 and 1970-01-01T00:00:00Z
 * - Byte arrays use base64 encoding, so for example "test" transforms to "dGVzdA=="
 */
private static BiFunction<Struct, String, String> getColumnParser(Type.Code columnType) {
  switch (columnType) {
    case BOOL:
      return nullSafeColumnParser(
          (currentRow, columnName) -> Boolean.toString(currentRow.getBoolean(columnName)));
    case INT64:
      return nullSafeColumnParser(
          (currentRow, columnName) -> Long.toString(currentRow.getLong(columnName)));
    case FLOAT64:
      return nullSafeColumnParser(
          ((currentRow, columnName) -> Double.toString(currentRow.getDouble(columnName))));
    case STRING:
      return nullSafeColumnParser(Struct::getString);
    case BYTES:
      return nullSafeColumnParser(
          (currentRow, columnName) ->
              Base64.getEncoder().encodeToString(currentRow.getBytes(columnName).toByteArray()));
    case DATE:
      return nullSafeColumnParser(
          (currentRow, columnName) -> currentRow.getDate(columnName).toString());
    case TIMESTAMP:
      return nullSafeColumnParser(
          (currentRow, columnName) -> currentRow.getTimestamp(columnName).toString());
    case ARRAY:
      return nullSafeColumnParser(SpannerConverters::parseArrayValue);
    default:
      throw new RuntimeException("Unsupported type: " + columnType);
  }
}
 
Example 3
Source File: RandomDdlGenerator.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
private Column generateColumn(Type.Code[] codes, int arrayPercentage) {
  int length = 1 + getRandom().nextInt(getMaxIdLength());
  String name = generateIdentifier(length);
  Type type = generateType(codes, arrayPercentage);
  int size = -1;
  boolean nullable = getRandom().nextBoolean();
  return Column.builder().name(name).type(type).size(size).notNull(nullable).autoBuild();
}
 
Example 4
Source File: SpannerSchemaUtils.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
private String getTypeDdlString(Type.Code type, boolean isArray,
		OptionalLong dataLength, boolean isNotNull, boolean isCommitTimestamp) {
	return (isCommitTimestamp ? Type.Code.TIMESTAMP.toString()
			: getTypeDdlStringWithLength(type, isArray, dataLength))
			+ (isNotNull ? " NOT NULL" : "")
			+ (isCommitTimestamp ? " OPTIONS (allow_commit_timestamp=true)" : "");
}
 
Example 5
Source File: SpannerSchemaUtils.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
private String getTypeDdlStringWithLength(Type.Code type, boolean isArray,
		OptionalLong dataLength) {
	Assert.notNull(type, "A valid Spanner column type is required.");
	if (isArray) {
		return "ARRAY<" + getTypeDdlStringWithLength(type, false, dataLength) + ">";
	}
	return type.toString() + ((type == Type.Code.STRING || type == Type.Code.BYTES)
			? "(" + (dataLength.isPresent() ? dataLength.getAsLong() : "MAX") + ")"
			: "");
}
 
Example 6
Source File: StructAccessor.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
List getListValue(String colName) {
	if (this.struct.getColumnType(colName).getCode() != Code.ARRAY) {
		throw new SpannerDataException("Column is not an ARRAY type: " + colName);
	}
	Type.Code innerTypeCode = this.struct.getColumnType(colName).getArrayElementType().getCode();
	Class clazz = SpannerTypeMapper.getSimpleJavaClassFor(innerTypeCode);
	BiFunction<Struct, String, List> readMethod = readIterableMapping.get(clazz);
	return readMethod.apply(this.struct, colName);
}
 
Example 7
Source File: MutationKeyEncoder.java    From beam with Apache License 2.0 5 votes vote down vote up
private void encodeKey(OrderedCode orderedCode, Mutation m) {
  Map<String, Value> mutationMap = mutationAsMap(m);
  for (SpannerSchema.KeyPart part : schema.getKeyParts(m.getTable())) {
    Value val = mutationMap.get(part.getField());
    if (val == null || val.isNull()) {
      if (part.isDesc()) {
        orderedCode.writeInfinityDecreasing();
      } else {
        orderedCode.writeInfinity();
      }
    } else {
      Type.Code code = val.getType().getCode();
      switch (code) {
        case BOOL:
          writeNumber(orderedCode, part, (long) (val.getBool() ? 0 : 1));
          break;
        case INT64:
          writeNumber(orderedCode, part, val.getInt64());
          break;
        case FLOAT64:
          writeNumber(orderedCode, part, Double.doubleToLongBits(val.getFloat64()));
          break;
        case STRING:
          writeString(orderedCode, part, val.getString());
          break;
        case BYTES:
          writeBytes(orderedCode, part, val.getBytes());
          break;
        case TIMESTAMP:
          writeTimestamp(orderedCode, part, val.getTimestamp());
          break;
        case DATE:
          writeNumber(orderedCode, part, encodeDate(val.getDate()));
          break;
        default:
          throw new IllegalArgumentException("Unknown type " + val.getType());
      }
    }
  }
}
 
Example 8
Source File: RandomDdlGenerator.java    From DataflowTemplates with Apache License 2.0 4 votes vote down vote up
private Type generateType(Type.Code[] codes, int arrayPercentage) {
  boolean isArray = getRandom().nextInt(100) <= arrayPercentage;
  Type.Code code = randomCode(codes);
  return isArray ? Type.array(typeOf(code)) : typeOf(code);
}
 
Example 9
Source File: RandomDdlGenerator.java    From DataflowTemplates with Apache License 2.0 4 votes vote down vote up
private Type.Code randomCode(Type.Code[] codes) {
  return codes[getRandom().nextInt(codes.length)];
}
 
Example 10
Source File: SpannerSchemaUtils.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
String getColumnDdlString(SpannerPersistentProperty spannerPersistentProperty,
		SpannerEntityProcessor spannerEntityProcessor) {
	Class columnType = spannerPersistentProperty.getType();
	String columnName = spannerPersistentProperty.getColumnName() + " ";
	Class spannerJavaType;
	Type.Code spannerColumnType = spannerPersistentProperty
			.getAnnotatedColumnItemType();
	if (ConversionUtils.isIterableNonByteArrayType(columnType)) {
		Class innerType = spannerPersistentProperty.getColumnInnerType();
		spannerJavaType = spannerEntityProcessor
				.getCorrespondingSpannerJavaType(innerType, true);

		if (spannerColumnType == null) {
			spannerColumnType = SpannerTypeMapper.getSimpleTypeCodeForJavaType(spannerJavaType);
		}

		if (spannerColumnType == null) {
			throw new SpannerDataException(
					"Could not find suitable Cloud Spanner column inner type for "
							+ "property type: " + innerType);
		}
		return columnName + getTypeDdlString(spannerColumnType, true,
				spannerPersistentProperty.getMaxColumnLength(),
				spannerPersistentProperty.isGenerateSchemaNotNull(),
				spannerPersistentProperty.isCommitTimestamp());
	}
	spannerJavaType = spannerEntityProcessor
				.getCorrespondingSpannerJavaType(columnType, false);

	if (spannerJavaType == null) {
		throw new SpannerDataException(
				"The currently configured custom type converters cannot "
						+ "convert the given type to a Cloud Spanner-compatible column type: "
						+ columnType);
	}

	if (spannerColumnType == null) {
		spannerColumnType = spannerJavaType.isArray()
				? SpannerTypeMapper.getArrayTypeCodeForJavaType(spannerJavaType)
				: SpannerTypeMapper.getSimpleTypeCodeForJavaType(spannerJavaType);
	}
	if (spannerColumnType == null) {
		throw new SpannerDataException(
				"Could not find suitable Cloud Spanner column type for property "
						+ "type :" + columnType);
	}

	return columnName + getTypeDdlString(spannerColumnType, spannerJavaType.isArray(),
			spannerPersistentProperty.getMaxColumnLength(),
			spannerPersistentProperty.isGenerateSchemaNotNull(),
			spannerPersistentProperty.isCommitTimestamp());
}
 
Example 11
Source File: SpannerTypeMapper.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
public static Class getSimpleJavaClassFor(Type.Code code) {
	return SPANNER_SIMPLE_COLUMN_CODES_TO_JAVA_TYPE_MAPPING.get(code);
}
 
Example 12
Source File: SpannerTypeMapper.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
public static Class getArrayJavaClassFor(Type.Code code) {
	return SPANNER_ARRAY_COLUMN_CODES_TO_JAVA_TYPE_MAPPING.get(code);
}
 
Example 13
Source File: SpannerTypeMapper.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
public static Type.Code getSimpleTypeCodeForJavaType(Class spannerJavaType) {
	return JAVA_TYPE_TO_SPANNER_SIMPLE_COLUMN_TYPE_MAPPING.get(spannerJavaType);
}
 
Example 14
Source File: SpannerTypeMapper.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
public static Type.Code getArrayTypeCodeForJavaType(Class spannerJavaType) {
	return JAVA_TYPE_TO_SPANNER_ARRAY_COLUMN_TYPE_MAPPING.get(spannerJavaType);
}