@Override public void endRecord() throws IOException { try { session.apply(insert); recordsSinceFlush++; if (recordsSinceFlush == FLUSH_FREQUENCY) { flush(); recordsSinceFlush = 0; } insert = null; } catch (Exception e) { throw new IOException(e); } }
private void apply(Operation op) { try { OperationResponse response = session.apply(op); if (response != null && response.hasRowError()) { LOG.info("Write operation failed: {}", response.getRowError()); } } catch (KuduException ex) { LOG.warn("Write operation failed", ex); } } }
@Override public CompletableFuture<?> appendPage(Page page) { for (int position = 0; position < page.getPositionCount(); position++) { Upsert upsert = table.newUpsert(); PartialRow row = upsert.getRow(); int start = 0; if (generateUUID) { String id = String.format("%s-%08x", uuid, nextSubId++); row.addString(0, id); start = 1; } for (int channel = 0; channel < page.getChannelCount(); channel++) { appendColumn(row, page, position, channel, channel + start); } try { session.apply(upsert); } catch (KuduException e) { throw new RuntimeException(e); } } return NOT_BLOCKED; }
@Override public void createSchema(KuduClient client, String schemaName) { if (DEFAULT_SCHEMA.equals(schemaName)) { throw new SchemaAlreadyExistsException(schemaName); } else { try { KuduTable schemasTable = getSchemasTable(client); KuduSession session = client.newSession(); try { Upsert upsert = schemasTable.newUpsert(); upsert.getRow().addString(0, schemaName); session.apply(upsert); } finally { session.close(); } } catch (KuduException e) { throw new PrestoException(GENERIC_INTERNAL_ERROR, e); } } }
private void createAndFillSchemasTable(KuduClient client) throws KuduException { List<String> existingSchemaNames = listSchemaNamesFromTablets(client); ColumnSchema schemaColumnSchema = new ColumnSchema.ColumnSchemaBuilder("schema", Type.STRING) .key(true).build(); Schema schema = new Schema(ImmutableList.of(schemaColumnSchema)); CreateTableOptions options = new CreateTableOptions(); options.addHashPartitions(ImmutableList.of(schemaColumnSchema.getName()), 2); KuduTable schemasTable = client.createTable(rawSchemasTableName, schema, options); KuduSession session = client.newSession(); try { session.setFlushMode(SessionConfiguration.FlushMode.AUTO_FLUSH_BACKGROUND); for (String schemaName : existingSchemaNames) { Insert insert = schemasTable.newInsert(); insert.getRow().addString(0, schemaName); session.apply(insert); } } finally { session.close(); } }
@Override public void deleteRows(Block rowIds) { Schema schema = table.getSchema(); KuduSession session = clientSession.newSession(); session.setFlushMode(FlushMode.AUTO_FLUSH_BACKGROUND); try { try { for (int i = 0; i < rowIds.getPositionCount(); i++) { int len = rowIds.getSliceLength(i); Slice slice = rowIds.getSlice(i, 0, len); PartialRow row = KeyEncoderAccessor.decodePrimaryKey(schema, slice.getBytes()); Delete delete = table.newDelete(); RowHelper.copyPrimaryKey(schema, row, delete.getRow()); session.apply(delete); } } finally { session.close(); } } catch (KuduException e) { throw new RuntimeException(e); } }
@Override public void dropSchema(KuduClient client, String schemaName) { if (DEFAULT_SCHEMA.equals(schemaName)) { throw new PrestoException(GENERIC_USER_ERROR, "Deleting default schema not allowed."); } else { try { String prefix = getPrefixForTablesOfSchema(schemaName); for (String name : client.getTablesList(prefix).getTablesList()) { client.deleteTable(name); } KuduTable schemasTable = getSchemasTable(client); KuduSession session = client.newSession(); try { Delete delete = schemasTable.newDelete(); delete.getRow().addString(0, schemaName); session.apply(delete); } finally { session.close(); } } catch (KuduException e) { throw new PrestoException(GENERIC_INTERNAL_ERROR, e); } } }
OperationResponse response = kuduSession.apply(operation); if (response != null && response.hasRowError()) {
row.addFloat(3, i + 0.01f); row.addString(4, ("Row " + i)); session.apply(insert);
try session.apply(operation);
session.apply(delete);
private void updateRow(long keyOne, long keyTwo, int newCount) throws IOException { Update update = table.newUpdate(); PartialRow row = update.getRow(); row.addLong(COLUMN_KEY_ONE, keyOne); row.addLong(COLUMN_KEY_TWO, keyTwo); row.addInt(COLUMN_UPDATE_COUNT, newCount); session.apply(update); }
private void insertDefaultRow(KuduTable table, KuduSession session, int key) throws Exception { Insert insert = table.newInsert(); PartialRow row = insert.getRow(); row.addInt("key", key); // Omit value. session.apply(insert); }
/** * Load a table of default schema with the specified number of records, in ascending key order. */ public static void loadDefaultTable(KuduClient client, String tableName, int numRows) throws KuduException { KuduTable table = client.openTable(tableName); KuduSession session = client.newSession(); for (int i = 0; i < numRows; i++) { Insert insert = createBasicSchemaInsert(table, i); session.apply(insert); } session.flush(); session.close(); }
private void insertRows(KuduTable table, Set<Row> rows) throws KuduException { KuduSession session = client.newSession(); try { for (Row row : rows) { Insert insert = table.newInsert(); PartialRow insertRow = insert.getRow(); row.fillPartialRow(insertRow); session.apply(insert); } } finally { session.close(); } }
@Test(timeout = 10000) public void testUpsert() throws Exception { KuduTable table = client.createTable(tableName, basicSchema, getBasicCreateTableOptions()); KuduSession session = client.newSession(); // Test an Upsert that acts as an Insert. assertFalse(session.apply(createUpsert(table, 1, 1, false)).hasRowError()); List<String> rowStrings = scanTableToStrings(table); assertEquals(1, rowStrings.size()); assertEquals( "INT32 key=1, INT32 column1_i=1, INT32 column2_i=3, " + "STRING column3_s=a string, BOOL column4_b=true", rowStrings.get(0)); // Test an Upsert that acts as an Update. assertFalse(session.apply(createUpsert(table, 1, 2, false)).hasRowError()); rowStrings = scanTableToStrings(table); assertEquals( "INT32 key=1, INT32 column1_i=2, INT32 column2_i=3, " + "STRING column3_s=a string, BOOL column4_b=true", rowStrings.get(0)); }
@Override public void run() { try { KuduTable table = localClient.openTable(TABLE_NAME); KuduSession session = localClient.newSession(); session.setFlushMode(SessionConfiguration.FlushMode.AUTO_FLUSH_SYNC); for (int i = 0; i < 100; i++) { Insert insert = createBasicSchemaInsert(table, id * 100 + i); session.apply(insert); } session.close(); } catch (Exception e) { fail("insert thread should not throw exception: " + e); } } });
public boolean writeRow(KuduRow row, Consistency consistency, WriteMode writeMode) throws Exception { final Operation operation = KuduMapper.toOperation(table, writeMode, row); if (Consistency.EVENTUAL.equals(consistency)) { AsyncKuduSession session = client.newSession(); session.apply(operation); session.flush(); return session.close().addCallback(new ResponseCallback()).join(); } else { KuduSession session = client.syncClient().newSession(); session.apply(operation); session.flush(); return processResponse(session.close()); } }
@Test(timeout = 100000) public void testBasicOps() throws Exception { KuduTable table = client.createTable(tableName, basicSchema, getBasicCreateTableOptions()); KuduSession session = client.newSession(); for (int i = 0; i < 10; i++) { session.apply(createInsert(table, i)); } assertEquals(10, countRowsInScan(client.newScannerBuilder(table).build())); OperationResponse resp = session.apply(createInsert(table, 0)); assertTrue(resp.hasRowError()); session.setFlushMode(SessionConfiguration.FlushMode.MANUAL_FLUSH); for (int i = 10; i < 20; i++) { session.apply(createInsert(table, i)); } session.flush(); assertEquals(20, countRowsInScan(client.newScannerBuilder(table).build())); }
@Test(timeout = 10000) public void testInsertAutoFlushSyncNonCoveredRange() throws Exception { CreateTableOptions createOptions = getBasicTableOptionsWithNonCoveredRange(); createOptions.setNumReplicas(1); client.createTable(tableName, basicSchema, createOptions); KuduTable table = client.openTable(tableName); KuduSession session = client.newSession(); session.setFlushMode(SessionConfiguration.FlushMode.AUTO_FLUSH_SYNC); List<Integer> nonCoveredKeys = ImmutableList.of(350, 300, 199, 150, 100, -1, -50); for (int key : nonCoveredKeys) { OperationResponse response = session.apply(createBasicSchemaInsert(table, key)); assertTrue(response.hasRowError()); assertTrue(response.getRowError().getErrorStatus().isNotFound()); } }