1 package org.openecomp.core.tools.importinfo;
3 import com.datastax.driver.core.BoundStatement;
4 import com.datastax.driver.core.DataType.Name;
5 import com.datastax.driver.core.PreparedStatement;
6 import com.datastax.driver.core.Session;
7 import com.google.common.collect.ImmutableMap;
8 import com.google.common.collect.ImmutableMap.Builder;
9 import com.google.common.collect.Sets;
10 import java.io.IOException;
11 import java.nio.ByteBuffer;
12 import java.nio.file.Path;
13 import java.util.Base64;
14 import java.util.Date;
15 import java.util.HashMap;
16 import java.util.List;
19 import java.util.stream.Collectors;
20 import org.apache.commons.lang3.StringUtils;
21 import org.codehaus.jackson.map.ObjectMapper;
22 import org.openecomp.core.nosqldb.impl.cassandra.CassandraSessionFactory;
23 import org.openecomp.core.tools.exportinfo.ExportDataCommand;
24 import org.openecomp.core.tools.model.ColumnDefinition;
25 import org.openecomp.core.tools.model.TableData;
26 import org.openecomp.core.tools.util.Utils;
27 import org.openecomp.sdc.logging.api.Logger;
28 import org.openecomp.sdc.logging.api.LoggerFactory;
30 public class ImportSingleTable {
32 private static final Logger logger = LoggerFactory.getLogger(ImportSingleTable.class);
34 private static final String INSERT_INTO = "INSERT INTO ";
35 private static final String VALUES = " VALUES ";
36 private static final Map<String, PreparedStatement> statementsCache = new HashMap<>();
38 public void importFile(Path file) {
40 ObjectMapper objectMapper = new ObjectMapper();
41 TableData tableData = objectMapper.readValue(file.toFile(), TableData.class);
42 Session session = CassandraSessionFactory.getSession();
43 PreparedStatement ps = getPrepareStatement(tableData, session);
44 tableData.rows.forEach(row -> executeQuery(session, ps, tableData.definitions, row));
45 } catch (IOException e) {
46 Utils.logError(logger, e);
51 private PreparedStatement getPrepareStatement(TableData tableData, Session session) {
52 String query = createQuery(tableData);
53 if (statementsCache.containsKey(query)) {
54 return statementsCache.get(query);
56 PreparedStatement preparedStatement = session.prepare(query);
57 statementsCache.put(query, preparedStatement);
58 return preparedStatement;
61 private void executeQuery(Session session, PreparedStatement ps, List<ColumnDefinition> definitions, List<String> rows) {
62 BoundStatement bind = ps.bind();
63 for (int i = 0; i < definitions.size(); i++) {
64 ColumnDefinition columnDefinition = definitions.get(i);
65 String rowData = rows.get(i);
66 Name name = dataTypesMap.get(columnDefinition.getType());
67 handleByType(bind, i, rowData, name);
69 session.execute(bind);
72 private void handleByType(BoundStatement bind, int i, String rowData, Name name) {
77 bind.setString(i, new String(Base64.getDecoder().decode(rowData)));
80 bind.setBytes(i, ByteBuffer.wrap(Base64.getDecoder().decode(rowData.getBytes())));
83 if (StringUtils.isEmpty(rowData)){
84 bind.setDate(i, null);
86 bind.setDate(i, new Date(Long.parseLong(rowData)));
90 bind.setBool(i, Boolean.parseBoolean(rowData));
93 bind.setLong(i, Long.parseLong(rowData));
96 bind.setInt(i, Integer.parseInt(rowData));
99 bind.setFloat(i, Float.parseFloat(rowData));
102 byte[] decoded = Base64.getDecoder().decode(rowData);
103 String decodedStr = new String(decoded);
104 if (!StringUtils.isEmpty(decodedStr)) {
105 String[] splitted = decodedStr.split(ExportDataCommand.JOIN_DELIMITER_SPLITTER);
106 Set set = Sets.newHashSet(splitted);
110 bind.setSet(i, null);
114 byte[] decodedMap = Base64.getDecoder().decode(rowData);
115 String mapStr = new String(decodedMap);
116 if (!StringUtils.isEmpty(mapStr)) {
117 String[] splittedMap = mapStr.split(ExportDataCommand.JOIN_DELIMITER_SPLITTER);
118 Map<String, String> map = new HashMap<>();
119 for (String keyValue : splittedMap) {
120 String[] split = keyValue.split(ExportDataCommand.MAP_DELIMITER_SPLITTER);
121 map.put(split[0], split[1]);
125 bind.setMap(i, null);
129 throw new UnsupportedOperationException("Name is not supported :" + name);
134 private String createQuery(TableData tableData) {
135 ColumnDefinition def = tableData.definitions.iterator().next();
136 StringBuilder sb = new StringBuilder();
137 sb.append(INSERT_INTO).append(def.getKeyspace()).append(".").append(def.getTable());
138 sb.append(tableData.definitions.stream().map(ColumnDefinition::getName).collect(Collectors.joining(" , ", " ( ", " ) ")));
139 sb.append(VALUES).append(tableData.definitions.stream().map(definition -> "?").collect(Collectors.joining(" , ", " ( ", " ) "))).append(";");
140 return sb.toString();
143 public static final ImmutableMap<String, Name> dataTypesMap;
146 Builder<String, Name> builder = ImmutableMap.builder();
147 Name[] values = Name.values();
148 for (Name name : values) {
149 builder.put(name.name().toLowerCase(), name);
151 dataTypesMap = builder.build();