本文整理匯總了Java中com.datastax.driver.core.DataType類的典型用法代碼示例。如果您正苦於以下問題:Java DataType類的具體用法?Java DataType怎麽用?Java DataType使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
DataType類屬於com.datastax.driver.core包,在下文中一共展示了DataType類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getColValue
import com.datastax.driver.core.DataType; //導入依賴的package包/類
public Object getColValue(Row row, String colName, DataType colType){
switch(colType.getName()){
case VARCHAR:
return row.getString(colName);
case UUID:
return row.getUUID(colName);
case VARINT:
return row.getVarint(colName);
case BIGINT:
return row.getLong(colName);
case INT:
return row.getInt(colName);
case FLOAT:
return row.getFloat(colName);
case DOUBLE:
return row.getDouble(colName);
case BOOLEAN:
return row.getBool(colName);
case MAP:
return row.getMap(colName, String.class, String.class);
default:
return null;
}
}
示例2: getColumnInfo
import com.datastax.driver.core.DataType; //導入依賴的package包/類
/**
* Returns a map with column name as key and column date type as value.
*
* The value might be as simple as "Boolean" or more complex like
* - "Set|Boolean"
* - "List|String"
* - "Map|String|Integer"
* these are cases when the data type is a container of primitive data types.
*
* @param tableName
* @return
* @throws DbException
*/
public Map<String, String> getColumnInfo(
String tableName ) throws DbException {
connect();
ResultSet results = session.execute("SELECT * FROM " + this.dbName + "." + tableName + " LIMIT 1");
Map<String, String> columnInfo = new HashMap<String, String>();
for (Definition columnDefinition : results.getColumnDefinitions()) {
DataType dataType = columnDefinition.getType();
String dataTypeName = dataType.getName().name();
if ("Set".equalsIgnoreCase(dataTypeName)) {
dataTypeName = dataTypeName + "|" + dataType.getTypeArguments().get(0);
} else if ("List".equalsIgnoreCase(dataTypeName)) {
dataTypeName = dataTypeName + "|" + dataType.getTypeArguments().get(0);
} else if ("Map".equalsIgnoreCase(dataTypeName)) {
dataTypeName = dataTypeName + "|" + dataType.getTypeArguments().get(0) + "|"
+ dataType.getTypeArguments().get(1);
}
columnInfo.put(columnDefinition.getName(), dataTypeName);
}
return columnInfo;
}
示例3: convertToCQLDataType
import com.datastax.driver.core.DataType; //導入依賴的package包/類
public static String convertToCQLDataType(DataType type,Object valueObj){
String value ="";
switch (type.getName()) {
case UUID:
value = valueObj+"";
break;
case TEXT: case VARCHAR:
String valueString = valueObj+"";
valueString = valueString.replace("'", "''");
value = "'"+valueString+"'";
break;
case MAP:{
Map<String,Object> otMap = (Map<String,Object>)valueObj;
value = "{"+jsonMaptoSqlString(otMap, ",")+"}";
break;
}
default:
value = valueObj+"";
break;
}
return value;
}
示例4: convertToActualDataType
import com.datastax.driver.core.DataType; //導入依賴的package包/類
public static Object convertToActualDataType(DataType colType,Object valueObj){
String valueObjString = valueObj+"";
switch(colType.getName()){
case UUID:
return UUID.fromString(valueObjString);
case VARINT:
return BigInteger.valueOf(Long.parseLong(valueObjString));
case BIGINT:
return Long.parseLong(valueObjString);
case INT:
return Integer.parseInt(valueObjString);
case FLOAT:
return Float.parseFloat(valueObjString);
case DOUBLE:
return Double.parseDouble(valueObjString);
case BOOLEAN:
return Boolean.parseBoolean(valueObjString);
case MAP:
return (Map<String,Object>)valueObj;
default:
return valueObjString;
}
}
示例5: insertRow
import com.datastax.driver.core.DataType; //導入依賴的package包/類
public boolean insertRow(String tablename, Map<String, Object> valuesMap, Map<String, String> consistencyInfo, JsonInsert insObj) throws Exception {
// Note: https://docs.datastax.com/en/cql/3.0/cql/cql_reference/insert_r.html
String[] parts = tablename.split("\\.");
KeyspaceMetadata ks = cluster.getMetadata().getKeyspace(parts[0]);
TableMetadata tableInfo = ks.getTable(parts[1]);
StringBuilder fields = new StringBuilder();
StringBuilder values = new StringBuilder();
String prefix = "";
for (String key : valuesMap.keySet()) {
fields.append(prefix).append(key);
Object valueObj = valuesMap.get(key);
DataType colType = tableInfo.getColumn(key).getType();
values.append(prefix).append(convertToSqlDataType(colType, valueObj));
prefix = ", ";
}
String suffix = getTTLSuffix(insObj);
String query = String.format("INSERT INTO %s (%s) VALUES (%s)%s;", tablename, fields.toString(), values.toString(), suffix);
LOG.debug(query);
String consistency = extractConsistencyInfo(tablename, consistencyInfo);
executeCreateQuery(query, consistency);
return false;
}
示例6: readRow
import com.datastax.driver.core.DataType; //導入依賴的package包/類
private Object readRow(final Row row, final String name, final DataType colType) {
switch (colType.getName()) {
case BIGINT:
return row.getLong(name);
case BOOLEAN:
return row.getBool(name);
case DOUBLE:
return row.getDouble(name);
case FLOAT:
return row.getFloat(name);
case INT:
return row.getInt(name);
case MAP:
return row.getMap(name, String.class, String.class);
case UUID:
return row.getUUID(name);
case TEXT:
case VARCHAR:
return row.getString(name);
case VARINT:
return row.getVarint(name);
// These are not supported right now....
// ASCII
// BLOB
// COUNTER
// CUSTOM
// DECIMAL
// INET
// LIST
// SET
// TIMESTAMP
// TIMEUUID
// TUPLE
// UDT
default:
return null;
}
}
示例7: getRowIdentifier
import com.datastax.driver.core.DataType; //導入依賴的package包/類
private RowIdentifier getRowIdentifier(String keyspace,String tablename, MultivaluedMap<String, String> rowParams){
String rowIdString="";
int counter =0;
TableMetadata tableInfo = MusicCore.returnColumnMetadata(keyspace, tablename);
String primaryKeyValue="";
for (MultivaluedMap.Entry<String, List<String>> entry : rowParams.entrySet()){
String keyName = entry.getKey();
List<String> valueList = entry.getValue();
String indValue = valueList.get(0);
DataType colType = tableInfo.getColumn(entry.getKey()).getType();
String formattedValue = MusicCore.convertToCQLDataType(colType,indValue);
if(counter ==0)
primaryKeyValue = primaryKeyValue+indValue;
rowIdString = rowIdString + keyName +"="+ formattedValue;
if(counter!=rowParams.size()-1)
rowIdString = rowIdString+" AND ";
counter = counter +1;
}
return new RowIdentifier(primaryKeyValue, rowIdString);
}
示例8: buildSchema
import com.datastax.driver.core.DataType; //導入依賴的package包/類
/**
* Build schema programmatically
* <p>
* DDL equivalent:
*
* <pre>
* CREATE TABLE messages (
* sessionId uuid,
* seqNo bigint,
* message blob,
* PRIMARY KEY (sessionId, seqNo ) );
* </pre>
*
* @throws StoreException if the store is not open
*
*/
public void buildSchema() throws StoreException {
if (session != null) {
// Appropriate for a local test only
session.execute(new SimpleStatement("CREATE KEYSPACE IF NOT EXISTS " + KEYSPACE_NAME
+ " WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }"));
System.out.format("Keyspace %s available\n", KEYSPACE_NAME);
Create create = SchemaBuilder.createTable(KEYSPACE_NAME, TABLE_NAME).ifNotExists()
.addPartitionKey(SESSION_ID_COLNAME, DataType.uuid())
.addClusteringColumn(SEQ_NO_COLNAME, DataType.bigint())
.addColumn(MESSAGE_COLNAME, DataType.blob());
ResultSet resultSet = session.execute(create);
System.out.format("Table %s available\n", TABLE_NAME);
} else {
throw new StoreException("Schema not created; store not open");
}
}
示例9: cqlType
import com.datastax.driver.core.DataType; //導入依賴的package包/類
private static DataType cqlType(FieldInfo info) {
if (String.class.equals(info.type())) {
return DataType.text();
} else if (Boolean.class.equals(info.type()) || Boolean.TYPE.equals(info.type())) {
return DataType.cboolean();
} else if (Long.class.equals(info.type()) || Long.TYPE.equals(info.type())) {
return DataType.cint();
} else if (Double.class.equals(info.type()) || Double.TYPE.equals(info.type())) {
return DataType.cint();
} else if (Float.class.equals(info.type()) || Float.TYPE.equals(info.type())) {
return DataType.cint();
} else if (Integer.class.equals(info.type()) || Integer.TYPE.equals(info.type())) {
return DataType.cint();
} else if (LocalDate.class.equals(info.type())) {
return DataType.date();
} else if (Enum.class.isAssignableFrom(info.type())) {
return DataType.set(DataType.text());
} else if (Collection.class.isAssignableFrom(info.type())) {
return DataType.set(DataType.text());
}
throw new IllegalArgumentException("unknown type " + info.type() + " for " + info.id());
}
示例10: cqlType
import com.datastax.driver.core.DataType; //導入依賴的package包/類
private static DataType cqlType(FieldInfo info) {
if (String.class.equals(info.type())) {
return text();
} else if (Boolean.class.equals(info.type()) || Boolean.TYPE.equals(info.type())) {
return DataType.cboolean();
} else if (Long.class.equals(info.type()) || Long.TYPE.equals(info.type())) {
return DataType.cint();
} else if (LocalDate.class.equals(info.type())) {
return DataType.date();
} else if (Enum.class.isAssignableFrom(info.type())) {
return DataType.set(text());
} else if (Collection.class.isAssignableFrom(info.type())) {
return DataType.set(text());
}
throw new IllegalArgumentException("unknown type " + info.type() + " for " + info.id());
}
示例11: buildColumnHandle
import com.datastax.driver.core.DataType; //導入依賴的package包/類
private CassandraColumnHandle buildColumnHandle(ColumnMetadata columnMeta, boolean partitionKey, boolean clusteringKey, int ordinalPosition, boolean hidden)
{
CassandraType cassandraType = CassandraType.getCassandraType(columnMeta.getType().getName());
List<CassandraType> typeArguments = null;
if (cassandraType != null && cassandraType.getTypeArgumentSize() > 0) {
List<DataType> typeArgs = columnMeta.getType().getTypeArguments();
switch (cassandraType.getTypeArgumentSize()) {
case 1:
typeArguments = ImmutableList.of(CassandraType.getCassandraType(typeArgs.get(0).getName()));
break;
case 2:
typeArguments = ImmutableList.of(CassandraType.getCassandraType(typeArgs.get(0).getName()), CassandraType.getCassandraType(typeArgs.get(1).getName()));
break;
default:
throw new IllegalArgumentException("Invalid type arguments: " + typeArgs);
}
}
boolean indexed = columnMeta.getIndex() != null;
return new CassandraColumnHandle(connectorId, columnMeta.getName(), ordinalPosition, cassandraType, typeArguments, partitionKey, clusteringKey, indexed, hidden);
}
示例12: getClassWithTypes
import com.datastax.driver.core.DataType; //導入依賴的package包/類
/**
* Handle getting the class names for parameterized types.
*
* @param type the cassandra data type to extract from
* @return the parameterized type result
*/
public static TypeResult getClassWithTypes(DataType type) {
ClassName outer = getRawType(type);
List<TypeName> generics = new ArrayList<>();
boolean hasFrozenType = false;
for(DataType genericType : type.getTypeArguments()) {
if(Udt.instance.isUdt(genericType)) {
generics.add(MetaData.getClassNameForUdt((UserType) genericType));
if(genericType.isFrozen()) {
hasFrozenType = true;
}
} else {
generics.add(getRawType(genericType).box());
}
}
return new TypeResult(ParameterizedTypeName.get(outer, generics.toArray(new TypeName[generics.size()])), hasFrozenType);
}
示例13: getSetter
import com.datastax.driver.core.DataType; //導入依賴的package包/類
/**
* Get a setter spec for a entity field.
*
* @param field the field name
* @param type the cassandra field type
* @return the setter method spec
*/
public static MethodSpec getSetter(String field, DataType type) {
String methodRoot = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, field);
String paramName = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, field);
MethodSpec.Builder spec;
if (type.getTypeArguments().size() == 0) {
if(Udt.instance.isUdt(type)) {
spec = MethodSpec.methodBuilder("set" + methodRoot).addParameter(MetaData.getClassNameForUdt((UserType) type), paramName);
} else {
spec = MethodSpec.methodBuilder("set" + methodRoot).addParameter(getRawType(type), paramName);
}
} else {
TypeResult result = getClassWithTypes(type);
spec = MethodSpec.methodBuilder("set" + methodRoot).addParameter(result.type, paramName);
}
spec.addModifiers(Modifier.PUBLIC).addStatement("this.$L = $L", paramName, paramName);
return spec.build();
}
示例14: getGetter
import com.datastax.driver.core.DataType; //導入依賴的package包/類
/**
* Get a getter spec for a entity field.
*
* @param field the field name
* @param type the cassandra field type
* @return the getter method spec
*/
public static MethodSpec getGetter(String field, DataType type) {
String methodRoot = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, field);
String paramName = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, field);
MethodSpec.Builder spec;
if (type.getTypeArguments().size() == 0) {
if(Udt.instance.isUdt(type)) {
spec = MethodSpec.methodBuilder("get" + methodRoot).returns(MetaData.getClassNameForUdt((UserType) type));
} else {
spec = MethodSpec.methodBuilder("get" + methodRoot).returns(getRawType(type));
}
} else {
TypeResult result = getClassWithTypes(type);
spec = MethodSpec.methodBuilder("get" + methodRoot).returns(result.type);
}
spec.addModifiers(Modifier.PUBLIC).addStatement("return $L", paramName);
return spec.build();
}
示例15: getValueFromObject
import com.datastax.driver.core.DataType; //導入依賴的package包/類
/**
* Gets field value as an object having Cassandra compatible type.
* This it could be stored directly into Cassandra without any conversions.
*
* @param obj Object instance.
* @param serializer {@link org.apache.ignite.cache.store.cassandra.serializer.Serializer} to use.
* @return Object to store in Cassandra table column.
*/
public Object getValueFromObject(Object obj, Serializer serializer) {
Object val = accessor.getValue(obj);
if (val == null)
return null;
DataType.Name cassandraType = PropertyMappingHelper.getCassandraType(val.getClass());
if (cassandraType != null)
return val;
if (serializer == null) {
throw new IllegalStateException("Can't serialize value from object '" +
val.getClass().getName() + "' field '" + name + "', cause there is no BLOB serializer specified");
}
return serializer.serialize(val);
}