private void checkSqlColumnsMatch(String sqlTable) { Set<String> expectedColumns = Sets.newHashSet(); for (ColumnSchema column : schema.getColumns()) { expectedColumns.add(column.getColumnName()); } List<String> invalidColumns = Lists.newLinkedList(); for (String column : columns) { if (!expectedColumns.contains(column)) { LOG.error("Found non-existent column " + column); invalidColumns.add(column); } } if (invalidColumns.size() > 0) { String expectedColumnString = Joiner.on(",").join(expectedColumns); String invalidColumnString = Joiner.on(",").join(invalidColumns); String message = String.format("In table %s following columns (%s)" + " are not valid columns. Expected columns (%s)", sqlTable, invalidColumnString, expectedColumnString); throw new IllegalStateException(message); } }
/** * Parse a line of a CSV into a Honeycomb {@link Row} * * @param line CSV line * @return Honeycomb {@link Row} * @throws IOException The CSV line was improperly formed * @throws ParseException The CSV line contained invalid data. */ public Row parseRow(String line) throws IOException, ParseException { checkNotNull(line); String[] unparsedFields = csvParser.parseLine(line); checkArgument((schema.getColumns().size() == unparsedFields.length), "Line contains wrong number of columns."); ImmutableMap.Builder<String, ByteBuffer> fields = ImmutableMap.builder(); for (int i = 0; i < columns.length; i++) { fields.put(columns[i], FieldParser.parse(unparsedFields[i], schema.getColumnSchema(columns[i]))); } return new Row(fields.build(), UUID.randomUUID()); } }
/** * Performs all metadata operations necessary to create a table * * @param tableName The name of the table to create, not null or empty * @param schema The schema details of the table to create, not null */ public void createTable(final String tableName, final TableSchema schema) { Verify.isNotNullOrEmpty(tableName); checkNotNull(schema); final List<Put> puts = Lists.newArrayList(); // Fetch the next table id to use for this table final long tableId = getNextTableId(); puts.add(putTableId(tableName, tableId)); puts.add(putColumnIds(tableId, schema.getColumns())); puts.add(putTableSchema(tableId, schema)); if (schema.hasIndices()) { puts.add(putIndices(tableId, schema.getIndices())); } performMutations(ImmutableList.<Delete>of(), puts); }
public RowGenerator(TableSchema schema) { super(); ImmutableMap.Builder<String, Generator<ByteBuffer>> recordGenerators = ImmutableMap.builder(); for (ColumnSchema column : schema.getColumns()) { recordGenerators.put(column.getColumnName(), new FieldGenerator(column)); } this.recordGenerators = recordGenerators.build(); }
@Test public void testDeserializeValidSerializedSchema() { final TableSchema actualSchema = TableSchema.deserialize(tableSchema.serialize()); assertEquals(COLUMNS.size(), tableSchema.getColumns().size()); assertEquals(INDICES.size(), tableSchema.getIndices().size()); assertEquals(tableSchema, actualSchema); }