Java Code Examples for com.datastax.driver.core.ColumnDefinitions#getType()
The following examples show how to use
com.datastax.driver.core.ColumnDefinitions#getType() .
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: DatabaseSpec.java From bdt with Apache License 2.0 | 5 votes |
@SuppressWarnings("rawtypes") private void equalsColumns(ColumnDefinitions resCols, Map<String, String> dataTableColumns) { Iterator it = dataTableColumns.entrySet().iterator(); while (it.hasNext()) { Map.Entry e = (Map.Entry) it.next(); assertThat(resCols.toString()).as("The table not contains the column.").contains(e.getKey().toString()); DataType type = resCols.getType(e.getKey().toString()); assertThat(type.getName().toString()).as("The column type is not equals.").isEqualTo(e.getValue().toString()); } }
Example 2
Source File: CassandraQuery.java From micro-integrator with Apache License 2.0 | 4 votes |
private DataEntry getDataEntryFromRow(Row row, ColumnDefinitions defs) throws DataServiceFault { boolean useColumnNumbers = this.isUsingColumnNumbers(); DataType columnType; DataEntry entry = new DataEntry(); ParamValue paramValue = null; for (int i = 0; i < defs.size(); i++) { columnType = defs.getType(i); if (columnType.getName().equals(DataType.Name.ASCII)) { paramValue = new ParamValue(row.getString(i)); } else if (columnType.getName().equals(DataType.Name.VARCHAR)) { paramValue = new ParamValue(row.getString(i)); } else if (columnType.getName().equals(DataType.Name.TEXT)) { paramValue = new ParamValue(row.getString(i)); } else if (columnType.getName().equals(DataType.Name.BIGINT)) { paramValue = new ParamValue(row.isNull(i) ? null : Long.toString(row.getLong(i))); } else if (columnType.getName().equals(DataType.Name.BLOB)) { paramValue = new ParamValue(this.base64EncodeByteBuffer(row.getBytes(i))); } else if (columnType.getName().equals(DataType.Name.BOOLEAN)) { paramValue = new ParamValue(row.isNull(i) ? null : Boolean.toString(row.getBool(i))); } else if (columnType.getName().equals(DataType.Name.COUNTER)) { paramValue = new ParamValue(row.isNull(i) ? null : Long.toString(row.getLong(i))); } else if (columnType.getName().equals(DataType.Name.CUSTOM)) { paramValue = new ParamValue(this.base64EncodeByteBuffer(row.getBytes(i))); } else if (columnType.getName().equals(DataType.Name.DECIMAL)) { paramValue = new ParamValue(row.isNull(i) ? null : row.getDecimal(i).toString()); } else if (columnType.getName().equals(DataType.Name.DOUBLE)) { paramValue = new ParamValue(row.isNull(i) ? null : Double.toString(row.getDouble(i))); } else if (columnType.getName().equals(DataType.Name.FLOAT)) { paramValue = new ParamValue(row.isNull(i) ? null : Float.toString(row.getFloat(i))); } else if (columnType.getName().equals(DataType.Name.INET)) { paramValue = new ParamValue(row.isNull(i) ? null :row.getInet(i).toString()); } else if (columnType.getName().equals(DataType.Name.INT)) { paramValue = new ParamValue(row.isNull(i) ? null : Integer.toString(row.getInt(i))); } else if (columnType.getName().equals(DataType.Name.LIST)) { paramValue = new ParamValue(row.isNull(i) ? null : Arrays.toString(row.getList(i, Object.class) .toArray())); } else if (columnType.getName().equals(DataType.Name.MAP)) { paramValue = new ParamValue(row.getMap(i, Object.class, Object.class).toString()); } else if (columnType.getName().equals(DataType.Name.SET)) { paramValue = new ParamValue(row.getSet(i, Object.class).toString()); } else if (columnType.getName().equals(DataType.Name.TIMESTAMP)) { paramValue = new ParamValue(row.isNull(i) ? null : ConverterUtil.convertToString(row.getTimestamp(i))); } else if (columnType.getName().equals(DataType.Name.TIMEUUID)) { paramValue = new ParamValue(row.getUUID(i).toString()); } else if (columnType.getName().equals(DataType.Name.UUID)) { paramValue = new ParamValue(row.getUUID(i).toString()); } else if (columnType.getName().equals(DataType.Name.VARINT)) { paramValue = new ParamValue(row.getVarint(i).toString()); } else if (columnType.getName().equals(DataType.Name.UDT)) { paramValue = new ParamValue(row.isNull(i) ? null : ConverterUtil.convertToString(row.getUDTValue(i))); } else if (columnType.getName().equals(DataType.Name.TUPLE)) { paramValue = new ParamValue(row.isNull(i) ? null : ConverterUtil.convertToString(row.getTupleValue(i))); } else if (columnType.getName().equals(DataType.Name.TINYINT)) { paramValue = new ParamValue(row.isNull(i) ? null : Integer.toString(row.getByte(i))); } else if (columnType.getName().equals(DataType.Name.SMALLINT)) { paramValue = new ParamValue(row.isNull(i) ? null : Integer.toString(row.getShort(i))); } else if (columnType.getName().equals(DataType.Name.TIME)) { paramValue = new ParamValue(row.isNull(i) ? null : ConverterUtil.convertToString(row.getTime(i))); } else if (columnType.getName().equals(DataType.Name.DATE)) { paramValue = new ParamValue(row.isNull(i) ? null : ConverterUtil.convertToString(row.getDate(i))); } entry.addValue(useColumnNumbers ? Integer.toString(i) : defs.getName(i), paramValue); } return entry; }
Example 3
Source File: CassandraDataHandler.java From micro-integrator with Apache License 2.0 | 4 votes |
/** * 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 |
/** * Converts a result set into an Avro record and writes it to the given stream. * * @param rs The result set to convert * @param outStream The stream to which the Avro record will be written * @param timeout The max number of timeUnits to wait for a result set fetch to complete * @param timeUnit The unit of time (SECONDS, e.g.) associated with the timeout amount * @return The number of rows from the result set written to the stream * @throws IOException If the Avro record cannot be written * @throws InterruptedException If a result set fetch is interrupted * @throws TimeoutException If a result set fetch has taken longer than the specified timeout * @throws ExecutionException If any error occurs during the result set fetch */ public static long convertToAvroStream(final ResultSet rs, final OutputStream outStream, long timeout, TimeUnit timeUnit) throws IOException, InterruptedException, TimeoutException, ExecutionException { final Schema schema = createSchema(rs); final GenericRecord rec = new GenericData.Record(schema); final DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema); try (final DataFileWriter<GenericRecord> dataFileWriter = new DataFileWriter<>(datumWriter)) { dataFileWriter.create(schema, outStream); final ColumnDefinitions columnDefinitions = rs.getColumnDefinitions(); long nrOfRows = 0; if (columnDefinitions != null) { do { // Grab the ones we have int rowsAvailableWithoutFetching = rs.getAvailableWithoutFetching(); if (rowsAvailableWithoutFetching == 0) { // Get more if (timeout <= 0 || timeUnit == null) { rs.fetchMoreResults().get(); } else { rs.fetchMoreResults().get(timeout, timeUnit); } } for (Row row : rs) { for (int i = 0; i < columnDefinitions.size(); i++) { final DataType dataType = columnDefinitions.getType(i); if (row.isNull(i)) { rec.put(i, null); } else { rec.put(i, getCassandraObject(row, i, dataType)); } } dataFileWriter.append(rec); nrOfRows += 1; } } while (!rs.isFullyFetched()); } return nrOfRows; } }
Example 5
Source File: QueryCassandra.java From localization_nifi with Apache License 2.0 | 4 votes |
/** * 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 6
Source File: CassandraPOJOOutputOperator.java From attic-apex-malhar with Apache License 2.0 | 4 votes |
@Override public void activate(Context.OperatorContext context) { com.datastax.driver.core.ResultSet rs = store.getSession().execute("select * from " + store.keyspace + "." + tablename); final ColumnDefinitions rsMetaData = rs.getColumnDefinitions(); if (fieldInfos == null) { populateFieldInfosFromPojo(rsMetaData); } for (FieldInfo fieldInfo : getFieldInfos()) { // get the designated column's data type. final DataType type = rsMetaData.getType(fieldInfo.getColumnName()); columnDataTypes.add(type); final Object getter; final String getterExpr = fieldInfo.getPojoFieldExpression(); switch (type.getName()) { case ASCII: case TEXT: case VARCHAR: getter = PojoUtils.createGetter(pojoClass, getterExpr, String.class); break; case BOOLEAN: getter = PojoUtils.createGetterBoolean(pojoClass, getterExpr); break; case INT: getter = PojoUtils.createGetterInt(pojoClass, getterExpr); break; case BIGINT: case COUNTER: getter = PojoUtils.createGetterLong(pojoClass, getterExpr); break; case FLOAT: getter = PojoUtils.createGetterFloat(pojoClass, getterExpr); break; case DOUBLE: getter = PojoUtils.createGetterDouble(pojoClass, getterExpr); break; case DECIMAL: getter = PojoUtils.createGetter(pojoClass, getterExpr, BigDecimal.class); break; case SET: getter = PojoUtils.createGetter(pojoClass, getterExpr, Set.class); break; case MAP: getter = PojoUtils.createGetter(pojoClass, getterExpr, Map.class); break; case LIST: getter = PojoUtils.createGetter(pojoClass, getterExpr, List.class); break; case TIMESTAMP: getter = PojoUtils.createGetter(pojoClass, getterExpr, Date.class); break; case UUID: getter = PojoUtils.createGetter(pojoClass, getterExpr, UUID.class); break; default: getter = PojoUtils.createGetter(pojoClass, getterExpr, Object.class); break; } getters.add(getter); } super.activate(context); }
Example 7
Source File: CassandraPOJOInputOperator.java From attic-apex-malhar with Apache License 2.0 | 4 votes |
@Override public void activate(OperatorContext context) { Long keyToken; if (startRow != null) { if ((keyToken = fetchKeyTokenFromDB(startRow)) != null) { startRowToken = keyToken; } } com.datastax.driver.core.ResultSet rs = store.getSession().execute("select * from " + store.keyspace + "." + tablename + " LIMIT " + 1); ColumnDefinitions rsMetaData = rs.getColumnDefinitions(); if (query.contains("%t")) { query = query.replace("%t", tablename); } if (query.contains("%p")) { query = query.replace("%p", primaryKeyColumn); } if (query.contains("%l")) { query = query.replace("%l", limit + ""); } LOG.debug("query is {}", query); for (FieldInfo fieldInfo : fieldInfos) { // Get the designated column's data type. DataType type = rsMetaData.getType(fieldInfo.getColumnName()); columnDataTypes.add(type); Object setter; final String setterExpr = fieldInfo.getPojoFieldExpression(); switch (type.getName()) { case ASCII: case TEXT: case VARCHAR: setter = PojoUtils.createSetter(pojoClass, setterExpr, String.class); break; case BOOLEAN: setter = PojoUtils.createSetterBoolean(pojoClass, setterExpr); break; case INT: setter = PojoUtils.createSetterInt(pojoClass, setterExpr); break; case BIGINT: case COUNTER: setter = PojoUtils.createSetterLong(pojoClass, setterExpr); break; case FLOAT: setter = PojoUtils.createSetterFloat(pojoClass, setterExpr); break; case DOUBLE: setter = PojoUtils.createSetterDouble(pojoClass, setterExpr); break; case DECIMAL: setter = PojoUtils.createSetter(pojoClass, setterExpr, BigDecimal.class); break; case SET: setter = PojoUtils.createSetter(pojoClass, setterExpr, Set.class); break; case MAP: setter = PojoUtils.createSetter(pojoClass, setterExpr, Map.class); break; case LIST: setter = PojoUtils.createSetter(pojoClass, setterExpr, List.class); break; case TIMESTAMP: setter = PojoUtils.createSetter(pojoClass, setterExpr, Date.class); break; case UUID: setter = PojoUtils.createSetter(pojoClass, setterExpr, UUID.class); break; default: setter = PojoUtils.createSetter(pojoClass, setterExpr, Object.class); break; } setters.add(setter); } }
Example 8
Source File: DatastaxColumnKey.java From SimpleFlatMapper with MIT License | 4 votes |
public static DatastaxColumnKey of(ColumnDefinitions metaData, int column) { return new DatastaxColumnKey(metaData.getName(column), column , metaData.getType(column)); }
Example 9
Source File: QueryCassandra.java From nifi with Apache License 2.0 | 4 votes |
/** * Converts a result set into an Avro record and writes it to the given stream. * * @param rs The result set to convert * @param outStream The stream to which the Avro record will be written * @param timeout The max number of timeUnits to wait for a result set fetch to complete * @param timeUnit The unit of time (SECONDS, e.g.) associated with the timeout amount * @return The number of rows from the result set written to the stream * @throws IOException If the Avro record cannot be written * @throws InterruptedException If a result set fetch is interrupted * @throws TimeoutException If a result set fetch has taken longer than the specified timeout * @throws ExecutionException If any error occurs during the result set fetch */ public static long convertToAvroStream(final ResultSet rs, final OutputStream outStream, long timeout, TimeUnit timeUnit) throws IOException, InterruptedException, TimeoutException, ExecutionException { final Schema schema = createSchema(rs); final GenericRecord rec = new GenericData.Record(schema); final DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema); try (final DataFileWriter<GenericRecord> dataFileWriter = new DataFileWriter<>(datumWriter)) { dataFileWriter.create(schema, outStream); final ColumnDefinitions columnDefinitions = rs.getColumnDefinitions(); long nrOfRows = 0; if (columnDefinitions != null) { do { // Grab the ones we have int rowsAvailableWithoutFetching = rs.getAvailableWithoutFetching(); if (rowsAvailableWithoutFetching == 0) { // Get more if (timeout <= 0 || timeUnit == null) { rs.fetchMoreResults().get(); } else { rs.fetchMoreResults().get(timeout, timeUnit); } } for (Row row : rs) { for (int i = 0; i < columnDefinitions.size(); i++) { final DataType dataType = columnDefinitions.getType(i); if (row.isNull(i)) { rec.put(i, null); } else { rec.put(i, getCassandraObject(row, i, dataType)); } } dataFileWriter.append(rec); nrOfRows += 1; } } while (!rs.isFullyFetched()); } return nrOfRows; } }
Example 10
Source File: QueryCassandra.java From nifi with Apache License 2.0 | 4 votes |
/** * 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(); }