FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Basic Put support via AnviltopConnection by AngusDavis · Pull Request #23 · googleapis/java-bigtable-hbase · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (8) .xml  (1) All 2 file types selected
Only manifest files
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
9 changes: 9 additions & 0 deletions pom.xml
This file contains hidden or 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
Expand Up @@ -23,6 +23,7 @@
<artifactId>hbase-client</artifactId>
<version>${hbase.version}</version>
</dependency>

<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
Expand Down Expand Up @@ -88,6 +89,14 @@
<type>test-jar</type>
<scope>test</scope>
</dependency>

<dependency>
<groupId>anviltop-client</groupId>
<artifactId>anviltop-client-interface</artifactId>
<version>0.1</version>
<systemPath>/google/data/ro/users/an/angusdavis/public/driver_deploy.jar</systemPath>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I assume this is just a stop-gap. What needs to be in place before we can remove this? The gRPC Java client?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Correct.

<scope>system</scope>
</dependency>
</dependencies>

<build>
Expand Down
This file contains hidden or 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,46 @@
/*
* Copyright (c) 2014 Google Inc.
*
* Licensed 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 com.google.cloud.anviltop.hbase;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;

import org.apache.hadoop.conf.Configuration;

/**
* Static methods to convert an instance of {@link Configuration}
* to a {@link AnviltopOptions} instance.
*/
public class AnvilTopOptionsFactory {
public static final String PROJECT_ID_KEY = "anviltop.project.id";
public static final String API_ENDPOINT_KEY = "anviltop.endpoint.url";

public static AnviltopOptions fromConfiguration(Configuration configuration) {
AnviltopOptions.Builder optionsBuilder = new AnviltopOptions.Builder();

String projectId = configuration.get(PROJECT_ID_KEY);
Preconditions.checkArgument(
!Strings.isNullOrEmpty(projectId),
String.format("Project ID must be supplied via %s", PROJECT_ID_KEY));
optionsBuilder.setProjectId(projectId);

String apiEndpoint = configuration.get(API_ENDPOINT_KEY);
Preconditions.checkArgument(
!Strings.isNullOrEmpty(apiEndpoint),
String.format("API endpoint URL must be supplied via %s", API_ENDPOINT_KEY));
optionsBuilder.setApiEndpoint(apiEndpoint);

return optionsBuilder.build();
}
}
This file contains hidden or 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
Expand Up @@ -13,10 +13,13 @@
*/
package com.google.cloud.anviltop.hbase;

import com.google.cloud.anviltop.hbase.adapters.PutAdapter;
import com.google.cloud.hadoop.hbase.AnviltopClient;
import com.google.protobuf.Descriptors;
import com.google.protobuf.Message;
import com.google.protobuf.Service;
import com.google.protobuf.ServiceException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
Expand All @@ -39,16 +42,27 @@
import java.util.List;
import java.util.Map;

