コード例 #1
0
  @Test
  public void testUpsertDateValues() throws Exception {
    long ts = nextTimestamp();
    Date now = new Date(System.currentTimeMillis());
    ensureTableCreated(getUrl(), TestUtil.PTSDB_NAME, null, ts - 2);
    Properties props = new Properties();
    props.setProperty(
        PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 1)); // Execute at timestamp 1
    Connection conn = DriverManager.getConnection(PHOENIX_JDBC_URL, props);
    String dateString = "1999-01-01 02:00:00";
    PreparedStatement upsertStmt =
        conn.prepareStatement(
            "upsert into ptsdb(inst,host,date) values('aaa','bbb',to_date('" + dateString + "'))");
    int rowsInserted = upsertStmt.executeUpdate();
    assertEquals(1, rowsInserted);
    upsertStmt =
        conn.prepareStatement(
            "upsert into ptsdb(inst,host,date) values('ccc','ddd',current_date())");
    rowsInserted = upsertStmt.executeUpdate();
    assertEquals(1, rowsInserted);
    conn.commit();

    props.setProperty(
        PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 2)); // Execute at timestamp 1
    conn = DriverManager.getConnection(PHOENIX_JDBC_URL, props);
    String select = "SELECT date,current_date() FROM ptsdb";
    ResultSet rs = conn.createStatement().executeQuery(select);
    Date then = new Date(System.currentTimeMillis());
    assertTrue(rs.next());
    Date date = DateUtil.parseDate(dateString);
    assertEquals(date, rs.getDate(1));
    assertTrue(rs.next());
    assertTrue(rs.getDate(1).after(now) && rs.getDate(1).before(then));
    assertFalse(rs.next());
  }
 @Override
 public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
   // This serializes the Map. The format is as follows
   // Map size(VInt ie. 1 to 5 bytes) +
   // ( key length [VInt ie. 1 to 5 bytes] + key bytes + value [VInt ie. 1 to 5 bytes] )*
   buffer = new byte[countMapSerializationSize()];
   int offset = 0;
   offset += ByteUtil.vintToBytes(buffer, offset, this.valueVsCount.size());
   for (Entry<ImmutableBytesPtr, Integer> entry : this.valueVsCount.entrySet()) {
     ImmutableBytesPtr key = entry.getKey();
     offset += ByteUtil.vintToBytes(buffer, offset, key.getLength());
     System.arraycopy(key.get(), key.getOffset(), buffer, offset, key.getLength());
     offset += key.getLength();
     offset += ByteUtil.vintToBytes(buffer, offset, entry.getValue().intValue());
   }
   ptr.set(buffer, 0, offset);
   return true;
 }