jhnmora000 commented on a change in pull request #178: GORA-485 Apache Kudu 
datastore for Gora
URL: https://github.com/apache/gora/pull/178#discussion_r316971247
 
 

 ##########
 File path: gora-kudu/src/main/java/org/apache/gora/kudu/store/KuduStore.java
 ##########
 @@ -0,0 +1,654 @@
+/*
+ * 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.gora.kudu.store;
+
+import org.apache.gora.kudu.query.KuduResult;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.invoke.MethodHandles;
+import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.concurrent.ConcurrentHashMap;
+import org.apache.gora.kudu.mapping.Column;
+import org.apache.gora.kudu.mapping.KuduMapping;
+import org.apache.gora.kudu.mapping.KuduMappingBuilder;
+import org.apache.gora.kudu.utils.KuduParameters;
+import org.apache.gora.persistency.impl.PersistentBase;
+import org.apache.gora.query.PartitionQuery;
+import org.apache.gora.query.Query;
+import org.apache.gora.query.Result;
+import org.apache.gora.store.impl.DataStoreBase;
+import org.apache.gora.util.AvroUtils;
+import org.apache.gora.util.GoraException;
+import org.apache.kudu.ColumnSchema;
+import org.apache.avro.Schema;
+import org.apache.avro.specific.SpecificDatumReader;
+import org.apache.avro.specific.SpecificDatumWriter;
+import org.apache.avro.util.Utf8;
+import org.apache.gora.kudu.query.KuduQuery;
+import org.apache.gora.kudu.utils.KuduClientUtils;
+import org.apache.gora.persistency.Persistent;
+import org.apache.gora.query.impl.PartitionQueryImpl;
+import org.apache.gora.util.IOUtils;
+import org.apache.kudu.Type;
+import org.apache.kudu.client.CreateTableOptions;
+import org.apache.kudu.client.Delete;
+import org.apache.kudu.client.KuduClient;
+import org.apache.kudu.client.KuduException;
+import org.apache.kudu.client.KuduPredicate;
+import org.apache.kudu.client.KuduScanner;
+import org.apache.kudu.client.KuduSession;
+import org.apache.kudu.client.KuduTable;
+import org.apache.kudu.client.OperationResponse;
+import org.apache.kudu.client.PartialRow;
+import org.apache.kudu.client.RowResult;
+import org.apache.kudu.client.RowResultIterator;
+import org.apache.kudu.client.SessionConfiguration;
+import org.apache.kudu.client.Update;
+import org.apache.kudu.client.Upsert;
+import org.apache.kudu.util.DecimalUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Implementation of a Apache Kudu data store to be used by Apache Gora.
+ *
+ * @param <K> class to be used for the key
+ * @param <T> class to be persisted within the store
+ */
+public class KuduStore<K, T extends PersistentBase> extends DataStoreBase<K, 
T> {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+  private static final String PARSE_MAPPING_FILE_KEY = 
"gora.kudu.mapping.file";
+  private static final String DEFAULT_MAPPING_FILE = "gora-kudu-mapping.xml";
+  private static final String XML_MAPPING_DEFINITION = "gora.mapping";
+  private KuduMapping kuduMapping;
+  private KuduClient client;
+  private KuduSession session;
+  private KuduTable table;
+
+  private static final ConcurrentHashMap<Schema, SpecificDatumReader<?>> 
readerMap = new ConcurrentHashMap<>();
+  private static final ConcurrentHashMap<Schema, SpecificDatumWriter<?>> 
writerMap = new ConcurrentHashMap<>();
+
+  @Override
+  public void initialize(Class<K> keyClass, Class<T> persistentClass, 
Properties properties) throws GoraException {
+    try {
+      super.initialize(keyClass, persistentClass, properties);
+      KuduMappingBuilder<K, T> builder = new KuduMappingBuilder<>(this);
+      InputStream mappingStream;
+      if (properties.containsKey(XML_MAPPING_DEFINITION)) {
+        if (LOG.isTraceEnabled()) {
+          LOG.trace("{} = {}", XML_MAPPING_DEFINITION, 
properties.getProperty(XML_MAPPING_DEFINITION));
+        }
+        mappingStream = 
org.apache.commons.io.IOUtils.toInputStream(properties.getProperty(XML_MAPPING_DEFINITION),
 (Charset) null);
+      } else {
+        mappingStream = 
getClass().getClassLoader().getResourceAsStream(getConf().get(PARSE_MAPPING_FILE_KEY,
 DEFAULT_MAPPING_FILE));
+      }
+      builder.readMappingFile(mappingStream);
+      kuduMapping = builder.getKuduMapping();
+      KuduParameters kuduParameters = KuduParameters.load(properties, 
getConf());
+      KuduClient.KuduClientBuilder kuduClientBuilder = new 
KuduClient.KuduClientBuilder(kuduParameters.getMasterAddresses());
+      if (kuduParameters.getBossCount() != null) {
+        kuduClientBuilder.bossCount(kuduParameters.getBossCount());
+      }
+      if (kuduParameters.getDefaultAdminOperationTimeoutMs() != null) {
+        
kuduClientBuilder.defaultAdminOperationTimeoutMs(kuduParameters.getDefaultAdminOperationTimeoutMs());
+      }
+      if (kuduParameters.getDefaultOperationTimeoutMs() != null) {
+        
kuduClientBuilder.defaultOperationTimeoutMs(kuduParameters.getDefaultOperationTimeoutMs());
+      }
+      if (kuduParameters.getDefaultSocketReadTimeoutMs() != null) {
+        
kuduClientBuilder.defaultSocketReadTimeoutMs(kuduParameters.getDefaultSocketReadTimeoutMs());
+      }
+      if (kuduParameters.getWorkerCount() != null) {
+        kuduClientBuilder.workerCount(kuduParameters.getWorkerCount());
+      }
+      if (kuduParameters.isClientStatistics() != null && 
!kuduParameters.isClientStatistics()) {
+        kuduClientBuilder.disableStatistics();
+      }
+      client = kuduClientBuilder.build();
+      session = client.newSession();
+      if (kuduParameters.getFlushMode() != null) {
+        
session.setFlushMode(SessionConfiguration.FlushMode.valueOf(kuduParameters.getFlushMode()));
+      }
+      if (kuduParameters.getFlushInterval() != null) {
+        session.setFlushInterval(kuduParameters.getFlushInterval());
+      }
+
+      LOG.info("Kudu store was successfully initialized");
+      if (!schemaExists()) {
+        createSchema();
+      } else {
+        table = client.openTable(kuduMapping.getTableName());
+      }
+    } catch (Exception ex) {
+      throw new GoraException(ex);
+    }
+  }
+
+  @Override
+  public String getSchemaName() {
+    return kuduMapping.getTableName();
+  }
+
+  @Override
+  public String getSchemaName(final String mappingSchemaName, final Class<?> 
persistentClass) {
+    return super.getSchemaName(mappingSchemaName, persistentClass);
+  }
+
+  @Override
+  public void createSchema() throws GoraException {
+    if (client == null) {
+      throw new GoraException(
+          "Impossible to create the schema as no connection has been 
initiated.");
+    }
+    if (schemaExists()) {
+      return;
+    }
+    try {
+      List<ColumnSchema> columns = new ArrayList<>();
+      List<String> keys = new ArrayList<>();
+      for (Column pk : kuduMapping.getPrimaryKey()) {
+        columns.add(new ColumnSchema.ColumnSchemaBuilder(pk.getName(), 
pk.getDataType().getType()).key(true).build());
+        keys.add(pk.getName());
+      }
+      for (Map.Entry<String, Column> clt : kuduMapping.getFields().entrySet()) 
{
+        Column aColumn = clt.getValue();
+        ColumnSchema aColumnSch;
+        ColumnSchema.ColumnSchemaBuilder aBaseColumn = new 
ColumnSchema.ColumnSchemaBuilder(aColumn.getName(), 
aColumn.getDataType().getType()).nullable(true);
+        if (aColumn.getDataType().getType() == Type.DECIMAL) {
+          aColumnSch = 
aBaseColumn.typeAttributes(DecimalUtil.typeAttributes(aColumn.getDataType().getPrecision(),
 aColumn.getDataType().getScale())).build();
+        } else {
+          aColumnSch = aBaseColumn.build();
+        }
+        columns.add(aColumnSch);
+      }
+      org.apache.kudu.Schema sch = new org.apache.kudu.Schema(columns);
+      CreateTableOptions cto = new CreateTableOptions();
+      cto.setNumReplicas(kuduMapping.getNumReplicas());
+      if (kuduMapping.getHashBuckets() > 0) {
+        cto.addHashPartitions(keys, kuduMapping.getHashBuckets());
+      }
+      if (!kuduMapping.getRangePartitions().isEmpty()) {
+        cto.setRangePartitionColumns(keys);
+        for (Map.Entry<String, String> range : 
kuduMapping.getRangePartitions()) {
+          PartialRow lowerPar = sch.newPartialRow();
+          PartialRow upperPar = sch.newPartialRow();
+          for (String ky : keys) {
+            if (!range.getKey().isEmpty()) {
+              lowerPar.addString(ky, range.getKey());
+            }
+            if (!range.getValue().isEmpty()) {
+              upperPar.addString(ky, range.getValue());
+            }
+          }
+          cto.addRangePartition(lowerPar, upperPar);
+        }
+      }
+      table = client.createTable(kuduMapping.getTableName(), sch, cto);
+    } catch (KuduException ex) {
+      throw new GoraException(ex);
+    }
+  }
+
+  @Override
+  public void deleteSchema() throws GoraException {
+    try {
+      client.deleteTable(kuduMapping.getTableName());
+      table = null;
+    } catch (KuduException ex) {
+      throw new GoraException(ex);
+    }
+  }
+
+  @Override
+  public boolean schemaExists() throws GoraException {
+    try {
+      return client.tableExists(kuduMapping.getTableName());
+    } catch (KuduException ex) {
+      throw new GoraException(ex);
+    }
+  }
+
+  @Override
+  public boolean exists(K key) throws GoraException {
+    try {
+      ColumnSchema column = 
table.getSchema().getColumn(kuduMapping.getPrimaryKey().get(0).getName());
 
 Review comment:
   +1

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to