public static ColumnDefinition fromThrift(String ksName, String cfName, AbstractType<?> thriftComparator, AbstractType<?> thriftSubcomparator, ColumnDef thriftColumnDef) throws SyntaxException, ConfigurationException { // For super columns, the componentIndex is 1 because the ColumnDefinition applies to the column component. Integer componentIndex = thriftSubcomparator != null ? 1 : null; AbstractType<?> comparator = thriftSubcomparator == null ? thriftComparator : thriftSubcomparator; try { comparator.validate(thriftColumnDef.name); } catch (MarshalException e) { throw new ConfigurationException(String.format("Column name %s is not valid for comparator %s", ByteBufferUtil.bytesToHex(thriftColumnDef.name), comparator)); } return new ColumnDefinition(ksName, cfName, new ColumnIdentifier(ByteBufferUtil.clone(thriftColumnDef.name), comparator), TypeParser.parse(thriftColumnDef.validation_class), thriftColumnDef.index_type == null ? null : IndexType.valueOf(thriftColumnDef.index_type.name()), thriftColumnDef.index_options, thriftColumnDef.index_name, componentIndex, Kind.REGULAR); }
/** * getting a column * * @param client * @param keyspace * @param columnFamily * @return */ protected static ColumnDef getColumn(Cassandra.Client client, String keyspace, String columnFamily, String column) { ColumnDef result = null; // try { CfDef cd = getColumnFamily(client, keyspace, columnFamily); for (ColumnDef entry : cd.getColumn_metadata()) { if (new String(entry.getName()).equals(column)) { result = entry; break; } } } catch (Exception ex) { ex.printStackTrace(); } return result; }
/** * modifying a column family to an existing keyspace * * @throws Exception */ @Test public void modifyColumn() throws Exception { String KEYSPACE = "mock"; client.set_keyspace(KEYSPACE); // String COLUMN_FAMILY = "student"; CfDef cd = getColumnFamily(client, KEYSPACE, COLUMN_FAMILY); System.out.println(cd.getName()); // String COLUMN = "grad"; ColumnDef columnDef = getColumn(client, KEYSPACE, COLUMN_FAMILY, COLUMN); System.out.println(new String(columnDef.getName())); // TODO System.out.println("modify column [" + COLUMN + "]"); }
@Test // test for CASSANDRA-8178 public void testNonTextComparator() throws Throwable { ColumnDef column = new ColumnDef(); column.setName(bytes(42)) .setValidation_class(UTF8Type.instance.toString()); CfDef cf = new CfDef("thriftcompat", "JdbcInteger"); cf.setColumn_type("Standard") .setComparator_type(Int32Type.instance.toString()) .setDefault_validation_class(UTF8Type.instance.toString()) .setKey_validation_class(BytesType.instance.toString()) .setColumn_metadata(Collections.singletonList(column)); SchemaLoader.createKeyspace("thriftcompat", KeyspaceParams.simple(1), ThriftConversion.fromThrift(cf)); // the comparator is IntegerType, and there is a column named 42 with a UTF8Type validation type execute("INSERT INTO \"thriftcompat\".\"JdbcInteger\" (key, \"42\") VALUES (0x00000001, 'abc')"); execute("UPDATE \"thriftcompat\".\"JdbcInteger\" SET \"42\" = 'abc' WHERE key = 0x00000001"); execute("DELETE \"42\" FROM \"thriftcompat\".\"JdbcInteger\" WHERE key = 0x00000000"); UntypedResultSet results = execute("SELECT key, \"42\" FROM \"thriftcompat\".\"JdbcInteger\""); assertEquals(1, results.size()); UntypedResultSet.Row row = results.iterator().next(); assertEquals(ByteBufferUtil.bytes(1), row.getBytes("key")); assertEquals("abc", row.getString("42")); }
public static ColumnDefinition fromThrift(CFMetaData cfm, ColumnDef thriftColumnDef) throws SyntaxException, ConfigurationException { // For super columns, the componentIndex is 1 because the ColumnDefinition applies to the column component. Integer componentIndex = cfm.isSuper() ? 1 : null; AbstractType<?> comparator = cfm.getComponentComparator(componentIndex, Kind.REGULAR); try { comparator.validate(thriftColumnDef.name); } catch (MarshalException e) { throw new ConfigurationException(String.format("Column name %s is not valid for comparator %s", ByteBufferUtil.bytesToHex(thriftColumnDef.name), comparator)); } ColumnDefinition cd = new ColumnDefinition(cfm, ByteBufferUtil.clone(thriftColumnDef.name), TypeParser.parse(thriftColumnDef.validation_class), componentIndex, Kind.REGULAR); cd.setIndex(thriftColumnDef.index_name, thriftColumnDef.index_type == null ? null : IndexType.valueOf(thriftColumnDef.index_type.name()), thriftColumnDef.index_options); return cd; }
public static List<ColumnDefinition> getIndexMetadata( String indexes ) { if ( indexes == null ) { return null; } String[] index_entries = split( indexes, ',' ); List<ColumnDef> columns = new ArrayList<ColumnDef>(); for ( String index_entry : index_entries ) { String column_name = stringOrSubstringBeforeFirst( index_entry, ':' ).trim(); String comparer = substringAfterLast( index_entry, ":" ).trim(); if ( StringUtils.isBlank( comparer ) ) { comparer = "UUIDType"; } if ( StringUtils.isNotBlank( column_name ) ) { ColumnDef cd = new ColumnDef( bytebuffer( column_name ), comparer ); cd.setIndex_name( column_name ); cd.setIndex_type( IndexType.KEYS ); columns.add( cd ); } } return ThriftColumnDef.fromThriftList( columns ); }
public static List<ColumnDef> toThrift(Map<ByteBuffer, ColumnDefinition> columns) { List<ColumnDef> thriftDefs = new ArrayList<>(columns.size()); for (ColumnDefinition def : columns.values()) if (def.kind == ColumnDefinition.Kind.REGULAR) thriftDefs.add(def.toThrift()); return thriftDefs; }
public ColumnDef toThrift() { ColumnDef cd = new ColumnDef(); cd.setName(ByteBufferUtil.clone(name.bytes)); cd.setValidation_class(type.toString()); cd.setIndex_type(indexType == null ? null : org.apache.cassandra.thrift.IndexType.valueOf(indexType.name())); cd.setIndex_name(indexName == null ? null : indexName); cd.setIndex_options(indexOptions == null ? null : Maps.newHashMap(indexOptions)); return cd; }
public static List<ColumnDefinition> fromThrift(String ksName, String cfName, AbstractType<?> thriftComparator, AbstractType<?> thriftSubcomparator, List<ColumnDef> thriftDefs) throws SyntaxException, ConfigurationException { if (thriftDefs == null) return new ArrayList<>(); List<ColumnDefinition> defs = new ArrayList<>(thriftDefs.size()); for (ColumnDef thriftColumnDef : thriftDefs) defs.add(fromThrift(ksName, cfName, thriftComparator, thriftSubcomparator, thriftColumnDef)); return defs; }
@Test public void testThriftConversion() throws Exception { CfDef cfDef = new CfDef().setDefault_validation_class(AsciiType.class.getCanonicalName()) .setComment("Test comment") .setColumn_metadata(columnDefs) .setKeyspace(KEYSPACE) .setName(COLUMN_FAMILY); // convert Thrift to CFMetaData CFMetaData cfMetaData = CFMetaData.fromThrift(cfDef); CfDef thriftCfDef = new CfDef(); thriftCfDef.keyspace = KEYSPACE; thriftCfDef.name = COLUMN_FAMILY; thriftCfDef.default_validation_class = cfDef.default_validation_class; thriftCfDef.comment = cfDef.comment; thriftCfDef.column_metadata = new ArrayList<ColumnDef>(); for (ColumnDef columnDef : columnDefs) { ColumnDef c = new ColumnDef(); c.name = ByteBufferUtil.clone(columnDef.name); c.validation_class = columnDef.getValidation_class(); c.index_name = columnDef.getIndex_name(); c.index_type = IndexType.KEYS; thriftCfDef.column_metadata.add(c); } CfDef converted = cfMetaData.toThrift(); assertEquals(thriftCfDef.keyspace, converted.keyspace); assertEquals(thriftCfDef.name, converted.name); assertEquals(thriftCfDef.default_validation_class, converted.default_validation_class); assertEquals(thriftCfDef.comment, converted.comment); assertEquals(new HashSet<>(thriftCfDef.column_metadata), new HashSet<>(converted.column_metadata)); }
public static List<ColumnDef> toThrift(Map<ByteBuffer, ColumnDefinition> columns) { List<ColumnDef> thriftDefs = new ArrayList<>(columns.size()); for (ColumnDefinition def : columns.values()) if (def.type == ColumnDefinition.Type.REGULAR) thriftDefs.add(def.toThrift()); return thriftDefs; }
public ColumnDef toThrift() { ColumnDef cd = new ColumnDef(); cd.setName(ByteBufferUtil.clone(name)); cd.setValidation_class(validator.toString()); cd.setIndex_type(indexType == null ? null : org.apache.cassandra.thrift.IndexType.valueOf(indexType.name())); cd.setIndex_name(indexName == null ? null : indexName); cd.setIndex_options(indexOptions == null ? null : Maps.newHashMap(indexOptions)); return cd; }
public static ColumnDefinition fromThrift(ColumnDef thriftColumnDef, boolean isSuper) throws SyntaxException, ConfigurationException { // For super columns, the componentIndex is 1 because the ColumnDefinition applies to the column component. return new ColumnDefinition(ByteBufferUtil.clone(thriftColumnDef.name), TypeParser.parse(thriftColumnDef.validation_class), thriftColumnDef.index_type == null ? null : IndexType.valueOf(thriftColumnDef.index_type.name()), thriftColumnDef.index_options, thriftColumnDef.index_name, isSuper ? 1 : null, Type.REGULAR); }
public static Map<ByteBuffer, ColumnDefinition> fromThrift(List<ColumnDef> thriftDefs, boolean isSuper) throws SyntaxException, ConfigurationException { if (thriftDefs == null) return new HashMap<>(); Map<ByteBuffer, ColumnDefinition> cds = new TreeMap<>(); for (ColumnDef thriftColumnDef : thriftDefs) cds.put(ByteBufferUtil.clone(thriftColumnDef.name), fromThrift(thriftColumnDef, isSuper)); return cds; }
@Test public void testThriftConversion() throws Exception { CfDef cfDef = new CfDef().setDefault_validation_class(AsciiType.class.getCanonicalName()) .setComment("Test comment") .setColumn_metadata(columnDefs) .setKeyspace(KEYSPACE) .setName(COLUMN_FAMILY); // convert Thrift to CFMetaData CFMetaData cfMetaData = CFMetaData.fromThrift(cfDef); CfDef thriftCfDef = new CfDef(); thriftCfDef.keyspace = KEYSPACE; thriftCfDef.name = COLUMN_FAMILY; thriftCfDef.default_validation_class = cfDef.default_validation_class; thriftCfDef.comment = cfDef.comment; thriftCfDef.column_metadata = new ArrayList<ColumnDef>(); for (ColumnDef columnDef : columnDefs) { ColumnDef c = new ColumnDef(); c.name = ByteBufferUtil.clone(columnDef.name); c.validation_class = columnDef.getValidation_class(); c.index_name = columnDef.getIndex_name(); c.index_type = IndexType.KEYS; thriftCfDef.column_metadata.add(c); } CfDef converted = cfMetaData.toThrift(); assertEquals(thriftCfDef.keyspace, converted.keyspace); assertEquals(thriftCfDef.name, converted.name); assertEquals(thriftCfDef.default_validation_class, converted.default_validation_class); assertEquals(thriftCfDef.comment, converted.comment); assertEquals(thriftCfDef.column_metadata, converted.column_metadata); }
/** * list all column familys * * @throws Exception */ @Test public void listColumnFamilys() throws Exception { long beg = System.currentTimeMillis(); // String KEYSPACE = "UIH"; client.set_keyspace(KEYSPACE); // KsDef kd = client.describe_keyspace(KEYSPACE); List<CfDef> result = kd.getCf_defs(); for (CfDef cd : result) { StringBuilder buffer = new StringBuilder(); buffer.append("["); buffer.append(cd.getName()); buffer.append("] "); // List<ColumnDef> cols = cd.getColumn_metadata(); int size = cols.size(); int i = 0; for (ColumnDef column : cols) { buffer.append(ByteHelper.toString(column.getName())); if (i < size - 1) { buffer.append(", "); } i++; } // System.out.println(buffer); } long end = System.currentTimeMillis(); // System.out.println((end - beg) + " at mills."); }
/** * getting a column * * @throws Exception */ @Test public void getColumn() throws Exception { String KEYSPACE = "mock"; String COLUMN_FAMILY = "student"; String COLUMN = "grad"; ColumnDef result = getColumn(client, KEYSPACE, COLUMN_FAMILY, COLUMN); System.out.println(result); }
@Test // test for CASSANDRA-9867 public void testDropCompactStaticColumn() { ColumnDef column1 = new ColumnDef(); column1.setName(bytes(42)) .setValidation_class(UTF8Type.instance.toString()); ColumnDef column2 = new ColumnDef(); column2.setName(bytes(25)) .setValidation_class(UTF8Type.instance.toString()); CfDef cf = new CfDef("thriftks", "staticcompact"); cf.setColumn_type("Standard") .setComparator_type(Int32Type.instance.toString()) .setDefault_validation_class(UTF8Type.instance.toString()) .setKey_validation_class(BytesType.instance.toString()) .setColumn_metadata(Arrays.asList(column1, column2)); SchemaLoader.createKeyspace("thriftks", KeyspaceParams.simple(1), ThriftConversion.fromThrift(cf)); CFMetaData cfm = Schema.instance.getCFMetaData("thriftks", "staticcompact"); // assert the both columns are in the metadata assertTrue(cfm.getColumnMetadata().containsKey(bytes(42))); assertTrue(cfm.getColumnMetadata().containsKey(bytes(25))); // remove column2 cf.setColumn_metadata(Collections.singletonList(column1)); MigrationManager.announceColumnFamilyUpdate(ThriftConversion.fromThriftForUpdate(cf, cfm), true); // assert that it's gone from metadata assertTrue(cfm.getColumnMetadata().containsKey(bytes(42))); assertFalse(cfm.getColumnMetadata().containsKey(bytes(25))); }
@Test public void testThriftConversion() throws Exception { CfDef cfDef = new CfDef().setDefault_validation_class(AsciiType.class.getCanonicalName()) .setComment("Test comment") .setColumn_metadata(columnDefs) .setKeyspace(KEYSPACE1) .setName(CF_STANDARD1); // convert Thrift to CFMetaData CFMetaData cfMetaData = ThriftConversion.fromThrift(cfDef); CfDef thriftCfDef = new CfDef(); thriftCfDef.keyspace = KEYSPACE1; thriftCfDef.name = CF_STANDARD1; thriftCfDef.default_validation_class = cfDef.default_validation_class; thriftCfDef.comment = cfDef.comment; thriftCfDef.column_metadata = new ArrayList<>(); for (ColumnDef columnDef : columnDefs) { ColumnDef c = new ColumnDef(); c.name = ByteBufferUtil.clone(columnDef.name); c.validation_class = columnDef.getValidation_class(); c.index_name = columnDef.getIndex_name(); c.index_type = columnDef.getIndex_type(); if (columnDef.isSetIndex_options()) c.setIndex_options(columnDef.getIndex_options()); thriftCfDef.column_metadata.add(c); } CfDef converted = ThriftConversion.toThrift(cfMetaData); assertEquals(thriftCfDef.keyspace, converted.keyspace); assertEquals(thriftCfDef.name, converted.name); assertEquals(thriftCfDef.default_validation_class, converted.default_validation_class); assertEquals(thriftCfDef.comment, converted.comment); assertEquals(new HashSet<>(thriftCfDef.column_metadata), new HashSet<>(converted.column_metadata)); }
@Test public void testThriftConversion() throws Exception { CfDef cfDef = new CfDef().setDefault_validation_class(AsciiType.class.getCanonicalName()) .setComment("Test comment") .setColumn_metadata(columnDefs) .setKeyspace(KEYSPACE1) .setName(CF_STANDARD1); // convert Thrift to CFMetaData CFMetaData cfMetaData = ThriftConversion.fromThrift(cfDef); CfDef thriftCfDef = new CfDef(); thriftCfDef.keyspace = KEYSPACE1; thriftCfDef.name = CF_STANDARD1; thriftCfDef.default_validation_class = cfDef.default_validation_class; thriftCfDef.comment = cfDef.comment; thriftCfDef.column_metadata = new ArrayList<>(); for (ColumnDef columnDef : columnDefs) { ColumnDef c = new ColumnDef(); c.name = ByteBufferUtil.clone(columnDef.name); c.validation_class = columnDef.getValidation_class(); c.index_name = columnDef.getIndex_name(); c.index_type = IndexType.KEYS; thriftCfDef.column_metadata.add(c); } CfDef converted = ThriftConversion.toThrift(cfMetaData); assertEquals(thriftCfDef.keyspace, converted.keyspace); assertEquals(thriftCfDef.name, converted.name); assertEquals(thriftCfDef.default_validation_class, converted.default_validation_class); assertEquals(thriftCfDef.comment, converted.comment); assertEquals(new HashSet<>(thriftCfDef.column_metadata), new HashSet<>(converted.column_metadata)); }
/** get next row */ public Tuple getNext() throws IOException { try { // load the next pair if (!reader.nextKeyValue()) return null; CfInfo cfInfo = getCfInfo(loadSignature); CfDef cfDef = cfInfo.cfDef; Row row = reader.getCurrentValue(); Tuple tuple = TupleFactory.getInstance().newTuple(cfDef.column_metadata.size()); Iterator<ColumnDef> itera = cfDef.column_metadata.iterator(); int i = 0; while (itera.hasNext()) { ColumnDef cdef = itera.next(); ByteBuffer columnValue = row.getBytesUnsafe(ByteBufferUtil.string(cdef.name.duplicate())); if (columnValue != null) { Cell cell = new BufferCell(CellNames.simpleDense(cdef.name), columnValue); AbstractType<?> validator = getValidatorMap(cfDef).get(cdef.name); setTupleValue(tuple, i, cqlColumnToObj(cell, cfDef), validator); } else tuple.set(i, null); i++; } return tuple; } catch (InterruptedException e) { throw new IOException(e.getMessage()); } }
public static List<ColumnDefinition> fromThrift(String ksName, String cfName, AbstractType<?> thriftComparator, AbstractType<?> thriftSubcomparator, List<ColumnDef> thriftDefs) throws SyntaxException, ConfigurationException { if (thriftDefs == null) return Collections.emptyList(); List<ColumnDefinition> defs = new ArrayList<>(thriftDefs.size()); for (ColumnDef thriftColumnDef : thriftDefs) defs.add(fromThrift(ksName, cfName, thriftComparator, thriftSubcomparator, thriftColumnDef)); return defs; }
@Test public void testThriftConversion() throws Exception { CfDef cfDef = new CfDef().setDefault_validation_class(AsciiType.class.getCanonicalName()) .setComment("Test comment") .setColumn_metadata(columnDefs) .setKeyspace(KEYSPACE1) .setName(CF_STANDARD1); // convert Thrift to CFMetaData CFMetaData cfMetaData = CFMetaData.fromThrift(cfDef); CfDef thriftCfDef = new CfDef(); thriftCfDef.keyspace = KEYSPACE1; thriftCfDef.name = CF_STANDARD1; thriftCfDef.default_validation_class = cfDef.default_validation_class; thriftCfDef.comment = cfDef.comment; thriftCfDef.column_metadata = new ArrayList<ColumnDef>(); for (ColumnDef columnDef : columnDefs) { ColumnDef c = new ColumnDef(); c.name = ByteBufferUtil.clone(columnDef.name); c.validation_class = columnDef.getValidation_class(); c.index_name = columnDef.getIndex_name(); c.index_type = IndexType.KEYS; thriftCfDef.column_metadata.add(c); } CfDef converted = cfMetaData.toThrift(); assertEquals(thriftCfDef.keyspace, converted.keyspace); assertEquals(thriftCfDef.name, converted.name); assertEquals(thriftCfDef.default_validation_class, converted.default_validation_class); assertEquals(thriftCfDef.comment, converted.comment); assertEquals(new HashSet<>(thriftCfDef.column_metadata), new HashSet<>(converted.column_metadata)); }
public ColumnDef toThrift() { ColumnDef cd = new ColumnDef(); cd.setName(ByteBufferUtil.clone(name)); cd.setValidation_class(validator.toString()); cd.setIndex_type(index_type == null ? null : IndexType.valueOf(index_type.name())); cd.setIndex_name(index_name == null ? null : index_name); cd.setIndex_options(index_options == null ? null : Maps.newHashMap(index_options)); return cd; }
public static ColumnDefinition fromThrift(ColumnDef thriftColumnDef) throws SyntaxException, ConfigurationException { return new ColumnDefinition(ByteBufferUtil.clone(thriftColumnDef.name), TypeParser.parse(thriftColumnDef.validation_class), thriftColumnDef.index_type, thriftColumnDef.index_options, thriftColumnDef.index_name, null); }
public static Map<ByteBuffer, ColumnDefinition> fromThrift(List<ColumnDef> thriftDefs) throws SyntaxException, ConfigurationException { if (thriftDefs == null) return new HashMap<ByteBuffer,ColumnDefinition>(); Map<ByteBuffer, ColumnDefinition> cds = new TreeMap<ByteBuffer, ColumnDefinition>(); for (ColumnDef thriftColumnDef : thriftDefs) cds.put(ByteBufferUtil.clone(thriftColumnDef.name), fromThrift(thriftColumnDef)); return cds; }
@Test public void testThriftToAvroConversion() throws Exception { CfDef cfDef = new CfDef().setDefault_validation_class(AsciiType.class.getCanonicalName()) .setComment("Test comment") .setColumn_metadata(columnDefs) .setKeyspace(KEYSPACE) .setName(COLUMN_FAMILY); // convert Thrift to CFMetaData CFMetaData cfMetaData = CFMetaData.fromThrift(cfDef); // make a correct Avro object CfDef thriftCfDef = new CfDef(); thriftCfDef.keyspace = KEYSPACE; thriftCfDef.name = COLUMN_FAMILY; thriftCfDef.default_validation_class = cfDef.default_validation_class; thriftCfDef.comment = cfDef.comment; thriftCfDef.column_metadata = new ArrayList<ColumnDef>(); for (ColumnDef columnDef : columnDefs) { ColumnDef c = new ColumnDef(); c.name = ByteBufferUtil.clone(columnDef.name); c.validation_class = columnDef.getValidation_class(); c.index_name = columnDef.getIndex_name(); c.index_type = IndexType.KEYS; thriftCfDef.column_metadata.add(c); } CfDef converted = cfMetaData.toThrift(); assertEquals(thriftCfDef.keyspace, converted.keyspace); assertEquals(thriftCfDef.name, converted.name); assertEquals(thriftCfDef.default_validation_class, converted.default_validation_class); assertEquals(thriftCfDef.comment, converted.comment); assertEquals(thriftCfDef.column_metadata, converted.column_metadata); }
public static List<ColumnDefinition> fromThrift(CFMetaData cfm, List<ColumnDef> thriftDefs) throws SyntaxException, ConfigurationException { if (thriftDefs == null) return Collections.emptyList(); List<ColumnDefinition> defs = new ArrayList<>(thriftDefs.size()); for (ColumnDef thriftColumnDef : thriftDefs) defs.add(fromThrift(cfm, thriftColumnDef)); return defs; }