Storm Topology从kafka读取数据并写入cassandra表
在Storm中,我正在准备方法中创建cassandra集群连接和会话。
cassandraCluster = Cluster.builder().withoutJMXReporting().withoutMetrics()
.addContactPoints(nodes)
.withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE)
.withReconnectionPolicy(new ExponentialReconnectionPolicy(100L,
TimeUnit.MINUTES.toMillis(5)))
.withLoadBalancingPolicy(
new TokenAwarePolicy(new RoundRobinPolicy()))
.build();
session = cassandraCluster.connect(keyspace);
在execute方法中,我可以处理元组并将其保存在cassandra表中
假设我想将单个元组中的数据写入多个表 为每个表编写单独的螺栓将是不错的选择。但我必须创建集群连接并在每个螺栓中对每个表进行会话。
但是在这个链接中,每个群集的单个连接对于性能来说是一个好主意 http://www.datastax.com/dev/blog/4-simple-rules-when-using-the-datastax-drivers-for-cassandra
您是否有任何想法在一个螺栓中创建群集连接并在其他螺栓中使用此连接?
答案 0 :(得分:0)
这取决于风暴如何为工人分配螺栓和喷口。您不能假设您可以在螺栓之间共享连接,因为它们可能在不同的工作程序(读取:JVM)中运行,或者完全在不同的节点上运行。
请在此处查看我的回答:Mongo connection pooling for Storm topology
可能看起来像这个伪代码:
public class CassandraBolt extends BaseRichBolt {
private static final long serialVersionUID = 1L;
private static Logger LOG = LoggerFactory.getLogger(CassandraBolt.class);
OutputCollector _collector;
// whatever your cassandra session is
// has to be transient because session is not serializable
protected transient CassandraSession _session;
@SuppressWarnings("rawtypes")
@Override
public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
_collector = collector;
// maybe get properties from stormConf instead of hard coding them
cassandraCluster = Cluster.builder().withoutJMXReporting().withoutMetrics()
.addContactPoints(nodes)
.withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE)
.withReconnectionPolicy(new ExponentialReconnectionPolicy(100L,
TimeUnit.MINUTES.toMillis(5)))
.withLoadBalancingPolicy(
new TokenAwarePolicy(new RoundRobinPolicy()))
.build();
_session = cassandraCluster.connect(keyspace);
}
@Override
public void execute(Tuple input) {
try {
// use _session to talk to cassandra
} catch (Exception e) {
LOG.error("CassandraBolt error", e);
_collector.reportError(e);
}
}
@Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
// TODO Auto-generated method stub
}
}