/** * Add a new column. * @param colSchema the schema of the new column * @return this instance */ public AlterTableOptions addColumn(ColumnSchema colSchema) { if (!colSchema.isNullable() && colSchema.getDefaultValue() == null) { throw new IllegalArgumentException("A new non-null column must have a default value"); } if (colSchema.isKey()) { throw new IllegalArgumentException("Key columns cannot be added"); } AlterTableRequestPB.Step.Builder step = pb.addAlterSchemaStepsBuilder(); step.setType(AlterTableRequestPB.StepType.ADD_COLUMN); step.setAddColumn(AlterTableRequestPB.AddColumn.newBuilder() .setSchema(ProtobufHelper.columnToPb(colSchema))); return this; }
public static Common.ColumnSchemaPB columnToPb(Common.ColumnSchemaPB.Builder schemaBuilder, ColumnSchema column) { schemaBuilder .setName(column.getName()) .setType(column.getType().getDataType(column.getTypeAttributes())) .setIsKey(column.isKey()) .setIsNullable(column.isNullable()) .setCfileBlockSize(column.getDesiredBlockSize()); if (column.getEncoding() != null) { schemaBuilder.setEncoding(column.getEncoding().getInternalPbType()); } if (column.getCompressionAlgorithm() != null) { schemaBuilder.setCompression(column.getCompressionAlgorithm().getInternalPbType()); } if (column.getDefaultValue() != null) { schemaBuilder.setReadDefaultValue(UnsafeByteOperations.unsafeWrap( objectToWireFormat(column, column.getDefaultValue()))); } if(column.getTypeAttributes() != null) { schemaBuilder.setTypeAttributes( columnTypeAttributesToPb(Common.ColumnTypeAttributesPB.newBuilder(), column)); } return schemaBuilder.build(); }
@Test public void testAlterModifyColumns() throws Exception { KuduTable table = createTable(ImmutableList.<Pair<Integer,Integer>>of()); insertRows(table, 0, 100); assertEquals(100, countRowsInTable(table)); // Check for expected defaults. ColumnSchema col = table.getSchema().getColumns().get(1); assertEquals(CompressionAlgorithm.DEFAULT_COMPRESSION, col.getCompressionAlgorithm()); assertEquals(Encoding.AUTO_ENCODING, col.getEncoding()); assertEquals(null, col.getDefaultValue()); // Alter the table. client.alterTable(tableName, new AlterTableOptions() .changeCompressionAlgorithm(col.getName(), CompressionAlgorithm.SNAPPY) .changeEncoding(col.getName(), Encoding.RLE) .changeDefault(col.getName(), 0)); // Check for new values. table = client.openTable(tableName); col = table.getSchema().getColumns().get(1); assertEquals(CompressionAlgorithm.SNAPPY, col.getCompressionAlgorithm()); assertEquals(Encoding.RLE, col.getEncoding()); assertEquals(0, col.getDefaultValue()); }
assertEquals(defaultInt, kuduTable.getSchema().getColumnByIndex(0).getDefaultValue()); assertEquals(defaultString, kuduTable.getSchema().getColumnByIndex(columns.size() - 2).getDefaultValue()); assertEquals(true, kuduTable.getSchema().getColumnByIndex(columns.size() - 1).getDefaultValue()); assertNull(kuduTableWithoutDefaults.getSchema().getColumnByIndex(0).getDefaultValue()); createTableWithSplitsAndTest(splitTablePrefix, 3); createTableWithSplitsAndTest(splitTablePrefix, 10);