com.datastax.driver.core.querybuilder.Insert类的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(16.0k)|赞(0)|评价(0)|浏览(79)

本文整理了Java中com.datastax.driver.core.querybuilder.Insert类的一些代码示例,展示了Insert类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Insert类的具体详情如下:
包路径:com.datastax.driver.core.querybuilder.Insert
类名称:Insert

Insert介绍

[英]A built INSERT statement.
[中]内置的INSERT语句。

代码示例

代码示例来源:origin: prestodb/presto

public static void insertIntoTableMultiPartitionClusteringKeys(CassandraSession session, SchemaTableName table)
{
  for (Integer rowNumber = 1; rowNumber < 10; rowNumber++) {
    Insert insert = QueryBuilder.insertInto(table.getSchemaName(), table.getTableName())
        .value("partition_one", "partition_one_" + rowNumber.toString())
        .value("partition_two", "partition_two_" + rowNumber.toString())
        .value("clust_one", "clust_one")
        .value("clust_two", "clust_two_" + rowNumber.toString())
        .value("clust_three", "clust_three_" + rowNumber.toString());
    session.execute(insert);
  }
  assertEquals(session.execute("SELECT COUNT(*) FROM " + table).all().get(0).getLong(0), 9);
}

代码示例来源:origin: apache/usergrid

@Override
public void writeMessageData( final UUID messageId, final DatabaseQueueMessageBody messageBody ) {
  Preconditions.checkArgument(QakkaUtils.isTimeUuid(messageId), "MessageId is not a type 1 UUID");
  logger.trace("writeMessageData {}", messageId);
  Statement insert = QueryBuilder.insertInto(TABLE_MESSAGE_DATA)
      .value( COLUMN_MESSAGE_ID, messageId)
      .value( COLUMN_MESSAGE_DATA, messageBody.getBlob())
      .value( COLUMN_CONTENT_TYPE, messageBody.getContentType())
    .using( QueryBuilder.ttl( maxTtl ) );
  cassandraClient.getApplicationSession().execute(insert);
}

代码示例来源:origin: kaaproject/kaa

private T insertLocked(T entity) {
 Insert insert = insertInto(getColumnFamilyName()).ifNotExists();
 CassandraEntityMapper<T> entityMapper = CassandraEntityMapper.getEntityMapperForClass(
   getColumnFamilyClass(), cassandraClient);
 for (String name : entityMapper.getKeyColumnNames()) {
  insert.value(name, entityMapper.getColumnValueForName(name, entity, cassandraClient));
 }
 for (String name : entityMapper.getNonKeyColumnNames()) {
  insert.value(name, entityMapper.getColumnValueForName(name, entity, cassandraClient));
 }
 insert.setConsistencyLevel(getWriteConsistencyLevel());
 ResultSet res = execute(insert);
 if (!res.wasApplied()) {
  LOG.error("[{}] Can't insert entity. Entity already exists!", getColumnFamilyClass());
  throw new KaaOptimisticLockingFailureException("Can't insert entity. Entity already exists!");
 } else {
  Clause[] whereClauses = buildKeyClauses(entityMapper, entity);
  Select.Where where = select().from(getColumnFamilyName()).where(whereClauses[0]);
  if (whereClauses.length > 1) {
   for (int i = 1; i < whereClauses.length; i++) {
    where = where.and(whereClauses[i]);
   }
  }
  return findOneByStatement(where);
 }
}

代码示例来源:origin: Netflix/conductor

/**
 * @return cql query statement to insert a new task into the "workflows" table
 */
public String getInsertTaskStatement() {
  return QueryBuilder.insertInto(keyspace, TABLE_WORKFLOWS)
      .value(WORKFLOW_ID_KEY, bindMarker())
      .value(SHARD_ID_KEY, bindMarker())
      .value(TASK_ID_KEY, bindMarker())
      .value(ENTITY_KEY, ENTITY_TYPE_TASK)
      .value(PAYLOAD_KEY, bindMarker())
      .getQueryString();
}

代码示例来源:origin: jooby-project/jooby

