本文整理汇总了Java中com.datastax.driver.mapping.annotations.PartitionKey类的典型用法代码示例。如果您正苦于以下问题:Java PartitionKey类的具体用法?Java PartitionKey怎么用?Java PartitionKey使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PartitionKey类属于com.datastax.driver.mapping.annotations包,在下文中一共展示了PartitionKey类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: generatePrimaryKey
import com.datastax.driver.mapping.annotations.PartitionKey; //导入依赖的package包/类
private static String generatePrimaryKey(Class<?> c) throws Exception {
Field[] fields = c.getFields();
List<PartitionKey> partitionKeys = new LinkedList<>();
Map<PartitionKey, String> partitionKeyToFieldName = new HashMap<>();
List<String> clusterColumns = new LinkedList<>();
for (Field f : fields) {
PartitionKey partitionKey = f.getAnnotation(PartitionKey.class);
ClusteringColumn clusterColumn = f.getAnnotation(ClusteringColumn.class);
if (partitionKey != null) {
partitionKeys.add(partitionKey);
partitionKeyToFieldName.put(partitionKey, getNameOfField(f));
// partitonKeys.
}
if (clusterColumn != null) {
clusterColumns.add(getNameOfField(f));
}
}
if (partitionKeys.size() == 0) {
throw new Exception("Partition key annotations are required!");
}
String partitonKey =
partitionKeys.stream().sorted((p1, p2) -> Integer.compare(p1.value(), p2.value()))
.map((p) -> partitionKeyToFieldName.get(p)).reduce((p1, p2) -> p1 + ", " + p2).get();
boolean multiPartitionKey = partitionKeys.size() > 1;
String result = "PRIMARY KEY (";
if (multiPartitionKey) {
result += "(";
}
result += partitonKey;
if (multiPartitionKey) {
result += ")";
}
if (clusterColumns != null) {
result += ", ";
result += clusterColumns.stream().reduce((c1, c2) -> c1 + ", " + c2).get();
}
result += ")";
return result;
}
示例2: getPartitionKeyAnnotation
import com.datastax.driver.mapping.annotations.PartitionKey; //导入依赖的package包/类
/**
* Get the PartitionKey annotation for a Table field.
* @param position the order of the field in the partition key
* @return the annotation
*/
public static AnnotationSpec getPartitionKeyAnnotation(int position) {
return AnnotationSpec.builder(PartitionKey.class).addMember("value", "$L", position).build();
}