forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CassandraAbstractSink.java
110 lines (98 loc) · 4.16 KB
/
CassandraAbstractSink.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.pulsar.io.cassandra;
import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.ResultSetFuture;
import com.datastax.driver.core.Session;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.MoreExecutors;
import java.util.Map;
import org.apache.pulsar.functions.api.Record;
import org.apache.pulsar.io.core.KeyValue;
import org.apache.pulsar.io.core.Sink;
import org.apache.pulsar.io.core.SinkContext;
/**
* A Simple abstract class for Cassandra sink
* Users need to implement extractKeyValue function to use this sink
*/
public abstract class CassandraAbstractSink<K, V> implements Sink<byte[]> {
// ----- Runtime fields
private Cluster cluster;
private Session session;
CassandraSinkConfig cassandraSinkConfig;
private PreparedStatement statement;
@Override
public void open(Map<String, Object> config, SinkContext sinkContext) throws Exception {
cassandraSinkConfig = CassandraSinkConfig.load(config);
if (cassandraSinkConfig.getRoots() == null
|| cassandraSinkConfig.getKeyspace() == null
|| cassandraSinkConfig.getKeyname() == null
|| cassandraSinkConfig.getColumnFamily() == null
|| cassandraSinkConfig.getColumnName() == null) {
throw new IllegalArgumentException("Required property not set.");
}
createClient(cassandraSinkConfig.getRoots());
statement = session.prepare("INSERT INTO " + cassandraSinkConfig.getColumnFamily() + " ("
+ cassandraSinkConfig.getKeyname() + ", " + cassandraSinkConfig.getColumnName() + ") VALUES (?, ?)");
}
@Override
public void close() throws Exception {
session.close();
cluster.close();
}
@Override
public void write(Record<byte[]> record) {
KeyValue<K, V> keyValue = extractKeyValue(record);
BoundStatement bound = statement.bind(keyValue.getKey(), keyValue.getValue());
ResultSetFuture future = session.executeAsync(bound);
Futures.addCallback(future,
new FutureCallback<ResultSet>() {
@Override
public void onSuccess(ResultSet result) {
record.ack();
}
@Override
public void onFailure(Throwable t) {
record.fail();
}
}, MoreExecutors.directExecutor());
}
private void createClient(String roots) {
String[] hosts = roots.split(",");
if (hosts.length <= 0) {
throw new RuntimeException("Invalid cassandra roots");
}
Cluster.Builder b = Cluster.builder();
for (int i = 0; i < hosts.length; ++i) {
String[] hostPort = hosts[i].split(":");
b.addContactPoint(hostPort[0]);
if (hostPort.length > 1) {
b.withPort(Integer.parseInt(hostPort[1]));
}
}
cluster = b.build();
session = cluster.connect();
session.execute("USE " + cassandraSinkConfig.getKeyspace());
}
public abstract KeyValue<K, V> extractKeyValue(Record<byte[]> record);
}