/** * 单纯插入 * * @param tablename * @param mid * @param fieldsValue */ private void insert(String tablename, String mid, String[] fieldsValue) { checkSession(); KuduTable table; if (Constants.UPUSERS_ATTR_TABLE.equals(tablename)) { table = table_attr; } else { table = table_days; } try { Insert insert = table.newInsert(); PartialRow row = insert.getRow(); setInsertValue(mid, Constants.DAYS_FIELDS, fieldsValue, row); OperationResponse rsInsert = session.apply(insert); if (rsInsert.hasRowError()) { System.out.println( "=======================================INSERT DATA:" + mid + "---------------" + Arrays.toString(fieldsValue) + rsInsert.getRowError().getMessage()); } } catch (Exception e) { collector.reportError(e); } }
protected static KuduTable createFourTabletsTableWithNineRows(String tableName) throws Exception { CreateTableBuilder builder = new CreateTableBuilder(); PartialRow splitRow = basicSchema.newPartialRow(); for (int i : KEYS) { splitRow.addInt(0, i); builder.addSplitRow(splitRow); } KuduTable table = createTable(tableName, basicSchema, builder); AsyncKuduSession session = client.newSession(); // create a table with on empty tablet and 3 tablets of 3 rows each for (int key1 : KEYS) { for (int key2 = 1; key2 <= 3; key2++) { Insert insert = table.newInsert(); PartialRow row = insert.getRow(); row.addInt(0, key1 + key2); row.addInt(1, key1); row.addInt(2, key2); row.addString(3, "a string"); row.addBoolean(4, true); session.apply(insert).join(DEFAULT_SLEEP); } } session.close().join(DEFAULT_SLEEP); return table; }
@Test public void test() throws Exception { createTable(TABLE_NAME, getBasicSchema(), getBasicCreateTableOptions()); KuduTableOutputFormat output = new KuduTableOutputFormat(); Configuration conf = new Configuration(); conf.set(KuduTableOutputFormat.MASTER_ADDRESSES_KEY, getMasterAddresses()); conf.set(KuduTableOutputFormat.OUTPUT_TABLE_KEY, TABLE_NAME); output.setConf(conf); String multitonKey = conf.get(KuduTableOutputFormat.MULTITON_KEY); KuduTable table = KuduTableOutputFormat.getKuduTable(multitonKey); assertNotNull(table); Insert insert = table.newInsert(); PartialRow row = insert.getRow(); row.addInt(0, 1); row.addInt(1, 2); row.addInt(2, 3); row.addString(3, "a string"); row.addBoolean(4, true); RecordWriter<NullWritable, Operation> rw = output.getRecordWriter(null); rw.write(NullWritable.get(), insert); rw.close(null); AsyncKuduScanner.AsyncKuduScannerBuilder builder = client.newScannerBuilder(table); assertEquals(1, countRowsInScan(builder.build())); }
protected Insert createBasicSchemaInsert(KuduTable table, int key) { Insert insert = table.newInsert(); PartialRow row = insert.getRow(); row.addInt(0, key); row.addInt(1, 2); row.addInt(2, 3); row.addString(3, "a string"); row.addBoolean(4, true); return insert; }
/** * 执行插入更新操作 * * @param tablename * @param mid * @param fieldsValue */ private void insertUpdate(String tablename, String mid, String[] fieldsValue) { checkSession(); KuduTable table; if (Constants.UPUSERS_ATTR_TABLE.equals(tablename)) { table = table_attr; } else { table = table_days; } try { Insert insert = table.newInsert(); PartialRow row = insert.getRow(); setInsertValue(mid, Constants.ATTR_INSERT_FIELDS, fieldsValue, row); OperationResponse rsInsert = session.apply(insert); if (rsInsert.hasRowError()) { if ("key already present".equals(rsInsert.getRowError().getMessage())) { Update update = table.newUpdate(); PartialRow urow = update.getRow(); setUpdateValue(mid, Constants.ATTR_UPDATE_FIELDS, fieldsValue, urow); OperationResponse rsUpdate = session.apply(update); if (rsUpdate.hasRowError()) { System.out.println( "=======================================ERROR UPDATE :" + rsUpdate.getRowError()); } else { System.out.println( "=======================================UPDATE DATA:" + mid + ":" + Arrays.toString(fieldsValue)); } } else { System.out.println( "=======================================ERROR INSERT :" + rsInsert.getRowError()); } } else { System.out.println( "=======================================INSERT DATA:" + mid + ":" + Arrays.toString(fieldsValue)); } } catch (Exception e) { collector.reportError(e); } }