public class AnvilTop implements HTableInterface {
public class AnvilTopTable implements HTableInterface {
protected final TableName tableName;
protected final AnviltopOptions options;
protected final AnviltopClient client;
protected final PutAdapter putAdapter = new PutAdapter();
protected final Configuration configuration;

/**
* Constructed by AnvilTopConnection
*
* @param tableName
* @param client
*/
public AnvilTop(TableName tableName) {
public AnvilTopTable(TableName tableName,
AnviltopOptions options,
Configuration configuration,
AnviltopClient client) {
this.tableName = tableName;
this.options = options;
this.client = client;
this.configuration = configuration;
}

@Override
Expand All @@ -63,7 +77,7 @@ public TableName getName() {

@Override
public Configuration getConfiguration() {
throw new UnsupportedOperationException(); // TODO
return this.configuration;
}

@Override
Expand Down Expand Up @@ -136,7 +150,14 @@ public ResultScanner getScanner(byte[] family, byte[] qualifier) throws IOExcept

@Override
public void put(Put put) throws IOException {
throw new UnsupportedOperationException(); // TODO
try {
client.mutateAtomic(
options.getProjectId(),
tableName.getQualifierAsString(),
putAdapter.adapt(put).build());
} catch (ServiceException e) {
throw new IOException("Failed to put row.", e);
}
}

@Override
Expand Down
This file contains hidden or 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,64 @@
/*
* Copyright (c) 2014 Google Inc.
*
* Licensed 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 com.google.cloud.anviltop.hbase;

/**
* An immutable class providing access to configuration options for Anviltop.
*/
public class AnviltopOptions {

/**
* A mutable builder for AnviltopConnectionOptions.
*/
public static class Builder {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

What's the advantage of including a Builder? I'm trying to see how this would make a developer's life easier.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

private String apiEndpoint = "";
private String projectId = "";

public Builder setApiEndpoint(String apiEndpoint) {
this.apiEndpoint = apiEndpoint;
return this;
}

public Builder setProjectId(String projectId) {
this.projectId = projectId;
return this;
}

public AnviltopOptions build() {
return new AnviltopOptions(this.apiEndpoint, this.projectId);
}
}

private final String apiEndpoint;
private final String projectId;

public AnviltopOptions(String apiEndpoint, String projectId) {
this.apiEndpoint = apiEndpoint;
this.projectId = projectId;
}

/**
* The API endpoint to connect to, including version information.
*/
public String getApiEndpoint() {
return apiEndpoint;
}

/**
* The project ID that table belong to.
*/
public String getProjectId() {
return projectId;
}
}
This file contains hidden or 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,33 @@
/*
* Copyright (c) 2014 Google Inc.
*
* Licensed 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 com.google.cloud.anviltop.hbase.adapters;

import com.google.protobuf.GeneratedMessage.Builder;

import org.apache.hadoop.hbase.client.Operation;

/**
* An interface for adapters that will convert an HBase Operation into an Anviltop
* @param <T> The HBase operation type
* @param <U> The Anviltop message type
*/
public interface OperationAdapter<T extends Operation, U extends Builder> {

/**
* Adapt a single HBase Operation to a single Anviltop generated message.
* @param operation The HBase operation to convert.
* @return An equivalent Anviltop
*/
public U adapt(T operation);
}
This file contains hidden or 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,79 @@
/*
* Copyright (c) 2014 Google Inc.
*
* Licensed 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 com.google.cloud.anviltop.hbase.adapters;

import com.google.bigtable.anviltop.AnviltopData;
import com.google.bigtable.anviltop.AnviltopData.RowMutation;
import com.google.bigtable.anviltop.AnviltopData.RowMutation.Mod;
import com.google.bigtable.anviltop.AnviltopData.RowMutation.Mod.SetCell;
import com.google.common.collect.ImmutableList;
import com.google.protobuf.ByteString;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.client.Put;

import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;

/**
* Adapt an HBase Put Operation into an Anviltop RowMutation
*/
public class PutAdapter implements OperationAdapter<Put, RowMutation.Builder> {

public static final ByteString SEPARATOR_BYTE_STRING = ByteString.copyFromUtf8(":");

@Override
public RowMutation.Builder adapt(Put operation) {
RowMutation.Builder result = AnviltopData.RowMutation.newBuilder();
result.setRowKey(ByteString.copyFrom(operation.getRow()));

for (Entry<byte[], List<Cell>> entry : operation.getFamilyCellMap().entrySet()) {
ByteString familyByteString = ByteString.copyFrom(entry.getKey());

for (Cell cell : entry.getValue()) {
Mod.Builder modBuilder = result.addModsBuilder();
SetCell.Builder setCellBuilder = modBuilder.getSetCellBuilder();

ByteString cellQualifierByteString = ByteString.copyFrom(
cell.getQualifierArray(),
cell.getQualifierOffset(),
cell.getQualifierLength());

setCellBuilder.setColumnName(
ByteString.copyFrom(
ImmutableList.of(
familyByteString,
SEPARATOR_BYTE_STRING,
cellQualifierByteString)));

AnviltopData.Cell.Builder cellBuilder = setCellBuilder.getCellBuilder();

if (cell.getTimestamp() != HConstants.LATEST_TIMESTAMP) {
long timestampMicros = TimeUnit.MILLISECONDS.toMicros(cell.getTimestamp());
cellBuilder.setTimestampMicros(timestampMicros);
}

cellBuilder.setValue(
ByteString.copyFrom(
cell.getValueArray(),
cell.getValueOffset(),
cell.getValueLength()));
}
}

return result;
}
}
Loading

Back | FazBrowse Home | New Git URL