Skip to main content

Google BigTable

Google BigTable is a fully managed, scalable NoSQL database service for large analytical and operational workloads with up to 99.999% availability

Credential configuration

This credential configuration is done with a single JSON that looks like this:

{
"projectId": "PROJECT_ID",
"credentials": {
"type": "service_account",
"project_id": "PROJECT_ID",
"private_key_id": "KEY_ID",
"private_key": "-----BEGIN PRIVATE KEY-----\nPRIVATE_KEY\n-----END PRIVATE KEY-----\n",
"client_email": "SERVICE_ACCOUNT_EMAIL",
"client_id": "CLIENT_ID",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/SERVICE_ACCOUNT_EMAIL"
}
}

The projectId corresponds to the project ID from the Google Developer's Console.

The credentials field of the JSON corresponds to a JSON key of the service account. This key is downloaded as a JSON file when you create it. You only need to paste its content inside credentials field. If you don't have one, create a service account and then, a JSON key for that service account. The service account which you use needs permissions to access BigQuery to work properly.

Here you have an example of a filled credential configuration form in YepCode:

Google BigTable snippets available in editor

note

The title is the triggering text for YepCode to autocomplete the script

Integration

New integration from credential
const googleBigTableClient = yepcode.integration.googleBigTable("credential-slug");
New integration from plain authentication data
const { Bigtable } = require("@google-cloud/bigtable");

const googleBigTableCredentials = {
projectId: "YepCode",
credentials: {
type: "service_account",
project_id: "yepcode",
private_key_id: "XXXXX",
private_key: \`-----BEGIN PRIVATE KEY-----\nx\n-----END PRIVATE KEY-----\`,
client_email: "yepcode@example.org",
client_id: "1234567890",
auth_uri: "https://example.org",
token_uri: "https://example.org",
auth_provider_x509_cert_url: "https://example.org",
client_x509_cert_url: "https://example.org",
}
};

const googleBigTableClient = new Bigtable(googleBigTableCredentials);

Write a row

Simple
const writeSimple = async () => {
try {
const instance = bigtable.instance("instance-id");
const table = instance.table("table-id");

const rowToInsert = {
key: "id#4c410523#20190501",
data: {
stats_summary: {
kind: "human",
nature: "agressive",
constitution: "strong",
},
},
};

await table.insert(rowToInsert);
console.log(`Successfully wrote row ${rowToInsert.key}`);
} catch (error) {
console.error(error);
}
};

writeSimple();
Multiple
const writeMultiple = async () => {
try {
const instance = bigtable.instance("instance-id");
const table = instance.table("table-id");

const rowsToInsert = [
{
key: "id#7x410521#90190501",
data: {
stats_summary: {
kind: "fremen",
nature: "peaceful",
constitution: "strong",
},
},
},
{
key: "id#4c410523#20190501",
data: {
stats_summary: {
kind: "human",
nature: "agressive",
constitution: "strong",
},
},
},
];

await table.insert(rowsToInsert);
console.log(
`Successfully wrote 2 rows: ${rowsToInsert[0].key} and ${rowsToInsert[1].key}`
);
} catch (error) {
console.error(error);
}
};

writeMultiple();
Conditional
const writeConditional = async () => {
try {
const instance = bigtable.instance("instance-id");
const table = instance.table("table-id");
const row = table.row("row-id");

const filter = [
{ column: "stats_summary", value: { nature: "agressive" } },
];

const config = {
onMatch: [
{ method: "insert", data: { stats_summary: { dangerous: true } } },
],
};

await row.filter(filter, config);

console.log("Successfully updated row");
} catch (error) {
console.error(error);
}
};

writeConditional();

Read a row

Simple
const readSimple = async () => {
try {
const instance = bigtable.instance("instance-id");
const table = instance.table("table-id");

const [row] = await table.row("row-id").get();
console.log(row);
} catch (error) {
console.error(error);
}
};

readSimple();
Multiple
const readMultiple = async () => {
try {
const instance = bigtable.instance("instance-id");
const table = instance.table("table-id");

const [rows] = await table.getRows({ keys: ["row-id"] });
rows.forEach((row) => console.log(row.id, row.data));
} catch (error) {
console.error(error);
}
};

readMultiple();
Conditional
const readConditional = async () => {
try {
const instance = bigtable.instance("instance-id");
const table = instance.table("table-id");

const filter = [
{ column: "stats_summary", value: { nature: "agressive" } },
];

const readStream = await table.createReadStream({ filter });
readStream.on("error", (err) => {
console.log(err);
});
readStream.on("data", (row) => {
console.log(row.id, row.data);
});
readStream.on("end", () => {});
} catch (error) {
console.error(error);
}
};

readConditional();