private static String insertSQL(final String table, final int timeout) {
 Insert insertInto = insertInto(table)
   .value(ID, raw("?"))
   .value(CREATED_AT, raw("?"))
   .value(ACCESSED_AT, raw("?"))
   .value(SAVED_AT, raw("?"))
   .value(ATTRIBUTES, raw("?"));
 if (timeout > 0) {
  insertInto.using(ttl(timeout));
 }
 return insertInto.getQueryString();
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@Test(groups = "short")
public void textRoutingKeyTest() throws Exception {
 BuiltStatement query;
 TableMetadata table = cluster().getMetadata().getKeyspace(keyspace).getTable(TABLE_TEXT);
 assertNotNull(table);
 ProtocolVersion protocolVersion =
   cluster().getConfiguration().getProtocolOptions().getProtocolVersion();
 CodecRegistry codecRegistry = CodecRegistry.DEFAULT_INSTANCE;
 String txt = "If she weighs the same as a duck... she's made of wood.";
 query = insertInto(table).values(new String[] {"k", "a", "b"}, new Object[] {txt, 1, 2});
 assertEquals(
   query.getRoutingKey(protocolVersion, codecRegistry), ByteBuffer.wrap(txt.getBytes()));
 session().execute(query);
 query = select().from(table).where(eq("k", txt));
 assertEquals(
   query.getRoutingKey(protocolVersion, codecRegistry), ByteBuffer.wrap(txt.getBytes()));
 Row row = session().execute(query).one();
 assertEquals(row.getString("k"), txt);
 assertEquals(row.getInt("a"), 1);
 assertEquals(row.getInt("b"), 2);
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@Test(groups = "short")
public void dateHandlingTest() throws Exception {
 Date d = new Date();
 session().execute(insertInto("dateTest").value("t", d));
 String query = select().from("dateTest").where(eq(token("t"), fcall("token", d))).toString();
 List<Row> rows = session().execute(query).all();
 assertEquals(1, rows.size());
 Row r1 = rows.get(0);
 assertEquals(d, r1.getTimestamp("t"));
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@Test(groups = "unit")
public void markerTest() throws Exception {
 String query;
 Statement insert;
 query = "INSERT INTO test (k,c) VALUES (0,?);";
 insert = insertInto("test").value("k", 0).value("c", bindMarker());
 assertEquals(insert.toString(), query);
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@Test(groups = "short")
public void executeTest() throws Exception {
 session()
   .execute(
     insertInto(TABLE1)
       .value("k", "k1")
       .value("t", "This is a test")
       .value("i", 3)
       .value("f", 0.42));
 session().execute(update(TABLE1).with(set("t", "Another test")).where(eq("k", "k2")));
 List<Row> rows = session().execute(select().from(TABLE1).where(in("k", "k1", "k2"))).all();
 assertEquals(2, rows.size());
 Row r1 = rows.get(0);
 assertEquals("k1", r1.getString("k"));
 assertEquals("This is a test", r1.getString("t"));
 assertEquals(3, r1.getInt("i"));
 assertFalse(r1.isNull("f"));
 Row r2 = rows.get(1);
 assertEquals("k2", r2.getString("k"));
 assertEquals("Another test", r2.getString("t"));
 assertTrue(r2.isNull("i"));
 assertTrue(r2.isNull("f"));
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@Test(groups = "short")
public void batchNonBuiltStatementTest() throws Exception {
 SimpleStatement simple =
   new SimpleStatement("INSERT INTO " + TABLE1 + " (k, t) VALUES ('batchTest1', 'val1')");
 RegularStatement built = insertInto(TABLE1).value("k", "batchTest2").value("t", "val2");
 session().execute(batch().add(simple).add(built));
 List<Row> rows =
   session().execute(select().from(TABLE1).where(in("k", "batchTest1", "batchTest2"))).all();
 assertEquals(2, rows.size());
 Row r1 = rows.get(0);
 assertEquals("batchTest1", r1.getString("k"));
 assertEquals("val1", r1.getString("t"));
 Row r2 = rows.get(1);
 assertEquals("batchTest2", r2.getString("k"));
 assertEquals("val2", r2.getString("t"));
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

private void testWideRows(int key) throws Throwable {
 // Write data
 for (int i = 0; i < 1000000; ++i) {
  session()
    .execute(
      insertInto("wide_rows")
        .value("k", key)
        .value("i", i)
        .setConsistencyLevel(ConsistencyLevel.QUORUM));
 }
 // Read data
 ResultSet rs = session().execute(select("i").from("wide_rows").where(eq("k", key)));
 // Verify data
 int i = 0;
 for (Row row : rs) {
  assertTrue(row.getInt("i") == i++);
 }
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@Test(groups = "short")
public void intRoutingBatchKeyTest() throws Exception {
 assertNotNull(table);
 ProtocolVersion protocolVersion =
   cluster().getConfiguration().getProtocolOptions().getProtocolVersion();
 BuiltStatement batch;
 query = select().from(table).where(eq("k", 42));
 batch_query += "APPLY BATCH;";
 batch =
   batch()
     .add(insertInto(table).values(new String[] {"k", "a"}, new Object[] {42, 1}))
     .add(update(table).using(ttl(400)));
 assertEquals(batch.getRoutingKey(protocolVersion, codecRegistry), bb);
 assertEquals(batch.toString(), batch_query);
 batch_query += "INSERT INTO foo.bar (a) VALUES (123);";
 batch_query += "APPLY BATCH;";
 batch = batch().using(timestamp(42)).add(insertInto("foo", "bar").value("a", 123));
 assertEquals(batch.getRoutingKey(protocolVersion, codecRegistry), null);
 assertEquals(batch.toString(), batch_query);

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@Test(groups = "short")
public void routingKeyColumnCaseSensitivityForQuotedIdentifiersTest() throws Exception {
 BuiltStatement query;
 TableMetadata table = cluster().getMetadata().getKeyspace(keyspace).getTable(TABLE_CASE_QUOTED);
 assertNotNull(table);
 ProtocolVersion protocolVersion =
   cluster().getConfiguration().getProtocolOptions().getProtocolVersion();
 CodecRegistry codecRegistry = CodecRegistry.DEFAULT_INSTANCE;
 query =
   insertInto(table)
     .values(
       new String[] {"\"theKey\"", "a", "b", "\"tHEkEY\""}, new Object[] {42, 1, 2, 3});
 ByteBuffer bb = ByteBuffer.allocate(4);
 bb.putInt(0, 42);
 assertEquals(query.getRoutingKey(protocolVersion, codecRegistry), bb);
 query =
   insertInto(table)
     .values(new String[] {"theKey", "a", "b", "\"tHEkEY\""}, new Object[] {42, 1, 2, 3});
 assertNull(query.getRoutingKey(protocolVersion, codecRegistry));
 query =
   insertInto(table)
     .values(new String[] {"theKey", "a", "b", "theKey"}, new Object[] {42, 1, 2, 3});
 assertNull(query.getRoutingKey(protocolVersion, codecRegistry));
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

"INSERT INTO foo (a,b,\"C\",d) VALUES (123,'127.0.0.1','foo''bar',{'x':3,'y':2}) USING TIMESTAMP 42 AND TTL 24;";
insert =
  insertInto("foo")
    .value("a", 123)
    .value("b", InetAddress.getByName("127.0.0.1"))
    .value(quote("C"), "foo'bar")
    .value(
      "d",
      new TreeMap<String, Integer>() {
    .using(timestamp(42))
    .and(ttl(24));
assertEquals(insert.toString(), query);
insert = insertInto("foo").value("a", 2).value("b", null);
assertEquals(insert.toString(), query);
    .values(
      new String[] {"a", "b"},
      new Object[] {
       3.4
      })
    .using(ttl(24))
    .and(timestamp(42));
assertEquals(insert.toString(), query);
    .values(
      new String[] {"a", "b"},

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@Test(groups = "unit")
@SuppressWarnings("serial")
public void batchTest() throws Exception {
 query += "APPLY BATCH;";
 batch =
   batch()
     .add(
       insertInto("foo")
         .values(
           new String[] {"a", "b"},
           new Object[] {
         .where(eq("k", 1)))
     .using(timestamp(42));
 assertEquals(batch.toString(), query);
 query += "APPLY BATCH;";
 batch = batch(delete().listElt("a", 3).from("foo").where(eq("k", 1)));
 assertEquals(batch.toString(), query);
 assertEquals(batch().toString(), "BEGIN BATCH APPLY BATCH;");

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@Test(groups = "unit")
@SuppressWarnings("serial")
public void insertInjectionTest() throws Exception {
 insert = insertInto("foo").value("a", "123); --comment");
 assertEquals(insert.toString(), query);
 insert = insertInto("foo").value("a,b", 123);
 assertEquals(insert.toString(), query);
   insertInto("foo")
     .values(
       new String[] {"a", "b"},
       new Object[] {
        3.4
       })
     .using(ttl(24))
     .and(timestamp(42));
 assertEquals(insert.toString(), query);

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

private void testByteRows(int key) throws Throwable {
 // Build small ByteBuffer sample
 ByteBuffer bb = ByteBuffer.allocate(58);
 bb.putShort((short) 0xCAFE);
 bb.flip();
 // Write data
 for (int i = 0; i < 1000000; ++i) {
  session()
    .execute(
      insertInto("wide_byte_rows")
        .value("k", key)
        .value("i", bb)
        .setConsistencyLevel(ConsistencyLevel.QUORUM));
 }
 // Read data
 ResultSet rs = session().execute(select("i").from("wide_byte_rows").where(eq("k", key)));
 // Verify data
 for (Row row : rs) {
  assertEquals(row.getBytes("i"), bb);
 }
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@Test(groups = "unit")
public void should_serialize_collections_of_serializable_elements() {
 Set<UUID> set = Sets.newHashSet(UUID.randomUUID());
 List<Date> list = Lists.newArrayList(new Date());
 Map<BigInteger, String> map = ImmutableMap.of(new BigInteger("1"), "foo");
 BuiltStatement query = insertInto("foo").value("v", set);
 assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES (?);");
 assertThat(query.getObject(0)).isEqualTo(set);
 query = insertInto("foo").value("v", list);
 assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES (?);");
 assertThat(query.getObject(0)).isEqualTo(list);
 query = insertInto("foo").value("v", map);
 assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES (?);");
 assertThat(query.getObject(0)).isEqualTo(map);
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

value = "3.6",
  description = "Requires CASSANDRA-7423 introduced in Cassandra 3.6")
@Test(groups = "short")
public void should_support_setting_and_retrieving_udt_fields() {
 String udt = "person";
 session()
   .execute(
     createType(udt).addColumn("first", DataType.text()).addColumn("last", DataType.text()));
 UserType userType = cluster().getMetadata().getKeyspace(keyspace).getUserType(udt);
   .execute(
     createTable(table)
       .addPartitionKey("k", DataType.text())
 value.setString("first", "Bob");
 value.setString("last", "Smith");
 session().execute(insertInto(table).value("k", "key").value("u", value));
   .execute(
     update(table)
       .with(set(path("u", "first"), "Rick"))
       .and(set(raw("u.last"), "Jones"))
       .where(eq("k", "key")));
     .execute(select().path("u", "first").raw("u.last").from(table).where(eq("k", "key")))
     .one();

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@Test(groups = "short")
public void should_handle_collections_of_UDT() throws Exception {
 UserType udtType = cluster().getMetadata().getKeyspace(keyspace).getUserType("udt");
 UDTValue udtValue =
   udtType.newValue().setInt("i", 2).setInet("a", InetAddress.getByName("localhost"));
 UDTValue udtValue2 =
   udtType.newValue().setInt("i", 3).setInet("a", InetAddress.getByName("localhost"));
 Statement insert = insertInto("udtTest").value("k", 1).value("l", ImmutableList.of(udtValue));
 assertThat(insert.toString())
   .isEqualTo("INSERT INTO udtTest (k,l) VALUES (1,[{i:2,a:'127.0.0.1'}]);");
 session().execute(insert);
 List<Row> rows = session().execute(select().from("udtTest").where(eq("k", 1))).all();
 assertThat(rows.size()).isEqualTo(1);
 Row r1 = rows.get(0);
 assertThat(r1.getList("l", UDTValue.class).get(0).getInet("a").getHostAddress())
   .isEqualTo("127.0.0.1");
 Map<Integer, UDTValue> map = Maps.newHashMap();
 map.put(0, udtValue);
 map.put(2, udtValue2);
 Statement updateMap = update("udtTest").with(putAll("m", map)).where(eq("k", 1));
 assertThat(updateMap.toString())
   .isEqualTo(
     "UPDATE udtTest SET m=m+{0:{i:2,a:'127.0.0.1'},2:{i:3,a:'127.0.0.1'}} WHERE k=1;");
 session().execute(updateMap);
 rows = session().execute(select().from("udtTest").where(eq("k", 1))).all();
 r1 = rows.get(0);
 assertThat(r1.getMap("m", Integer.class, UDTValue.class)).isEqualTo(map);
}

相关文章