Search in sources :

Example 1 with Definition

use of com.datastax.driver.core.ColumnDefinitions.Definition in project presto by prestodb.

the class RowUtil method createSingleStringRow.

public static Row createSingleStringRow(String value, int protocolVersion) {
    ColumnDefinitions definitions = new ColumnDefinitions(new Definition[] { new Definition("keyspace", "table", "column", DataType.ascii()) }, CodecRegistry.DEFAULT_INSTANCE);
    ByteBuffer data = ByteBuffer.wrap(value.getBytes(UTF_8));
    return ArrayBackedRow.fromData(definitions, null, ProtocolVersion.fromInt(protocolVersion), ImmutableList.of(data));
}
Also used : Definition(com.datastax.driver.core.ColumnDefinitions.Definition) ByteBuffer(java.nio.ByteBuffer)

Example 2 with Definition

use of com.datastax.driver.core.ColumnDefinitions.Definition in project GNS by MobilityFirst.

the class CassandraRecords method retrieveJSONObjectFromRow.

private JSONObject retrieveJSONObjectFromRow(Row row) {
    //JSONObject json = new JSONObject();
    StringBuilder result = new StringBuilder();
    result.append("{");
    String prefix = "";
    for (Definition def : row.getColumnDefinitions().asList()) {
        String name = def.getName();
        String value = row.getString(name);
        if (value != null) {
            // Building the JSON string here
            DatabaseConfig.getLogger().log(Level.FINER, "Name = {0} value = {1}", new Object[] { name, value });
            result.append(prefix);
            result.append("\"");
            result.append(name);
            result.append("\"");
            result.append(":");
            // Only quote them if they aren't a JSON array or object
            if (!value.startsWith("[") && !value.startsWith("{")) {
                result.append("\"");
            }
            result.append(value);
            // Only quote them if they aren't a JSON array or object
            if (!value.startsWith("[") && !value.startsWith("{")) {
                result.append("\"");
            }
            prefix = ",";
        }
    }
    result.append("}");
    //System.out.println(result);
    try {
        return new JSONObject(result.toString());
    } catch (JSONException e) {
        DatabaseConfig.getLogger().log(Level.WARNING, "Problem creating JSON object: {0}", e);
        return null;
    }
//return json;
}
Also used : JSONObject(org.json.JSONObject) Definition(com.datastax.driver.core.ColumnDefinitions.Definition) JSONException(org.json.JSONException)

Example 3 with Definition

use of com.datastax.driver.core.ColumnDefinitions.Definition in project ats-framework by Axway.

the class CassandraDbProvider method getColumnInfo.

/**
     * 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.asJavaClass().getSimpleName();
        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;
}
Also used : HashMap(java.util.HashMap) ResultSet(com.datastax.driver.core.ResultSet) Definition(com.datastax.driver.core.ColumnDefinitions.Definition) DataType(com.datastax.driver.core.DataType)

Example 4 with Definition

use of com.datastax.driver.core.ColumnDefinitions.Definition in project ats-framework by Axway.

the class CassandraDbProvider method select.

@Override
public DbRecordValuesList[] select(DbQuery dbQuery, DbReturnModes dbReturnMode) throws DbException {
    connect();
    ArrayList<DbRecordValuesList> dbRecords = new ArrayList<DbRecordValuesList>();
    String sqlQuery = dbQuery.getQuery();
    if (allowFiltering) {
        sqlQuery += " ALLOW FILTERING";
    }
    if (log.isDebugEnabled()) {
        log.debug(sqlQuery);
    }
    ResultSet results = session.execute(sqlQuery);
    int currentRow = 0;
    Iterator<Row> it = results.iterator();
    while (it.hasNext()) {
        Row row = it.next();
        currentRow++;
        if (log.isDebugEnabled()) {
            log.debug("Result row number: " + currentRow);
        }
        DbRecordValuesList recordList = new DbRecordValuesList();
        for (Definition columnDefinition : row.getColumnDefinitions()) {
            DbColumn dbColumn = new DbColumn(columnDefinition.getTable(), columnDefinition.getName());
            dbColumn.setColumnType(columnDefinition.getType().getName().toString());
            Object value = extractObjectFromResultSet(row, columnDefinition);
            DbRecordValue recordValue = new DbRecordValue(dbColumn, value);
            recordList.add(recordValue);
        }
        dbRecords.add(recordList);
    }
    return dbRecords.toArray(new DbRecordValuesList[] {});
}
Also used : DbColumn(com.axway.ats.core.dbaccess.DbColumn) DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) DbRecordValue(com.axway.ats.core.dbaccess.DbRecordValue) ArrayList(java.util.ArrayList) ResultSet(com.datastax.driver.core.ResultSet) Definition(com.datastax.driver.core.ColumnDefinitions.Definition) Row(com.datastax.driver.core.Row)

Aggregations

Definition (com.datastax.driver.core.ColumnDefinitions.Definition)4 ResultSet (com.datastax.driver.core.ResultSet)2 DbColumn (com.axway.ats.core.dbaccess.DbColumn)1 DbRecordValue (com.axway.ats.core.dbaccess.DbRecordValue)1 DbRecordValuesList (com.axway.ats.core.dbaccess.DbRecordValuesList)1 DataType (com.datastax.driver.core.DataType)1 Row (com.datastax.driver.core.Row)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1