Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Datastax Java驱动程序中使用异步/批写入功能_Java_Cassandra_Datastax Java Driver - Fatal编程技术网

如何在Datastax Java驱动程序中使用异步/批写入功能

如何在Datastax Java驱动程序中使用异步/批写入功能,java,cassandra,datastax-java-driver,Java,Cassandra,Datastax Java Driver,我计划使用Datastax Java驱动程序来编写Cassandra。。我主要对Datastax java驱动程序的批写和异步功能感兴趣,但我无法获得任何教程来解释如何将这些功能合并到我下面使用Datastax java驱动程序的代码中 /** * Performs an upsert of the specified attributes for the specified id. */ public void upsertAttributes(final String userId, f

我计划使用Datastax Java驱动程序来编写Cassandra。。我主要对Datastax java驱动程序的
批写
异步
功能感兴趣,但我无法获得任何教程来解释如何将这些功能合并到我下面使用Datastax java驱动程序的代码中

/**
 * Performs an upsert of the specified attributes for the specified id.
 */
public void upsertAttributes(final String userId, final Map<String, String> attributes, final String columnFamily) {

    try {

        // make a sql here using the above input parameters.

        String sql = sqlPart1.toString()+sqlPart2.toString();

        DatastaxConnection.getInstance();
        PreparedStatement prepStatement = DatastaxConnection.getSession().prepare(sql);
        prepStatement.setConsistencyLevel(ConsistencyLevel.ONE);        

        BoundStatement query = prepStatement.bind(userId, attributes.values().toArray(new Object[attributes.size()]));

        DatastaxConnection.getSession().execute(query);

    } catch (InvalidQueryException e) {
        LOG.error("Invalid Query Exception in DatastaxClient::upsertAttributes "+e);
    } catch (Exception e) {
        LOG.error("Exception in DatastaxClient::upsertAttributes "+e);
    }
}
/**
*为指定id执行指定属性的upsert。
*/
public void upserttributes(最终字符串userId、最终映射属性、最终字符串columnFamily){
试一试{
//使用上述输入参数在此处生成sql。
字符串sql=sqlPart1.toString()+sqlPart2.toString();
DatastaxConnection.getInstance();
PreparedStatement prepStatement=DatastaxConnection.getSession().prepare(sql);
prepStatement.SetConsistenceLevel(ConsistenceLevel.ONE);
BoundStatement query=prepStatement.bind(userId,attributes.values().toArray(新对象[attributes.size()]);
DatastaxConnection.getSession().execute(查询);
}捕获(InvalidQueryException e){
LOG.error(“DatastaxClient::upsertAttributes中的查询异常无效”+e);
}捕获(例外e){
LOG.error(“DatastaxClient::upsertAttributes中的异常”+e);
}
}
在下面的代码中,我使用Datastax Java驱动程序创建到Cassandra节点的连接

/**
 * Creating Cassandra connection using Datastax Java driver
 *
 */
private DatastaxConnection() {

    try{
        builder = Cluster.builder();
        builder.addContactPoint("some_nodes");

        builder.poolingOptions().setCoreConnectionsPerHost(
                HostDistance.LOCAL,
                builder.poolingOptions().getMaxConnectionsPerHost(HostDistance.LOCAL));

        cluster = builder
                .withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE)
                .withReconnectionPolicy(new ConstantReconnectionPolicy(100L))
                .build();

        StringBuilder s = new StringBuilder();
        Set<Host> allHosts = cluster.getMetadata().getAllHosts();
        for (Host h : allHosts) {
            s.append("[");
            s.append(h.getDatacenter());
            s.append(h.getRack());
            s.append(h.getAddress());
            s.append("]");
        }
        System.out.println("Cassandra Cluster: " + s.toString());

        session = cluster.connect("testdatastaxks");

    } catch (NoHostAvailableException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } catch (Exception e) {

    }
}
/**
*使用Datastax Java驱动程序创建Cassandra连接
*
*/
私有数据连接(){
试一试{
builder=Cluster.builder();
builder.addContactPoint(“某些_节点”);
builder.PooligoOptions().setCoreConnectionsPerHost(
HostDistance.LOCAL,
builder.poolgoptions().getMaxConnectionsPerHost(HostDistance.LOCAL));
集群=构建器
.withRetryPolicy(降级一致性eTryPolicy.INSTANCE)
.使用重新连接策略(新ConstantReconnectionPolicy(100L))
.build();
StringBuilder s=新的StringBuilder();
设置allHosts=cluster.getMetadata().getAllHosts();
用于(主机h:所有主机){
s、 附加(“[”);
s、 追加(h.getDatacenter());
s、 附加(h.getRack());
s、 追加(h.getAddress());
s、 附加(“]”);
}
System.out.println(“Cassandra集群:+s.toString());
session=cluster.connect(“testdatasaxks”);
}捕获(无主机可用例外){
e、 printStackTrace();
抛出新的运行时异常(e);
}捕获(例外e){
}
}
有谁能帮我在上面的代码中添加批写或异步特性吗。。谢谢你的帮助


我正在为asynch运行Cassandra 1.2.9

,只需使用
executeAsync
函数即可:

...
DatastaxConnection.getSession().executeAsync(query);
对于批处理,您需要构建查询(我使用字符串,因为编译器非常了解如何优化字符串连接):

另一方面,我认为您对语句的绑定可能与此类似,假设您将属性更改为映射列表,其中每个映射表示批内的更新/插入:

BoundStatement query = prepStatement.bind(userId,
                                          attributesList.get(0).values().toArray(new Object[attributes.size()]), 
                                          userId_2,
                                          attributesList.get(1).values().toArray(new Object[attributes.size()])); 

对于Lyuben的回答中提供的示例,使用字符串设置批处理的某些属性,如
Type.COUNTER
(如果需要更新计数器),将不起作用。相反,您可以按如下方式成批排列准备好的报表:

final String insertQuery = "INSERT INTO test.prepared (id, col_1) VALUES (?,?);";
final PreparedStatement prepared = session.prepare(insertQuery);

final BatchStatement batch = new BatchStatement(BatchStatement.Type.UNLOGGED);
batch.add(prepared.bind(userId1, "something"));
batch.add(prepared.bind(userId2, "another"));
batch.add(prepared.bind(userId3, "thing"));

session.executeAsync(batch);

有没有一种方法可以通过命名参数来实现这一点?@Highstead什么编程语言?上面是java(),我关注的是python,但我假设如果有一种方法可以在其中一种中实现,那么另一种方法也可以实现。旧的cql驱动程序支持它,但已被弃用。因此,我想用较新的Python DataStax驱动程序替换该功能。@Highstead Python=yes来替换命名参数。这是在服务器端还是客户端完成的?我倾向于用%(p_name)的语法猜测客户端。我更喜欢这个,而不是公认的答案。在这里,批处理的内容可以是动态的(相对于接受答案中的固定CQL和参数数量),我认为这是错误的代码(截至2019年)。BatchStatement是不可变的。您需要批处理=批处理。添加(。。。
final String insertQuery = "INSERT INTO test.prepared (id, col_1) VALUES (?,?);";
final PreparedStatement prepared = session.prepare(insertQuery);

final BatchStatement batch = new BatchStatement(BatchStatement.Type.UNLOGGED);
batch.add(prepared.bind(userId1, "something"));
batch.add(prepared.bind(userId2, "another"));
batch.add(prepared.bind(userId3, "thing"));

session.executeAsync(batch);