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));
}
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;
}
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;
}
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[] {});
}
Aggregations