Skip to content

Commit

Permalink
docs: Bigtable fraud example update readme and convert scan to get (#…
Browse files Browse the repository at this point in the history
…7367)

* docs: Bigtable fraud example update readme and convert scan to get

* fix lint line length
  • Loading branch information
billyjacobson authored Oct 28, 2022
1 parent 7879314 commit 16213c7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 31 deletions.
38 changes: 23 additions & 15 deletions bigtable/use-cases/fraudDetection/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,22 +158,30 @@ The ML model is located in the path: **terraform/model**

### Prerequisites

1) [Have a GCP project.](https://cloud.google.com/resource-manager/docs/creating-managing-projects)
2) [Have a service account that contains the following permissions:](https://cloud.google.com/docs/authentication/production)
1) Bigtable Administrator
2) Cloud Dataflow Service Agent
3) Compute Admin
4) Dataflow Admin
5) Dataflow Worker
6) Datapipelines Service Agent
7) Storage Admin
8) Vertex AI User
3) Set these environment variables:
1. [Have a GCP project.](https://cloud.google.com/resource-manager/docs/creating-managing-projects)
2. [Have a service account that contains the following permissions:](https://cloud.google.com/docs/authentication/production)
1. Bigtable Administrator
1. Cloud Dataflow Service Agent
1. Compute Admin
1. Dataflow Admin
1. Dataflow Worker
1. Datapipelines Service Agent
1. Storage Admin
1. Vertex AI User
3. Set these environment variables:
```
export GOOGLE_APPLICATION_CREDENTIALS={YOUR CREDS PATH}
export PROJECT_ID={YOUR PROJECT ID}
```

4. Enable the required APIs:
```
gcloud services enable aiplatform.googleapis.com bigtable.googleapis.com
bigtableadmin.googleapis.com compute.googleapis.com dataflow.googleapis.com
pubsub.googleapis.com storage-api.googleapis.com
storage-component.googleapis.com
```

```
export GOOGLE_APPLICATION_CREDENTIALS={YOUR CREDS PATH}
export PROJECT_ID={YOUR PROJECT ID}
```

### Running steps

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@
import org.apache.beam.sdk.transforms.ParDo;
import org.apache.beam.sdk.values.PCollection;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class FraudDetection {

private FraudDetection() {
}

Expand All @@ -61,9 +61,8 @@ private FraudDetection() {
FraudDetection.class);

/**
* Set the field isFraud to true if the fraud_probability was >= 0.1.
* This is a configurable number that should be tuned depending on
* the ML model.
* Set the field isFraud to true if the fraud_probability was >= 0.1. This is
* a configurable number that should be tuned depending on the ML model.
*/
private static final double FRAUD_PROBABILITY_THRESHOLD = 0.1d;

Expand Down Expand Up @@ -92,8 +91,8 @@ public static class ReadFromTableFn
AbstractCloudBigtableTableDoFn<TransactionDetails, AggregatedData> {

/**
* @param config the CloudBigtableConfiguration used in reading from
* Cloud Bigtable.
* @param config the CloudBigtableConfiguration used in reading from Cloud
* Bigtable.
*/
public ReadFromTableFn(final CloudBigtableConfiguration config) {
super(config);
Expand All @@ -115,20 +114,15 @@ public void processElement(
+ transactionDetails.getCustomerID());

// Read the cells for that customer ID.
Scan scan =
new Scan()
.withStartRow(Bytes.toBytes(transactionDetails.getCustomerID()))
.setOneRowLimit()
.setMaxVersions();
Table table = getConnection().getTable(
TableName.valueOf(options.getCBTTableId()));
ResultScanner data = table.getScanner(scan);
Result row = data.next();
Result row = table.get(
new Get(Bytes.toBytes(transactionDetails.getCustomerID())));

Preconditions.checkArgument(new String(row.getRow()).equals(
transactionDetails.getCustomerID()));

CustomerProfile customerProfile = new CustomerProfile(
row);
CustomerProfile customerProfile = new CustomerProfile(row);

// Generate an AggregatedData object.
AggregatedData aggregatedData =
Expand All @@ -144,6 +138,7 @@ public void processElement(

public static final class QueryMlModelFn
extends DoFn<AggregatedData, RowDetails> {

/**
* The region of the ML model.
*/
Expand Down

0 comments on commit 16213c7

Please sign in to comment.