-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Connection module in Java/Scala API (#6)
- Loading branch information
Showing
23 changed files
with
862 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
java/src/main/java/com/logicalclocks/featurestore/FsQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.logicalclocks.featurestore; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class FsQuery { | ||
@Getter @Setter | ||
private String query; | ||
} |
112 changes: 112 additions & 0 deletions
112
java/src/main/java/com/logicalclocks/featurestore/HopsworksConnection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package com.logicalclocks.featurestore; | ||
|
||
import com.google.common.base.Strings; | ||
import com.logicalclocks.featurestore.metadata.FeatureStoreApi; | ||
import com.logicalclocks.featurestore.metadata.HopsworksClient; | ||
import com.logicalclocks.featurestore.metadata.ProjectApi; | ||
import com.logicalclocks.featurestore.util.Constants; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import software.amazon.awssdk.regions.Region; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
|
||
public class HopsworksConnection implements Closeable { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(HopsworksConnection.class); | ||
|
||
@Getter | ||
private String host; | ||
|
||
@Getter | ||
private int port; | ||
|
||
@Getter | ||
private String project; | ||
|
||
@Getter | ||
private Region region; | ||
|
||
@Getter | ||
private SecretStore secretStore; | ||
|
||
@Getter | ||
private boolean hostnameVerification; | ||
|
||
@Getter | ||
private String trustStorePath; | ||
|
||
@Getter | ||
private String certPath; | ||
|
||
@Getter | ||
private String APIKeyFilePath; | ||
|
||
|
||
private FeatureStoreApi featureStoreApi = new FeatureStoreApi(); | ||
private ProjectApi projectApi = new ProjectApi(); | ||
|
||
private Project projectObj; | ||
|
||
@Builder | ||
public HopsworksConnection(String host, int port, String project, Region region, SecretStore secretStore, | ||
boolean hostnameVerification, String trustStorePath, | ||
String certPath, String APIKeyFilePath) throws IOException, FeatureStoreException { | ||
this.host = host; | ||
this.port = port; | ||
this.project = project; | ||
this.region = region; | ||
this.secretStore = secretStore; | ||
this.hostnameVerification = hostnameVerification; | ||
this.trustStorePath = trustStorePath; | ||
this.certPath = certPath; | ||
this.APIKeyFilePath = APIKeyFilePath; | ||
|
||
HopsworksClient hopsworksClient = HopsworksClient.setupHopsworksClient(host, port, region, secretStore, | ||
hostnameVerification, trustStorePath, APIKeyFilePath); | ||
projectObj = getProject(); | ||
hopsworksClient.downloadCredentials(projectObj, certPath); | ||
} | ||
|
||
/** | ||
* Retrieve the project feature store | ||
* @return | ||
* @throws IOException | ||
* @throws FeatureStoreException | ||
*/ | ||
public FeatureStore getFeatureStore() throws IOException, FeatureStoreException{ | ||
return getFeatureStore(project + Constants.FEATURESTORE_SUFFIX); | ||
} | ||
|
||
/** | ||
* Retrieve a feature store based on name. The feature store needs to be shared with | ||
* the connection's project. | ||
* @param name | ||
* @return | ||
* @throws IOException | ||
* @throws FeatureStoreException | ||
*/ | ||
public FeatureStore getFeatureStore(String name) throws IOException, FeatureStoreException { | ||
return featureStoreApi.get(projectObj.getProjectId(), name); | ||
} | ||
|
||
/** | ||
* Close the connection and clean up the certificates. | ||
*/ | ||
public void close() { | ||
// Close the client | ||
} | ||
|
||
private Project getProject() throws IOException, FeatureStoreException { | ||
if (Strings.isNullOrEmpty(project)) { | ||
// User didn't specify a project in the connection construction. Assume they are running | ||
// from within Hopsworks and the project name is available a system property | ||
project = System.getProperty(Constants.PROJECTNAME_ENV); | ||
} | ||
LOGGER.info("Getting information for project name: " + project); | ||
return projectApi.get(project); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
java/src/main/java/com/logicalclocks/featurestore/Project.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.logicalclocks.featurestore; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@NoArgsConstructor | ||
public class Project { | ||
|
||
@Getter @Setter | ||
private Integer projectId; | ||
@Getter @Setter | ||
private String projectName; | ||
@Getter @Setter | ||
private String owner; | ||
|
||
public Project(Integer projectId) { | ||
this.projectId = projectId; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
java/src/main/java/com/logicalclocks/featurestore/SecretStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.logicalclocks.featurestore; | ||
|
||
public enum SecretStore { | ||
PARAMETER_STORE, | ||
SECRET_MANAGER | ||
} |
34 changes: 34 additions & 0 deletions
34
java/src/main/java/com/logicalclocks/featurestore/metadata/AuthorizationHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.logicalclocks.featurestore.metadata; | ||
|
||
import org.apache.http.HttpResponse; | ||
import org.apache.http.HttpStatus; | ||
import org.apache.http.client.ClientProtocolException; | ||
import org.apache.http.client.ResponseHandler; | ||
import org.apache.http.util.EntityUtils; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
|
||
public class AuthorizationHandler<T> implements ResponseHandler<T> { | ||
|
||
private ResponseHandler<T> originalResponseHandler; | ||
|
||
AuthorizationHandler(ResponseHandler<T> originalResponseHandler) { | ||
this.originalResponseHandler = originalResponseHandler; | ||
} | ||
|
||
@Override | ||
public T handleResponse(HttpResponse response) throws ClientProtocolException, IOException { | ||
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) { | ||
throw new UnauthorizedException(); | ||
} else if (response.getStatusLine().getStatusCode() / 100 == 4) { | ||
throw new IOException("Error: " + response.getStatusLine().getStatusCode() + | ||
EntityUtils.toString(response.getEntity(), Charset.defaultCharset())); | ||
} else if (response.getStatusLine().getStatusCode() / 100 == 5) { | ||
// TODO(fabio): Propagate http error upstream | ||
throw new InternalException(); | ||
} | ||
|
||
return originalResponseHandler.handleResponse(response); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
java/src/main/java/com/logicalclocks/featurestore/metadata/Credentials.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.logicalclocks.featurestore.metadata; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class Credentials { | ||
|
||
@Getter @Setter | ||
private String kStore; | ||
@Getter @Setter | ||
private String tStore; | ||
@Getter @Setter | ||
private String password; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
java/src/main/java/com/logicalclocks/featurestore/metadata/FeatureStoreApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,33 @@ | ||
package com.logicalclocks.featurestore.metadata; | ||
|
||
import com.damnhandy.uri.template.UriTemplate; | ||
import com.logicalclocks.featurestore.FeatureStore; | ||
import com.logicalclocks.featurestore.FeatureStoreException; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
|
||
public class FeatureStoreApi { | ||
|
||
public static final String FEATURE_STORE_SERVICE_PATH = "/featurestores"; | ||
public static final String FEATURE_STORE_PATH = FEATURE_STORE_SERVICE_PATH + "{/fsId}"; | ||
public static final String FEATURE_STORE_NAME_PATH = FEATURE_STORE_SERVICE_PATH + "{/fsName}"; | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(FeatureStoreApi.class); | ||
|
||
public FeatureStore get(int projectId, String name) throws IOException, FeatureStoreException { | ||
HopsworksClient hopsworksClient = HopsworksClient.getInstance(); | ||
String pathTemplate = HopsworksClient.PROJECT_PATH | ||
+ FEATURE_STORE_NAME_PATH; | ||
|
||
String uri = UriTemplate.fromTemplate(pathTemplate) | ||
.set("projectId", projectId) | ||
.set("fsName", name) | ||
.expand(); | ||
|
||
LOGGER.info("Sending metadata request: " + uri); | ||
return hopsworksClient.handleRequest(new HttpGet(uri), FeatureStore.class); | ||
} | ||
} |
Oops, something went wrong.