本文整理匯總了Java中com.datastax.driver.core.Session.prepare方法的典型用法代碼示例。如果您正苦於以下問題:Java Session.prepare方法的具體用法?Java Session.prepare怎麽用?Java Session.prepare使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.datastax.driver.core.Session
的用法示例。
在下文中一共展示了Session.prepare方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testDelayOnPreparedStatementWhenIgnoreOnPrepareIsFalse
import com.datastax.driver.core.Session; //導入方法依賴的package包/類
@Test
public void testDelayOnPreparedStatementWhenIgnoreOnPrepareIsFalse() throws Exception {
Prime prime =
PrimeDsl.when("select * from table where c1=?")
.then(noRows())
.delay(2, TimeUnit.SECONDS)
.applyToPrepare()
.build();
HttpTestResponse response = server.prime(prime.getPrimedRequest());
assertNotNull(response);
RequestPrime responseQuery = om.readValue(response.body, RequestPrime.class);
assertThat(responseQuery).isEqualTo(prime.getPrimedRequest());
String contactPoint = HttpTestUtil.getContactPointString(server.getCluster(), 0);
try (com.datastax.driver.core.Cluster cluster =
defaultBuilder().addContactPoint(contactPoint).build()) {
Session session = cluster.connect();
long start = System.currentTimeMillis();
session.prepare("select * from table where c1=?");
long duration = System.currentTimeMillis() - start;
// should have taken longer than 2 seconds.
assertThat(duration).isGreaterThan(2000);
}
}
示例2: main
import com.datastax.driver.core.Session; //導入方法依賴的package包/類
public static void main(String[] args) {
Session session = Connection.connect();
PreparedStatement preparedStatement = session.prepare("insert into user (id, name, age) values (?, ?, ?)");
try {
BoundStatement boundStatement = preparedStatement.bind(UUIDs.timeBased(), "Hector", 34);
ResultSet rs = session.execute(boundStatement);
System.out.println(rs);
} catch (Exception ex) {
ex.printStackTrace();
}
Connection.close();
}
示例3: main
import com.datastax.driver.core.Session; //導入方法依賴的package包/類
public static void main(String[] args) {
Session session = Connection.connect();
BatchStatement batchStatement = new BatchStatement();
PreparedStatement preparedStatement = session.prepare("insert into user (id, name) values (?, ?)");
int i = 0;
while(i < 10) {
batchStatement.add(preparedStatement.bind(UUIDs.timeBased(), "user-" + i));
++i;
}
try {
ResultSet rs = session.execute(batchStatement);
System.out.println(rs);
} catch (Exception ex) {
ex.printStackTrace();
}
Connection.close();
}
示例4: Indexer
import com.datastax.driver.core.Session; //導入方法依賴的package包/類
Indexer(Session session, @Nullable Integer indexTtl,
@Nullable ConcurrentMap<PartitionKeyToTraceId, Pair<Long>> sharedState, IndexSupport index) {
this.index = index;
this.boundName = UPPER_CAMEL.to(LOWER_HYPHEN, index.getClass().getSimpleName());
Insert insert = index.declarePartitionKey(QueryBuilder.insertInto(index.table())
.value("ts", QueryBuilder.bindMarker("ts"))
.value("trace_id", QueryBuilder.bindMarker("trace_id")));
if (indexTtl != null) {
insert.using(QueryBuilder.ttl(QueryBuilder.bindMarker("ttl_")));
}
this.prepared = session.prepare(insert);
this.indexTtl = indexTtl;
this.session = session;
this.timestampCodec = new TimestampCodec(session);
this.sharedState = sharedState;
}
示例5: testNoDelayOnPreparedStatementWhenIgnoreOnPrepareIsFalse
import com.datastax.driver.core.Session; //導入方法依賴的package包/類
@Test
public void testNoDelayOnPreparedStatementWhenIgnoreOnPrepareIsFalse() throws Exception {
Prime prime =
PrimeDsl.when("select * from table where c1=?")
.then(noRows())
.delay(2, TimeUnit.SECONDS)
.ignoreOnPrepare()
.build();
HttpTestResponse response = server.prime(prime.getPrimedRequest());
assertNotNull(response);
RequestPrime responseQuery = om.readValue(response.body, RequestPrime.class);
assertThat(responseQuery).isEqualTo(prime.getPrimedRequest());
String contactPoint = HttpTestUtil.getContactPointString(server.getCluster(), 0);
try (com.datastax.driver.core.Cluster cluster =
defaultBuilder().addContactPoint(contactPoint).build()) {
Session session = cluster.connect();
long start = System.currentTimeMillis();
PreparedStatement prepared = session.prepare("select * from table where c1=?");
long duration = System.currentTimeMillis() - start;
// should not have applied delay to prepare.
assertThat(duration).isLessThan(2000);
start = System.currentTimeMillis();
session.execute(prepared.bind());
duration = System.currentTimeMillis() - start;
// should have taken longer than 2 seconds as delay is applied to execute.
assertThat(duration).isGreaterThan(2000);
}
}
示例6: makeNativeBoundQueryWithNameParams
import com.datastax.driver.core.Session; //導入方法依賴的package包/類
public static ResultSet makeNativeBoundQueryWithNameParams(
String query, String contactPoint, Map<String, Long> values) {
com.datastax.driver.core.Cluster cluster =
defaultBuilder().addContactPoint(contactPoint).build();
Session session = cluster.connect();
com.datastax.driver.core.PreparedStatement prepared = session.prepare(query);
BoundStatement bound = prepared.bind();
values.forEach((k, v) -> bound.setLong(k, v));
return executeQueryWithFreshSession(bound, contactPoint, session, cluster);
}
示例7: makeNativeBoundQueryWithNameParamsStrings
import com.datastax.driver.core.Session; //導入方法依賴的package包/類
public static ResultSet makeNativeBoundQueryWithNameParamsStrings(
String query, String contactPoint, Map<String, String> values) {
com.datastax.driver.core.Cluster cluster =
defaultBuilder().addContactPoint(contactPoint).build();
Session session = cluster.connect();
com.datastax.driver.core.PreparedStatement prepared = session.prepare(query);
BoundStatement bound = getBoundStatementNamed(query, contactPoint, values);
return executeQueryWithFreshSession(bound, contactPoint, session, cluster);
}
示例8: makeNativeBoundQueryWithPositionalParam
import com.datastax.driver.core.Session; //導入方法依賴的package包/類
public static ResultSet makeNativeBoundQueryWithPositionalParam(
String query, String contactPoint, Object param) {
com.datastax.driver.core.Cluster cluster =
defaultBuilder().addContactPoint(contactPoint).build();
Session session = cluster.connect();
com.datastax.driver.core.PreparedStatement prepared = session.prepare(query);
BoundStatement bound = getBoundStatement(query, contactPoint, param);
return executeQueryWithFreshSession(bound, contactPoint, session, cluster);
}
示例9: getBoundStatement
import com.datastax.driver.core.Session; //導入方法依賴的package包/類
public static BoundStatement getBoundStatement(String query, String contactPoint, Object param) {
try (com.datastax.driver.core.Cluster cluster =
defaultBuilder().addContactPoint(contactPoint).build()) {
Session session = cluster.connect();
com.datastax.driver.core.PreparedStatement prepared = session.prepare(query);
BoundStatement bound = prepared.bind(param);
return bound;
}
}
示例10: getBoundStatementNamed
import com.datastax.driver.core.Session; //導入方法依賴的package包/類
public static BoundStatement getBoundStatementNamed(
String query, String contactPoint, Map<String, String> values) {
try (com.datastax.driver.core.Cluster cluster =
defaultBuilder().addContactPoint(contactPoint).build()) {
Session session = cluster.connect();
com.datastax.driver.core.PreparedStatement prepared = session.prepare(query);
BoundStatement bound = prepared.bind();
values.forEach((k, v) -> bound.setString(k, v));
return bound;
}
}
示例11: prepareAndQuery
import com.datastax.driver.core.Session; //導入方法依賴的package包/類
private ResultSet prepareAndQuery() throws Exception {
try (com.datastax.driver.core.Cluster driverCluster =
defaultBuilder(server.getCluster())
.withRetryPolicy(FallthroughRetryPolicy.INSTANCE)
.build()) {
Session session = driverCluster.connect();
PreparedStatement prepared = session.prepare(query);
return session.execute(prepared.bind());
}
}
示例12: insertMulti
import com.datastax.driver.core.Session; //導入方法依賴的package包/類
@Override
public Status insertMulti(List<TsPoint> points) {
long costTime = 0L;
if (points != null) {
Cluster cluster = null;
try {
// cluster = Cluster.builder().addContactPoint(CASSADRA_URL).build();
// Session session = cluster.connect(KEY_SPACE_NAME);
Session session = SessionManager.getSession();
BatchStatement batch = new BatchStatement();
PreparedStatement ps = session.prepare(
"INSERT INTO " + TABLE_NAME + "(timestamp,device_code,sensor_code,value) VALUES(?,?,?,?)");
for (TsPoint point : points) {
BoundStatement bs = ps.bind(new Date(point.getTimestamp()), point.getDeviceCode(),
point.getSensorCode(), Double.parseDouble(point.getValue().toString()));
batch.add(bs);
}
long startTime = System.nanoTime();
session.execute(batch);
long endTime = System.nanoTime();
costTime = endTime - startTime;
batch.clear();
// session.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cluster != null)
cluster.close();
}
}
// System.out.println("costTime=" + costTime);
return Status.OK(costTime);
}
示例13: updatePoints
import com.datastax.driver.core.Session; //導入方法依賴的package包/類
@Override
public Status updatePoints(List<TsPoint> points) {
long costTime = 0L;
if (points != null) {
Cluster cluster = null;
try {
// cluster = Cluster.builder().addContactPoint(CASSADRA_URL).build();
// Session session = cluster.connect(KEY_SPACE_NAME);
Session session = SessionManager.getSession();
BatchStatement batch = new BatchStatement();
PreparedStatement ps = session.prepare(
"UPDATE " + TABLE_NAME + " SET value=? WHERE timestamp=? and device_code=? and sensor_code=?");
for (TsPoint point : points) {
BoundStatement bs = ps.bind(Double.parseDouble(point.getValue().toString()),new Date(point.getTimestamp()), point.getDeviceCode(),
point.getSensorCode());
batch.add(bs);
}
long startTime = System.nanoTime();
session.execute(batch);
long endTime = System.nanoTime();
costTime = endTime - startTime;
batch.clear();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cluster != null)
cluster.close();
}
}
// System.out.println("此次更新消耗時間[" + costTime / 1000 + "]s");
return Status.OK(costTime);
}
示例14: CassandraSpanConsumer
import com.datastax.driver.core.Session; //導入方法依賴的package包/類
CassandraSpanConsumer(Session session) {
this.session = session;
this.metadata = Schema.readMetadata(session);
insertSpan = session.prepare(
QueryBuilder
.insertInto(Schema.TABLE_TRACES)
.value("trace_id", QueryBuilder.bindMarker("trace_id"))
.value("ts_uuid", QueryBuilder.bindMarker("ts_uuid"))
.value("id", QueryBuilder.bindMarker("id"))
.value("ts", QueryBuilder.bindMarker("ts"))
.value("span_name", QueryBuilder.bindMarker("span_name"))
.value("parent_id", QueryBuilder.bindMarker("parent_id"))
.value("duration", QueryBuilder.bindMarker("duration"))
.value("annotations", QueryBuilder.bindMarker("annotations"))
.value("binary_annotations", QueryBuilder.bindMarker("binary_annotations"))
.value("all_annotations", QueryBuilder.bindMarker("all_annotations")));
insertTraceServiceSpanName = session.prepare(
QueryBuilder
.insertInto(Schema.TABLE_TRACE_BY_SERVICE_SPAN)
.value("service_name", QueryBuilder.bindMarker("service_name"))
.value("span_name", QueryBuilder.bindMarker("span_name"))
.value("bucket", QueryBuilder.bindMarker("bucket"))
.value("ts", QueryBuilder.bindMarker("ts"))
.value("trace_id", QueryBuilder.bindMarker("trace_id"))
.value("duration", QueryBuilder.bindMarker("duration")));
insertServiceSpanName = session.prepare(
QueryBuilder
.insertInto(Schema.TABLE_SERVICE_SPANS)
.value("service_name", QueryBuilder.bindMarker("service_name"))
.value("span_name", QueryBuilder.bindMarker("span_name")));
}
示例15: CassandraSpanConsumer
import com.datastax.driver.core.Session; //導入方法依賴的package包/類
CassandraSpanConsumer(Session session, int bucketCount, int spanTtl, int indexTtl,
@Nullable CacheBuilderSpec indexCacheSpec) {
this.session = session;
this.timestampCodec = new TimestampCodec(session);
this.spanTtl = spanTtl;
this.metadata = Schema.readMetadata(session);
this.indexTtl = metadata.hasDefaultTtl ? null : indexTtl;
insertSpan = session.prepare(
maybeUseTtl(QueryBuilder
.insertInto("traces")
.value("trace_id", QueryBuilder.bindMarker("trace_id"))
.value("ts", QueryBuilder.bindMarker("ts"))
.value("span_name", QueryBuilder.bindMarker("span_name"))
.value("span", QueryBuilder.bindMarker("span"))));
insertServiceName = session.prepare(
maybeUseTtl(QueryBuilder
.insertInto(Tables.SERVICE_NAMES)
.value("service_name", QueryBuilder.bindMarker("service_name"))));
insertSpanName = session.prepare(
maybeUseTtl(QueryBuilder
.insertInto(Tables.SPAN_NAMES)
.value("service_name", QueryBuilder.bindMarker("service_name"))
.value("bucket", 0) // bucket is deprecated on this index
.value("span_name", QueryBuilder.bindMarker("span_name"))));
deduplicatingExecutor = new DeduplicatingExecutor(session, WRITTEN_NAMES_TTL);
indexer = new CompositeIndexer(session, indexCacheSpec, bucketCount, this.indexTtl);
}