77
loading...
This website collects cookies to deliver better user experience
# Create the file repository configuration:
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
# Import the repository signing key:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
# Update the package lists:
sudo apt-get update
# Install the latest version of PostgreSQL.
# If you want a specific version, use 'postgresql-12' or similar instead of 'postgresql':
sudo apt-get -y install postgresql
postgres
role to create the database or create a database as a new user. Let’s see how to use both.postgres
role, we must switch over to the Postgres account on the server by typing the following:$ sudo -i -u postgres
psql
by typing the following command:$ psql
CREATE DATABASE ecom;
ecom
database.sudo -u postgres createuser testuser
testuser
. You can name yours anything you see fit.sudo -u postgres createdb ecom
ecom
, let’s proceed to grant all privileges to the user we just created. To grant privileges, we must first log into Postgres:sudo -i -u postgres
psql
alter user testuser with encrypted password 'testpassword';
grant all privileges on database ecom to testuser;
psql -h localhost -p 5432 -U testuser -d ecom -W
testpassword
.\q // to exit the psql terminal
sudo -i -u postgres // to login to postgres
curl -L http://cube.dev/downloads/ecom-dump.sql > ecom-dump.sql
psql --dbname ecom -f ecom-dump.sql
ecom
database, run this command to see the user’s relations count in the database:psql // to enter psql command line
\c ecom // to connect or enter your database
SELECT count(*) FROM users;
\d+
to see the list of all relations in our database. And now, we’re done with the backend setup.npx cubejs-cli create <project name> -d <database type>
npx cubejs-cli create analytics -d postgres
analytics
and postgres
as our database..env
file. This allows us connect our Postgres database to Cube.js:CUBEJS_DB_TYPE=postgres
CUBEJS_DB_HOST=localhost
CUBEJS_DB_NAME=ecom
CUBEJS_DB_USER=postgres
CUBEJS_DB_PASS=testpassword
user
, fill it with your unique credentials..env
file in our folder and we must check the options of seeing hidden files before we see the .env
file. We can use the terminal to add the credentials by simply running the following commands:cat .env // to view the file in your terminal
vi .env // to edit the file in your terminal editor
Press :wq! // keys to save the updated file and exit from the editor.
cat .env // to see the updated file
cd
into our folder and run the command below:cd analytics
npm run dev
Order.js
and Products.js
schema files.fuser -k 4000/tcp
npm i --save @cubejs-client/core @cubejs-client/react recharts
Analytics.js
file:import React from "react";
import cubejs from "@cubejs-client/core";
import { QueryRenderer } from "@cubejs-client/react";
import { BarChart, Bar, XAxis, YAxis, Tooltip } from "recharts";
@cubejs-client/core
, @cubejs-client/react
, and recharts
:import React from "react";
import cubejs from "@cubejs-client/core";
import { QueryRenderer } from "@cubejs-client/react";
import { BarChart, Bar, XAxis, YAxis, Tooltip } from "recharts";
const cubejsApi = cubejs(
"4c2a328092cda6a1944499663fcaf730c1622823fb714ee4e60ba4917cd9a773219d98df8aa91817813a9fffe012d8ebe0f507849719606a0722b0a47d08757d",
{ apiUrl: "http://localhost:4000/cubejs-api/v1" }
);
const Analytics = () => {
return (
<QueryRenderer
query={{
measures: ["Orders.count"],
dimensions: ["Products.name"],
timeDimensions: [
{
dimension: "Orders.createdAt",
granularity: "day",
dateRange: "last 3 days"
}
]
}}
cubejsApi={cubejsApi}
render={({ resultSet }) => {
if (!resultSet) {
return "Loading Analytics...";
}
return (
<BarChart width={600} height={300} data={resultSet.rawData()}>
<XAxis dataKey="Products.name" stroke="#8884d8" />
<YAxis />
<Tooltip />
<Bar barSize={30} dataKey="Orders.count" stroke="#8884d8" />
</BarChart>
);
}}
/>
);
};
@cubejs-client/core
allows us to connect to our Cube.js backend, taking two parameters: our Cube.js secret token, which can be found in our .env
file and the API URL, which is the Cube.js default URL in development mode.@cubejs-client/react
allows us to query our Cube.js backend from our functional React component, using the useCubeQuery
Hook to execute this query. We can then pass the dimensions
and measures
we need to the Hook.dimensions
is our quantitative data like the number of units sold, the number of unique visits, or order counts, while measures
is the categorical data like gender, product name, or units of time (like day, week, or month).granularity
query to week
, month
, or year
, or change the dateRange
to last {n} week
, month
, or year
, respectively. We can also change the measures
and dimensions
according to the schemas generated.BarChart
, which is from the Recharts module. We can also use the LineChart
, AreaChart
, or PieChart
that Recharts offers if desired.mongodb
instance and the MongoDB Connector for BI that helps write SQL queries on top of MongoDB.