You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@gobblin.apache.org by GitBox <gi...@apache.org> on 2019/05/13 18:54:14 UTC

[GitHub] [incubator-gobblin] jack-moseley commented on a change in pull request #2631: [GOBBLIN-768] Add MySQL implementation of SpecStore

jack-moseley commented on a change in pull request #2631: [GOBBLIN-768] Add MySQL implementation of SpecStore
URL: https://github.com/apache/incubator-gobblin/pull/2631#discussion_r283487436
 
 

 ##########
 File path: gobblin-runtime/src/main/java/org/apache/gobblin/runtime/spec_store/MysqlSpecStore.java
 ##########
 @@ -0,0 +1,215 @@
+/*
+ * 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.gobblin.runtime.spec_store;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.net.URI;
+import java.sql.Blob;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.Lists;
+import com.typesafe.config.Config;
+
+import javax.sql.DataSource;
+
+import org.apache.gobblin.broker.SharedResourcesBrokerFactory;
+import org.apache.gobblin.configuration.ConfigurationKeys;
+import org.apache.gobblin.metastore.MysqlDataSourceFactory;
+import org.apache.gobblin.runtime.api.Spec;
+import org.apache.gobblin.runtime.api.SpecNotFoundException;
+import org.apache.gobblin.runtime.api.SpecStore;
+
+
+/**
+ * Implementation of {@link SpecStore} that stores specs as serialized java objects in MySQL. Note that versions are not
+ * supported, so the version parameter will be ignored in methods that have it.
+ */
+public class MysqlSpecStore implements SpecStore {
+  private static final String CREATE_TABLE_STATEMENT =
+      "CREATE TABLE IF NOT EXISTS %s (spec_uri VARCHAR(128) NOT NULL, spec LONGBLOB, PRIMARY KEY (spec_uri))";
+  private static final String EXISTS_STATEMENT = "SELECT EXISTS(SELECT * FROM %s WHERE spec_uri = ?)";
+  private static final String INSERT_STATEMENT = "INSERT INTO %s (spec_uri, spec) VALUES (?, ?) ON DUPLICATE KEY UPDATE spec = VALUES(spec)";
+  private static final String DELETE_STATEMENT = "DELETE FROM %s WHERE spec_uri = ?";
+  private static final String GET_STATEMENT = "SELECT spec FROM %s WHERE spec_uri = ?";
+  private static final String GET_ALL_STATEMENT = "SELECT spec_uri, spec FROM %s";
+
+  private DataSource dataSource;
+  private String tableName;
+  private URI specStoreURI;
+
+  public MysqlSpecStore(Config config) throws IOException {
+    this.dataSource = MysqlDataSourceFactory.get(config, SharedResourcesBrokerFactory.getImplicitBroker());
+    this.tableName = config.getString(ConfigurationKeys.STATE_STORE_DB_TABLE_KEY);
+    this.specStoreURI = URI.create(config.getString(ConfigurationKeys.STATE_STORE_DB_URL_KEY));
+    try (Connection connection = this.dataSource.getConnection();
+        PreparedStatement statement = connection.prepareStatement(String.format(CREATE_TABLE_STATEMENT, this.tableName))) {
+      statement.executeUpdate();
+    } catch (SQLException e) {
+      throw new IOException(e);
+    }
+  }
+
+  @Override
+  public boolean exists(URI specUri) throws IOException {
+    try (Connection connection = this.dataSource.getConnection();
+        PreparedStatement statement = connection.prepareStatement(String.format(EXISTS_STATEMENT, this.tableName))) {
+      statement.setString(1, specUri.toString());
+      ResultSet rs = statement.executeQuery();
+      rs.next();
+      return rs.getBoolean(1);
+    } catch (SQLException e) {
+      throw new IOException(e);
+    }
+  }
+
+  @Override
+  public void addSpec(Spec spec) throws IOException {
+    try (Connection connection = this.dataSource.getConnection();
+        PreparedStatement statement = connection.prepareStatement(String.format(INSERT_STATEMENT, this.tableName));
+        ByteArrayOutputStream aos = new ByteArrayOutputStream();
+        ObjectOutputStream oos = new ObjectOutputStream(aos)) {
+
+      oos.writeObject(spec);
 
 Review comment:
   I think @jhsenjaliya is correct though, it's probably better to pass a `SpecSerDe` in the constructor and use it for serializing the same way it's done in `FSSpecStore`.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services