简体   繁体   English

防风栓中的动态卡桑德拉连接

[英]Dynamic cassandra connection in storm bolt

I am running a Storm topology that reads data from Kafka broker and writes into Cassandra. 我正在运行一个Storm拓扑,该拓扑从Kafka代理读取数据并写入Cassandra。 One of my Cassandra bolts performs read and write operations. 我的Cassandra螺栓之一执行读取和写入操作。 My keyspace always set dynamically. 我的键空间始终是动态设置的。 Now I want to get a connection to Cassandra using a connection pool? 现在我想使用连接池获得与Cassandra的连接吗?

My stream has keyspace name in it. 我的流中有键空间名称。 I need to dynamically insert data into appropriate keyspace/ 我需要将数据动态插入适当的键空间/

1) I tried to get a Cassandra connection using connection pool method inside of execute method so that each and every tuple gets a Cassandra connection. 1)我试图使用execute方法内部的连接池方法来获得Cassandra连接,以便每个元组都获得一个Cassandra连接。 So after some point of time my connection reached my thread 1024 pool connection limit. 因此,经过一段时间后,我的连接达到了线程1024池连接限制。 Connection time out error after. 连接超时错误之后。

Example: 例:

ExecutorService pool = Executors.newFixedThreadPool(1024); 
public void execute(Tuple input)    {
  if(input.size()>0)        {          
     ConnectionManager cm=new ConnectionManager();     
     cm.keyspace = "dda400db5ef2d";
     statement = cm.poolRun();  
     cql="select * form columnfamily where id='d78346';
   }
}

2) I tried to get a connection using prepare method when the topology initialized the worker and get a static connection 2)当拓扑初始化worker并获得静态连接时,我尝试使用prepare方法获得连接

public void prepare(Map stormConf, TopologyContext context,OutputCollector collector) {          
  _OutputCollector=collector;
  ConnectionManager cm=new ConnectionManager();
  cm.keyspace ="dda400db5ef2d";    statement = cm.poolRun();    
} 

public void execute(Tuple input)  { 
  if(input.size()>0) {          
    cql="select * form columnfamily where id='d78346';
  }
}

The second case works if data belongs to one keyspace. 如果数据属于一个键空间,则第二种情况适用。 But my case data belongs to different keyspaces, and here only one topology it will identify keyspace and write into that keyspace. 但是我的案例数据属于不同的键空间,在这里只有一种拓扑可以识别键空间并写入该键空间。

Is there any hashing method available in storm to hold a keyspace connections? 风暴中是否有任何哈希方法来保存键空间连接?

or 要么

Any other logic? 还有其他逻辑吗?

I'm not familiar with Cassandra but I think this is what you want: 我不熟悉Cassandra,但我认为这是您想要的:

private ConnectionManager cm = null;
public void prepare(Map stormConf, TopologyContext context,OutputCollector collector) {          
  _OutputCollector=collector;
  cm=new ConnectionManager();    
}

public void execute(Tuple input)  { 
  cm.keyspace = "dda400db5ef2d";
  statement = cm.poolRun();  
  cql="select * form columnfamily where id='d78346';
}

The prepare method is run at the start of the topology once. prepare方法在拓扑的开头运行一次。 You can use it to initialize your variables/connections and run them in your execute method. 您可以使用它来初始化变量/连接并在execute方法中运行它们。 You will probably want to close your connection once your query is complete and reset the connection on the next query. 查询完成后,您可能希望关闭连接,并在下一个查询中重置连接。 Hope this helps 希望这可以帮助

Do following things - 做以下事情-

1) Not just Cassandra cluster however, create a session in prepare method of your bolt. 1)但是,不仅是Cassandra集群,还需要使用螺栓的prepare方法创建一个会话。 Do not use keyspace while create a session. 创建会话时不要使用键空间。

public void prepare(Map stormConf, TopologyContext context,OutputCollector collector) {   
    CassandraConnector client = new CassandraConnector();
    client.connect("127.0.0.1", 9142);
    this.session = client.getSession();
}

2) Now create your query based on your data. 2)现在,根据您的数据创建查询。 Here you should have your keyspace name prefixed with your table name. 在这里,您的键空间名称应以表名称为前缀。

public void execute(Tuple input)  { 
  if(input.size()>0) {          
     cql="select * form keyspace.table where id='d78346'";
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM