Java Code Examples for com.datastax.driver.core.ColumnDefinitions#getName()

The following examples show how to use com.datastax.driver.core.ColumnDefinitions#getName() . 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: CassandraFactory.java    From database-transform-tool with Apache License 2.0 6 votes vote down vote up
/**
 * 描述: 查询数据表字段名(key:字段名,value:字段类型名)
 * 时间: 2017年11月15日 上午11:29:32
 * @author yi.zhang
 * @param table	表名
 * @return
 */
public Map<String,String> queryColumns(String table){
	try {
		String sql = "select * from "+table;
		ResultSet rs = session.execute(sql);
		ColumnDefinitions rscd = rs.getColumnDefinitions();
		int count = rscd.size();
		Map<String,String> reflect = new HashMap<String,String>();
		for (int i = 0; i < count; i++) {
			String column = rscd.getName(i);
			String type = rscd.getType(i).getName().name().toLowerCase();
			reflect.put(column, type);
		}
		return reflect;
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;
}
 
Example 2
Source File: CassandraPOJOOutputOperator.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
private void populateFieldInfosFromPojo(ColumnDefinitions rsMetaData)
{
  fieldInfos = Lists.newArrayList();
  Field[] fields = pojoClass.getDeclaredFields();
  for (int i = 0; i < rsMetaData.size(); i++) {
    String columnName = rsMetaData.getName(i);
    String pojoField = getMatchingField(fields, columnName);
    if (pojoField != null && pojoField.length() != 0) {
      fieldInfos.add(new FieldInfo(columnName, pojoField, null));
    } else {
      LOG.warn("Couldn't find corrosponding pojo field for column: " + columnName);
    }
  }
}
 
Example 3
Source File: CassandraDataHandler.java    From micro-integrator with Apache License 2.0 4 votes vote down vote up
/**
 * This method wraps result set data in to DataEntry and creates a list of DataEntry.
 *
 * @param tableName         Table Name
 * @param row               Row
 * @param columnDefinitions Column Definition
 * @return DataEntry
 * @throws ODataServiceFault
 */
private ODataEntry createDataEntryFromRow(String tableName, Row row, ColumnDefinitions columnDefinitions)
        throws ODataServiceFault {
    String paramValue;
    ODataEntry entry = new ODataEntry();
    //Creating a unique string to represent the
    try {
        for (int i = 0; i < columnDefinitions.size(); i++) {
            String columnName = columnDefinitions.getName(i);
            DataType columnType = columnDefinitions.getType(i);

            switch (columnType.getName()) {
                case ASCII:
                    paramValue = row.getString(i);
                    break;
                case BIGINT:
                    paramValue = row.isNull(i) ? null : ConverterUtil.convertToString(row.getLong(i));
                    break;
                case BLOB:
                    paramValue = this.base64EncodeByteBuffer(row.getBytes(i));
                    break;
                case BOOLEAN:
                    paramValue = row.isNull(i) ? null : ConverterUtil.convertToString(row.getBool(i));
                    break;
                case COUNTER:
                    paramValue = row.isNull(i) ? null : ConverterUtil.convertToString(row.getLong(i));
                    break;
                case DECIMAL:
                    paramValue = row.isNull(i) ? null : ConverterUtil.convertToString(row.getDecimal(i));
                    break;
                case DOUBLE:
                    paramValue = row.isNull(i) ? null : ConverterUtil.convertToString(row.getDouble(i));
                    break;
                case FLOAT:
                    paramValue = row.isNull(i) ? null : ConverterUtil.convertToString(row.getFloat(i));
                    break;
                case INET:
                    paramValue = row.getInet(i).toString();
                    break;
                case INT:
                    paramValue = row.isNull(i) ? null : ConverterUtil.convertToString(row.getInt(i));
                    break;
                case TEXT:
                    paramValue = row.getString(i);
                    break;
                case TIMESTAMP:
                    paramValue = row.isNull(i) ? null : ConverterUtil.convertToString(row.getDate(i));
                    break;
                case UUID:
                    paramValue = row.isNull(i) ? null : ConverterUtil.convertToString(row.getUUID(i));
                    break;
                case VARCHAR:
                    paramValue = row.getString(i);
                    break;
                case VARINT:
                    paramValue = row.isNull(i) ? null : ConverterUtil.convertToString(row.getVarint(i));
                    break;
                case TIMEUUID:
                    paramValue = row.isNull(i) ? null : ConverterUtil.convertToString(row.getUUID(i));
                    break;
                case LIST:
                    paramValue = row.isNull(i) ? null : Arrays.toString(row.getList(i, Object.class).toArray());
                    break;
                case SET:
                    paramValue = row.isNull(i) ? null : row.getSet(i, Object.class).toString();
                    break;
                case MAP:
                    paramValue = row.isNull(i) ? null : row.getMap(i, Object.class, Object.class).toString();
                    break;
                case UDT:
                    paramValue = row.isNull(i) ? null : row.getUDTValue(i).toString();
                    break;
                case TUPLE:
                    paramValue = row.isNull(i) ? null : row.getTupleValue(i).toString();
                    break;
                case CUSTOM:
                    paramValue = row.isNull(i) ? null : this.base64EncodeByteBuffer(row.getBytes(i));
                    break;
                default:
                    paramValue = row.getString(i);
                    break;
            }
            entry.addValue(columnName, paramValue);
        }
    } catch (DataServiceFault e) {
        throw new ODataServiceFault(e, "Error occurred when creating OData entry. :" + e.getMessage());
    }
    //Set E-Tag to the entity
    entry.addValue("ETag", ODataUtils.generateETag(this.configID, tableName, entry));
    return entry;
}
 
Example 4
Source File: QueryCassandra.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
/**
 * Creates an Avro schema from the given result set. The metadata (column definitions, data types, etc.) is used
 * to determine a schema for Avro.
 *
 * @param rs The result set from which an Avro schema will be created
 * @return An Avro schema corresponding to the given result set's metadata
 * @throws IOException If an error occurs during schema discovery/building
 */
public static Schema createSchema(final ResultSet rs) throws IOException {
    final ColumnDefinitions columnDefinitions = rs.getColumnDefinitions();
    final int nrOfColumns = (columnDefinitions == null ? 0 : columnDefinitions.size());
    String tableName = "NiFi_Cassandra_Query_Record";
    if (nrOfColumns > 0) {
        String tableNameFromMeta = columnDefinitions.getTable(1);
        if (!StringUtils.isBlank(tableNameFromMeta)) {
            tableName = tableNameFromMeta;
        }
    }

    final SchemaBuilder.FieldAssembler<Schema> builder = SchemaBuilder.record(tableName).namespace("any.data").fields();
    if (columnDefinitions != null) {
        for (int i = 0; i < nrOfColumns; i++) {

            DataType dataType = columnDefinitions.getType(i);
            if (dataType == null) {
                throw new IllegalArgumentException("No data type for column[" + i + "] with name " + columnDefinitions.getName(i));
            }

            // Map types from Cassandra to Avro where possible
            if (dataType.isCollection()) {
                List<DataType> typeArguments = dataType.getTypeArguments();
                if (typeArguments == null || typeArguments.size() == 0) {
                    throw new IllegalArgumentException("Column[" + i + "] " + dataType.getName()
                            + " is a collection but no type arguments were specified!");
                }
                // Get the first type argument, to be used for lists and sets
                DataType firstArg = typeArguments.get(0);
                if (dataType.equals(DataType.set(firstArg))
                        || dataType.equals(DataType.list(firstArg))) {
                    builder.name(columnDefinitions.getName(i)).type().unionOf().nullBuilder().endNull().and().array()
                            .items(getUnionFieldType(getPrimitiveAvroTypeFromCassandraType(firstArg))).endUnion().noDefault();
                } else {
                    // Must be an n-arg collection like map
                    DataType secondArg = typeArguments.get(1);
                    if (dataType.equals(DataType.map(firstArg, secondArg))) {
                        builder.name(columnDefinitions.getName(i)).type().unionOf().nullBuilder().endNull().and().map().values(
                                getUnionFieldType(getPrimitiveAvroTypeFromCassandraType(secondArg))).endUnion().noDefault();
                    }
                }
            } else {
                builder.name(columnDefinitions.getName(i))
                        .type(getUnionFieldType(getPrimitiveAvroTypeFromCassandraType(dataType))).noDefault();
            }
        }
    }
    return builder.endRecord();
}
 
Example 5
Source File: DatastaxColumnKey.java    From SimpleFlatMapper with MIT License 4 votes vote down vote up
public static DatastaxColumnKey of(ColumnDefinitions metaData, int column) {
	return new DatastaxColumnKey(metaData.getName(column), column , metaData.getType(column));
}
 
Example 6
Source File: QueryCassandra.java    From nifi with Apache License 2.0 4 votes vote down vote up
/**
 * Creates an Avro schema from the given result set. The metadata (column definitions, data types, etc.) is used
 * to determine a schema for Avro.
 *
 * @param rs The result set from which an Avro schema will be created
 * @return An Avro schema corresponding to the given result set's metadata
 * @throws IOException If an error occurs during schema discovery/building
 */
public static Schema createSchema(final ResultSet rs) throws IOException {
    final ColumnDefinitions columnDefinitions = rs.getColumnDefinitions();
    final int nrOfColumns = (columnDefinitions == null ? 0 : columnDefinitions.size());
    String tableName = "NiFi_Cassandra_Query_Record";
    if (nrOfColumns > 0) {
        String tableNameFromMeta = columnDefinitions.getTable(0);
        if (!StringUtils.isBlank(tableNameFromMeta)) {
            tableName = tableNameFromMeta;
        }
    }

    final SchemaBuilder.FieldAssembler<Schema> builder = SchemaBuilder.record(tableName).namespace("any.data").fields();
    if (columnDefinitions != null) {
        for (int i = 0; i < nrOfColumns; i++) {

            DataType dataType = columnDefinitions.getType(i);
            if (dataType == null) {
                throw new IllegalArgumentException("No data type for column[" + i + "] with name " + columnDefinitions.getName(i));
            }

            // Map types from Cassandra to Avro where possible
            if (dataType.isCollection()) {
                List<DataType> typeArguments = dataType.getTypeArguments();
                if (typeArguments == null || typeArguments.size() == 0) {
                    throw new IllegalArgumentException("Column[" + i + "] " + dataType.getName()
                            + " is a collection but no type arguments were specified!");
                }
                // Get the first type argument, to be used for lists and sets
                DataType firstArg = typeArguments.get(0);
                if (dataType.equals(DataType.set(firstArg))
                        || dataType.equals(DataType.list(firstArg))) {
                    builder.name(columnDefinitions.getName(i)).type().unionOf().nullBuilder().endNull().and().array()
                            .items(getUnionFieldType(getPrimitiveAvroTypeFromCassandraType(firstArg))).endUnion().noDefault();
                } else {
                    // Must be an n-arg collection like map
                    DataType secondArg = typeArguments.get(1);
                    if (dataType.equals(DataType.map(firstArg, secondArg))) {
                        builder.name(columnDefinitions.getName(i)).type().unionOf().nullBuilder().endNull().and().map().values(
                                getUnionFieldType(getPrimitiveAvroTypeFromCassandraType(secondArg))).endUnion().noDefault();
                    }
                }
            } else {
                builder.name(columnDefinitions.getName(i))
                        .type(getUnionFieldType(getPrimitiveAvroTypeFromCassandraType(dataType))).noDefault();
            }
        }
    }
    return builder.endRecord();
}