示例#1
0
  /**
   * 单纯插入
   *
   * @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);
    }
  }
示例#2
0
 /** Completes the given operation response. */
 @SuppressWarnings("unchecked")
 private void completeResponse(OperationResponse response, CompletableFuture future) {
   if (response.status() == Response.Status.OK) {
     future.complete(response.result());
     resetMembers();
   } else {
     future.completeExceptionally(response.error().createException());
   }
 }
示例#3
0
  @Test(timeout = 100000)
  public void testBasicOps() throws Exception {
    String tableName = TABLE_NAME_PREFIX + "-testBasicOps";
    table = createTable(tableName, basicSchema, new CreateTableOptions());

    KuduSession session = syncClient.newSession();
    for (int i = 0; i < 10; i++) {
      session.apply(createInsert(i));
    }
    assertEquals(10, countRowsInScan(client.newScannerBuilder(table).build()));

    OperationResponse resp = session.apply(createInsert(0));
    assertTrue(resp.hasRowError());

    session.setFlushMode(SessionConfiguration.FlushMode.MANUAL_FLUSH);

    for (int i = 10; i < 20; i++) {
      session.apply(createInsert(i));
    }
    session.flush();
    assertEquals(20, countRowsInScan(client.newScannerBuilder(table).build()));
  }
示例#4
0
  /**
   * 执行插入更新操作
   *
   * @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);
    }
  }