Once you are logged into your Ubuntu 16.04 instance, run the following command to update your base system with the latest available packages.
apt-get update -y
apt-get upgrade
reboot
Next, create a new user account for Hadoop. You can do this by running the following command:
adduser hadoop (and existing password)
Next, you will also need to set up SSH key-based authentication. First, login to Hadoop user with the following command:
su – hadoop
2 – Getting Started Hadoop is written in Java, so you will need to install Java to your server. You can install it by just running the following command:
apt-get install default-jdk -y
Once Java is installed, verify the version of the Java using the following command:
For a project I was reading some benchmarks at GitHub and google, I found an article then I was surprised, its too much interesting result for me, you can check the result below.
> docker run hello-world
docker : Unable to find image 'hello-world:latest' locally
...
latest:
Pulling from library/hello-world
ca4f61b1923c:
Pulling fs layer
ca4f61b1923c:
Download complete
ca4f61b1923c:
Pull complete
Digest: sha256:97ce6fa4b6cdc0790cda65fe7290b74cfebd9fa0c9b8c38e979330d547d22ce1
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
...
5 – List the hello-worldimage that was downloaded from Docker Hub:
> docker image ls
6 – Explore the Docker help pages by running some help commands:
> docker --help
> docker container --help
> docker container ls --help
> docker run --help
Sample Complex Applications
In this section, demonstrated sample the ease and power of Dockerized applications by running something more complex, such as an OS and a webserver.
Pull an image of the Ubuntu OS and run an interactive terminal inside the spawned container: > docker run --interactive --tty ubuntu bash
docker run --interactive --tty ubuntu bash
docker : Unable to find image 'ubuntu:latest' locally
...
latest:
Pulling from library/ubuntu
22dc81ace0ea:
Pulling fs layer
1a8b3c87dba3:
Pulling fs layer
91390a1c435a:
Pulling fs layer
...
Digest: sha256:e348fbbea0e0a0e73ab0370de151e7800684445c509d46195aef73e090a49bd6
Status: Downloaded newer image for ubuntu:latest
You are in the container. At the root # prompt, check the hostname of the container:
root@8aea0acb7423:/# hostname
8aea0acb7423
Notice that the hostname is assigned as the container ID (and is also used in the prompt).
Exit the shell with the exit command (which also stops the container):
List containers with the --all option (because no containers are running).
The hello-world container (randomly named, relaxed_sammet) stopped after displaying its message. The ubuntu container (randomly named, laughing_kowalevski) stopped when you exited the container.
docker container ls --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8aea0acb7423 ubuntu "bash" 2 minutes ago Exited (0) 2 minutes ago laughing_kowalevski
45f77eb48e78 hello-world "/hello" 3 minutes ago Exited (0) 3 minutes ago relaxed_sammet
Pull and run a Dockerized nginx web server that we name, webserver:
docker run --detach --publish 80:80 --name webserver nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
fdd5d7827f33: Pull complete
a3ed95caeb02: Pull complete
716f7a5f3082: Pull complete
7b10f03a0309: Pull complete
Digest: sha256:f6a001272d5d324c4c9f3f183e1b69e9e0ff12debeb7a092730d638c33e0de3e
Status: Downloaded newer image for nginx:latest
dfe13c68b3b86f01951af617df02be4897184cbf7a8b4d5caf1c3c5bd3fc267f
Point your web browser at http://localhost to display the nginx start page. (You don’t need to append :80 because you specified the default HTTP port in the docker command.)
List only your running containers:
docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e788d8e4dfd nginx "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 0.0.0.0:80->80/tcp webserver
Stop the running nginx container by the name we assigned it, webserver:
> docker container stop webserver
Remove all three containers by their names — the latter two names will differ for you:
As a developer working with SQL Server there was a need to import data from the database to Elasticsearch and analyze data in Kibana.
As Elasticsearch is an open-source project built with Java and handles most other open-source projects, documentation on importing data from SQL Server to ES using LogStash.
I’d like to share how to import SQL Server data to Elasticsearch (version 6.2) using LS and verify the result on Kibana.
Assumption
I will skip on installing ELK (ElasticSearch, LogStash, and Kibana) stack as it’s outside the scope of this article. Please refer to installation steps on Elastic download pages.
Here are the steps required to import SQL Server data to Elasticsearch.
Install Java Development Kit (JDK)
Install JDBC Driver for SQL Server
Set CLASSPATH for the driver
Create an Elasticsearch Index to Import Data to
Configure LogStash configuration file
Run LogStash
Verify in Kibana
Step 1 – Install Java SE Development Kit 8
One of the gotchas is that you might install the latest version of JDK, which is version 9 but Elasticsearch documentation requires you to install JDK 8.
At the time of writing, the latest JDK 8 version is 8u162, which can be downloaded here.
Download “JDK8 8u162” and install it on your machine and make sure that “java” is in the PATH variable so that it can be called in any directory within a command line.
As Elasticsearch is built with JDK 8, you can’t use the latest version of JDBC Driver (version 6.2) for SQL Server as it does not support JDK 8.
Step 3 – Set CLASSPATH for the JDBC Driver
We need to set the path so that Java can find the JDBC driver.
? Note: I am working on Windows 10 machine.
1. Go to the directory under which you have installed SQL Server JDBC.
2. Now you need to navigate to find a JAR file named sqljdbc42.jar, which is found under<<JDBC installation folder>>\sqljdbc_4.2\enu\jre8
3. And then copy the full path to the JAR file.
A cool trick on Windows 7/8/10 is that, when shift+right click on a file, it gives you a “Copy as Path” option.
4. Go to Windows Start button and type “environment” and click on “Edit the system environment variables”.
5. Add a CLASSPATH environment variable with following values (if you don’t already have one).
“.” – for the current directory to search.
And the JAR file path copied in previously (e.g. “C:\talih\Java\MicrosoftJDBCDriversSQLServer\sqljdbc_4.2\enu\jre8\sqljdbc42.jar”).
Gotcha: If you have a space in the path for JDBC JAR file, make sure to put double quotes around it.
Not doing so will result in either of following error messages when you start LogStash service in later step.
c:\talih\elasticco\logstash-6.2.2>bin\logstash -f sql.conf
Error: Could not find or load main class JDBC
- Or -
c:\talih\elasticco\logstash-6.2.2>bin\logstash -f sql.conf
Error: Could not find or load main class File\Microsoft
Let’s now move onto to create an Elasticsearch index to import data to.
Step 4 – Create an Elasticsearch Index to Import Data to
You can use cURL or Postman to create an Index but I will use Kibana console to create an index named “cs_users”, which is equivalent to a database in relational database terminology.
Before we start the Kibana service, we need to start Elasticsearch so that Kibana would not whine about Elasticsearch not being present.
Kibana warnings on lines 12~21 due to Elasticsearch being unavailable
Go to the Elasticsearch installation and start the service.
on the left panel of the Kibana Dev Tools Console.
Create a new Elasticsearch index named “cs_users”
I won’t go into details on “shards” and “replicas” since it’s outside the scope of this article. For more information on the syntax, refer to the official Elasticsearch documentation.
And you will see the response from Elasticsearch with index creation confirmation on the panel right.
A new index “cs_users” is created on Elasticsearch successfully
OK, now we are finally ready to move onto creating a configuration file for LogStash to actually import data.
Step 5 – Configure LogStash configuration file
Go to the LogStash installation folder and create a file named “sql.conf” (name doesn’t really matter). Here is the LogStash configuration I will be using.
Elasticsearch will connect to the server named “cc” running on port 1433 to connect to a database named “StackExchangeCS” with integrated security authentication method.
jdbc_driver_class – This is the driver class contained within the JDBC JAR file. The JDBC JAR file contains a driver of type “com.microsoft.sqlserver.jdbc.SQLServerDriver” according to the documentation.
If you have an inquisitive mind, you can confirm it by opening the JAR file with your choice of ZIP program as JAR is a simple ZIP file.
Unzip JAR to verify JDBC driver name
jdbc_user – If you are using “Integrated Security” as an authentication option, this can be any string (I just entered “xxx” since that’s the easiest thing I can type ?).
SQL Server data (all cs.stackexchange.com users) will be sent to Elasticsearch running on the local machine port 9200 and will be indexed under “cs_users” index created in “Step 4 – Create an Elasticsearch Index to Import Data to”. There are quite a bit of Elasticsearch configuration options so please refer to the official LogStash documentation for more “elasticsearch” output plugin options.
Step 6 – Import Data with LogStash
With prerequisites out of the way, we are now ready to import data to Elasticsearch from SQL Server. Go to the LogStash installation location under which you should have created “sql.conf” and run LogStash service.
bin\logstash -f sql.conf
-f flag specifies the configuration file to use. In our case, “sql.conf” we created in the previous step.
The result of successful LogStash run will look similar to the following output.
Step 7 – Verify in Kibana
Wow, we have finally imported data. Now let’s do a quick check whether the number of records in the database matches the records in Elasticsearch.
Verifying result of data import
“User” table in the SQL Server has 59394 records and Elasticsearch returns the same number as well. ? Note: You can use following command to get the list of all records in “cs_users” index.
Elasticsearch can be downloaded directly from elastic.co in zip, tar.gz, deb, or rpm packages. For Ubuntu, it’s best to use the deb (Debian) package which will install everything you need to run Elasticsearch.
First, update your package index.
sudo apt-get update
Download the latest Elasticsearch version, which is 2.3.1 at the time of writing.
Then install it in the usual Ubuntu way with dpkg.
sudo dpkg -i elasticsearch-2.3.1.deb
This results in Elasticsearch being installed in /usr/share/elasticsearch/ with its configuration files placed in /etc/elasticsearch and its init script added in /etc/init.d/elasticsearch.
To make sure Elasticsearch starts and stops automatically with the server, add its init script to the default runlevels.
sudo systemctl enable elasticsearch.service
Before starting Elasticsearch for the first time, please check the next section about the recommended minimum configuration.
Step 2 — Configuring Elasticsearch
Now that Elasticsearch and its Java dependencies have been installed, it is time to configure Elasticsearch. The Elasticsearch configuration files are in the /etc/elasticsearch directory. There are two files:
elasticsearch.yml configures the Elasticsearch server settings. This is where all options, except those for logging, are stored, which is why we are mostly interested in this file.
logging.yml provides configuration for logging. In the beginning, you don’t have to edit this file. You can leave all default logging options. You can find the resulting logs in /var/log/elasticsearch by default.
The first variables to customize on any Elasticsearch server are node.name and cluster.name in elasticsearch.yml. As their names suggest, node.name specifies the name of the server (node) and the cluster to which the latter is associated.
If you don’t customize these variable, a node.name will be assigned automatically in respect to the Droplet hostname. The cluster.name will be automatically set to the name of the default cluster.
The cluster.name value is used by the auto-discovery feature of Elasticsearch to automatically discover and associate Elasticsearch nodes to a cluster. Thus, if you don’t change the default value, you might have unwanted nodes, found on the same network, in your cluster.
To start editing the main elasticsearch.yml configuration file with nano or your favorite text editor.
sudo nano /etc/elasticsearch/elasticsearch.yml
Remove the # character at the beginning of the lines for cluster.name and node.name to uncomment them, and then update their values. Your first configuration changes in the /etc/elasticsearch/elasticsearch.yml file should look like this:
/etc/elasticsearch/elasticsearch.yml
. . .
cluster.name: mycluster1
node.name: "My First Node"
. . .
These the minimum settings you can start with using Elasticsearch. However, it’s recommended to continue reading the configuration part for more thorough understanding and fine-tuning of Elasticsearch.
One especially important setting of Elasticsearch is the role of the server, which is either master or slave. Master servers are responsible for the cluster health and stability. In large deployments with a lot of cluster nodes, it’s recommended to have more than one dedicated master. Typically, a dedicated master will not store data or create indexes. Thus, there should be no chance of being overloaded, by which the cluster health could be endangered.
Slave servers are used as workhorses which can be loaded with data tasks. Even if a slave node is overloaded, the cluster health shouldn’t be affected seriously, provided there are other nodes to take additional load.
The setting which determines the role of the server is called node.master. By default, a node is a master. If you have only one Elasticsearch node, you should leave this option to the default true value because at least one master is always needed. Alternatively, if you wish to configure the node as a slave, assign a false value to the variable node.master like this:/etc/elasticsearch/elasticsearch.yml
. . .
node.master: false
. . .
Another important configuration option is node.data, which determines whether a node will store data or not. In most cases this option should be left to its default value (true), but there are two cases in which you might wish not to store data on a node. One is when the node is a dedicated master” as previously mentioned. The other is when a node is used only for fetching data from nodes and aggregating results. In the latter case the node will act up as a search load balancer.
Again, if you have only one Elasticsearch node, you should not change this value. Otherwise, to disable storing data locally, specify node.data as false like this:/etc/elasticsearch/elasticsearch.yml
. . .
node.data: false
. . .
In larger Elasticsearch deployments with many nodes, two other important options are index.number_of_shards and index.number_of_replicas. The first determines how many pieces, or shards, the index will be split into. The second defines the number of replicas which will be distributed across the cluster. Having more shards improves the indexing performance, while having more replicas makes searching faster.
By default, the number of shards is 5 and the number of replicas is 1. Assuming that you are still exploring and testing Elasticsearch on a single node, you can start with only one shard and no replicas. Thus, their values should be set like this:/etc/elasticsearch/elasticsearch.yml
One final setting which you might be interested in changing is path.data, which determines the path where data is stored. The default path is /var/lib/elasticsearch. In a production environment, it’s recommended that you use a dedicated partition and mount point for storing Elasticsearch data. In the best case, this dedicated partition will be a separate storage media which will provide better performance and data isolation. You can specify a different path.data path by specifying it like this:/etc/elasticsearch/elasticsearch.yml
. . .
path.data: /media/different_media
. . .
Once you make all the changes, save and exit the file. Now you can start Elasticsearch for the first time.
sudo systemctl start elasticsearch
Give Elasticsearch a few to fully start before you try to use it. Otherwise, you may get errors about not being able to connect.
Step 3 — Securing Elasticsearch
By default, Elasticsearch has no built-in security and can be controlled by anyone who can access the HTTP API. This is not always a security risk because Elasticsearch listens only on the loopback interface (i.e., 127.0.0.1) which can be accessed only locally. Thus, no public access is possible and your Elasticsearch is secure enough as long as all server users are trusted or this is a dedicated Elasticsearch server.
Still, if you wish to harden the security, the first thing to do is to enable authentication. Authentication is provided by the commercial Shield plugin. Unfortunately, this plugin is not free but there is a free 30 day trial you can use to test it. Its official page has excellent installation and configuration instructions. The only thing you may need to know in addition is that the path to the Elasticsearch plugin installation manager is /usr/share/elasticsearch/bin/plugin.
If you don’t want to use the commercial plugin but you still have to allow remote access to the HTTP API, you can at least limit the network exposure with Ubuntu’s default firewall, UFW (Uncomplicated Firewall). By default, UFW is installed but not enabled. If you decide to use it, follow these steps:
First, create a rule to allow any needed services. You will need at least SSH allowed so that you can log in the server. To allow world-wide access to SSH, whitelist port 22.
sudo ufw allow 22
Then allow access to the default Elasticsearch HTTP API port (TCP 9200) for the trusted remote host, e.g.TRUSTED_IP, like this:
sudo ufw allow from TRUSTED_IP to any port 9200
Only after that enable UFW with the command:
sudo ufw enable
Finally, check the status of UFW with the following command:
sudo ufw status
If you have specified the rules correctly, the output should look like this:
Output of java -versionStatus: active
To Action From
-- ------ ----
9200 ALLOW TRUSTED_IP
22 ALLOW Anywhere
22 (v6) ALLOW Anywhere (v6)
Once you have confirmed UFW is enabled and protecting Elasticsearch port 9200, then you can allow Elasticsearch to listen for external connections. To do this, open the elasticsearch.yml configuration file again.
sudo nano /etc/elasticsearch/elasticsearch.yml
Find the line that contains network.bind_host, uncomment it by removing the # character at the beginning of the line, and change the value to 0.0.0.0 so it looks like this:/etc/elasticsearch/elasticsearch.yml
. . .
network.host: 0.0.0.0
. . .
We have specified 0.0.0.0 so that Elasticsearch listens on all interfaces and bound IPs. If you want it to listen only on a specific interface, you can specify its IP in place of 0.0.0.0.
To make the above setting take effect, restart Elasticsearch with the command:
sudo systemctl restart elasticsearch
After that try to connect from the trusted host to Elasticsearch. If you cannot connect, make sure that the UFW is working and the network.host variable has been correctly specified.
Step 4 — Testing Elasticsearch
By now, Elasticsearch should be running on port 9200. You can test it with curl, the command line client-side URL transfers tool and a simple GET request.
curl -X GET 'http://localhost:9200'
You should see the following response:
Output of curl{
"name" : "My First Node",
"cluster_name" : "mycluster1",
"version" : {
"number" : "2.3.1",
"build_hash" : "bd980929010aef404e7cb0843e61d0665269fc39",
"build_timestamp" : "2016-04-04T12:25:05Z",
"build_snapshot" : false,
"lucene_version" : "5.5.0"
},
"tagline" : "You Know, for Search"
}
If you see a response similar to the one above, Elasticsearch is working properly. If not, make sure that you have followed correctly the installation instructions and you have allowed some time for Elasticsearch to fully start.
To perform a more thorough check of Elasticsearch execute the following command:
curl -XGET 'http://localhost:9200/_nodes?pretty'
In the output from the above command you can see and verify all the current settings for the node, cluster, application paths, modules, etc.
Step 5 — Using Elasticsearch
To start using Elasticsearch, let’s add some data first. As already mentioned, Elasticsearch uses a RESTful API, which responds to the usual CRUD commands: create, read, update, and delete. For working with it, we’ll use again curl.
You can add your first entry with the command:
curl -X POST 'http://localhost:9200/tutorial/helloworld/1' -d '{ "message": "Hello World!" }'
In the above example we have modified the message of the first entry to “Hello People!”. With that, the version number has been automatically increased to 2.
You may have noticed the extra argument pretty in the above request. It enables human readable format so that you can write each data field on a new row. You can also “prettify” your results when retrieving data and get much nicer output like this:
curl -X GET 'http://localhost:9200/tutorial/helloworld/1?pretty'
So far we have added to and queried data in Elasticsearch. To learn about the other operations please check the API documentation.
Last Step – Get Data from SQL to Elastich With Nifi
Conclusion
That’s how easy it is to install, configure, and begin using Elasticsearch. Once you have played enough with manual queries, your next task will be to start using it from your applications.
1 – Verify the JDK installation on your machine. Open the shell/terminal and type java -version and javac -version.
2 – Download Scala Binaries from http://www.scala-lang.org/download/. As of writing this post Scala version is 2.11.6, so you should be getting downloaded file as scala-2.11.6.tgz. Unzip the scala-2.11.6.tgz file using the following command as shown below.
3 – tar -xvzf scala-2.11.6.tgz
4 – After unzipping, change the path to point to the directory using cd command as shown below.
5 – For instance my directory is Downloads in which Scala binaries are unzipped.
6 – Now we are in the downloads directory where Scala binaries are present. Just go to the bin directory.
7 – cd scala-2.11.6 / cd bin
8 – This is the Scala REPL shell in which we can type programs and see the outcome right in the shell.
Scala Hello World Example
class Student() {
var id:Int = 0
var age:Int = 0
def studentDetails(i:Int,a:Int) {
id = i
age = a
println(“Student Id is :”+id);
println(“Student Age is :”+age);
}
}
Output: defined class Student
Here we create a Student class and print the student details in the studentDetails method by passing student id and age as parameter. If there are no errors in the code then a message “defined class Student” is displayed.
Create the student object and invoke the studdetails method by passing the student id and age.
object Stud {
def main(args:Array[String]) {
val stu = new Student();
stu.studentDetails(10,8);
}
}
At the highest level, a Pulsar instance is composed of one or more Pulsar clusters. Clusters within an instance can replicate data amongst themselves.
The diagram below provides an illustration of a Pulsar cluster:
Pulsar Comparison With Apache Kafka
The table below lists the similarities and differences between Apache Pulsar and Apache Kafka:
KAFKA
PULSAR
Concepts
Producer-topic-consumer group-consumer
Producer-topic-subscription-consumer
Consumption
More focused on streaming, exclusive messaging on partitions. No shared consumption.
Unified messaging model and API.
Streaming via exclusive, failover subscription
Queuing via shared subscription
Acking
Simple offset management
Prior to Kafka 0.8, offsets are stored in ZooKeeper
After Kafka 0.8, offsets are stored on offset topics
Unified messaging model and API.
Streaming via exclusive, failover subscription
Queuing via shared subscription
Retention
Messages are deleted based on retention. If a consumer doesn’t read messages before the retention period, it will lose data.
Messages are only deleted after all subscriptions consumed them. No data loss even the consumers of a subscription are down for a long time.Messages are allowed to keep for a configured retention period time even after all subscriptions consume them.
TTL
No TTL support
Supports message TTL
Conclusion
Apache Pulsar is an effort undergoing incubation at The Apache Software Foundation (ASF) sponsored by the Apache Incubator PMC. It seems that it will be a competitive alternative to Apache Kafka due to its unique features.
As also you know about Redshift is too much fast when you have to read or select some data, but of course you have to do something for like theoretical white paper performance. Even if you’ve carefully planned out your schema, sortkeys, distkeys and compression encodings, your Redshift queries may still be awfully slow if you have long-running vacuums taking place in the background.
The number one enemy for query performance is the vacuum—it can slow down your ETL jobs and analytical queries by as much as 80%. It is an I/O intensive process that sorts the table, reclaims unused disk space, and impacts all other I/O bound processes (such as queries against large tables). This guide can help you cut down the time it takes to vacuum your cluster (these steps lowered our vacuum time from 10–30 hours to less than 1 hour).
This guide assumes you’ve chosen sortkeys and distkeys for your table, and are vacuuming regularly. If you are not doing these things, use this guide and this guide to get them set up (the flow charts are quite helpful).
Contents
0- What is the Vacuum?
1 – Insert Data in Sortkey Order (for Tables that are Updated Regularly)
2 – Use Compression Encodings (for Large Tables)
3 – Deep Copy Instead Of Vacuuming (When the Unsorted Section is Large)
4 – Call ANALYZE After Vacuuming
5 – VACUUM to 99% on Large Tables
6 – Keep Your Tables Skinny
The vacuum is a process that carries out one or both of the following two steps: sorting tables and reclaiming unused disk blocks. Lets talk about sorting,
VACUUM SORT ONLY;
The first time you insert data into the table, it will land sorted according to its sortkey (if one exists), and this data will make up the “sorted” section of the table. Note the unsorted percentage on the newly populated table below.
COPY INTO my_table FROM s3://my-bucket/csv;
SELECT "table", unsorted FROM svv_table_info;
Subsequent inserts are appended to a completely different section on disk called the “unsorted” section of the table. Calling VACUUM SORT ONLY initiates two processes,
a sorting of the unsorted section,
a merging of the sorted and unsorted sections;
both of these steps can be costly, but there are simple ways to cut down that cost, which we’ll discuss below.
Now onto deleting,
VACUUM DELETE ONLY;
If you called DELETE on any rows from your table since the last vacuum, they were merely marked for deletion. A vacuum operation is necessary to actually reclaim that disk space.
These two steps, sorting tables and reclaiming disk space, can be run together efficiently,
VACUUM FULL;
This command simply runs both a sort only and a delete only operation, but there are advantages to doing them concurrently. If you have deleted andinserted new data, always do a “full” vacuum. It will be faster than a manual vacuum sort only followed by a manual vacuum delete only.
Now that we have described the vacuum, lets talk about how to make it faster. I’ll describe each tip, then describe why it matters.
1 -Insert Data in Sortkey Order (for Tables that are Updated Regularly)
If you have a monotonically increasing sortkey like date, timestamp or auto-incrementing id, make that the first column of your (compound) sortkey. This will cause your inserts to conform to your sortkey configuration, and drastically reduce the merging Redshift needs to do when the vacuum is invoked. If you do one thing in this guide, do this.
Why?
If the unsorted section fully belongs at the end of the sorted sectionalready(say, because time is an arrow, and you’re sorting by timestamp), then the merge step is over almost immediately.
Meanwhile, if you have two sorted sections, and you wish to merge them, but the sort order is interleaved between the two tables (say, because you’re sorting by customer), you will likely have to rewrite the entire table. This will cost you dearly!
Furthermore, if in general if you do queries like,
SELECT
AGGREGATE(column)
FROM my_table
WHERE date = '2018–01–01'
AND action = 'message_clicked'
AND customer = 'taco-town';
then a compound key, sorted by date first, will be both performant in terms of query speed and in terms of vacuum time. You may also consider sorting by customer or action, but these must be subsequent keys in the sortkey, not the first. It will be difficult to optimize your sortkey selection for every query pattern your cluster might see, but you can target and optimize the most likely patterns. Furthermore, by avoiding long vacuums, you are in effect improving query performance.
2- Use Compression Encodings (for Large Tables)
Compression encodings will give you 2–4x compression on disk. Almost always use Zstandard encoding. But you may use the following command to get compression encoding recommendations on a column-by-column basis,
ANALYZE COMPRESSION my_table;
This command will lock the table for the duration of the analysis, so often you need to take a small copy of your table and run the analysis on it separately.
CREATE TABLE my_table_tmp (LIKE my_table);
INSERT INTO my_table_tmp (
-- Generate a pseudo-random filter of ~100,000 rows.
-- This works for a table with ~10e9 rows.
SELECT * FROM my_table
WHERE ABS(STRTOL(LEFT(MD5('seed' || id), 15), 16)) < POW(2, 59)
);
-- Recreate my_table with these recommendations.
ANALYZE COMPRESSION my_table_tmp;
COPY INTO my_table FROM s3://bucket COMPUPDATE ON;
If your tables are small enough to fit into memory without compression, then do not bother encoding them. If your tables are very small, and very low read latency is a requirement, get them out of Redshift altogether.
Why?
The smaller your data, the more data you can fit into memory, the faster your queries will be. So compression helps in both keeping disk space down and reducing the I/O cost of querying against tables that are much larger than memory.
For small tables, the calculus changes. We generally accept a small decompression cost over an I/O cost, but when there is no I/O cost because the table is small, then the decompression cost makes up a significant portion of the total query cost and is no longer worth it. Cutting down on disk space usage frees up the overhead to do deep copies if necessary (see point 3).
3- Deep Copy Instead Of Vacuuming (When the Unsorted Section is Large)
If for some reason your table ends up at more than 20% unsorted, you may be better off copying it than vacuuming it. Bear in mind that Redshift will require 2–3x the table size in free disk space to complete the copy.
Why?
On the first insert to an empty table, Redshift will sort the data according to the sortkey, on subsequent inserts it will not. So a deep copy is identical to a vacuum in this way (as long as the copy takes place in one step). It will likely complete much faster as well (and tie up less resources), but you may not have the 2–3x disk space overhead to complete the copy operation. That is why you should be using appropriate compression encodings (see point 2).
Your deep copy code:
BEGIN;
CREATE TABLE my_table_tmp (LIKE my_table);
INSERT INTO my_table_tmp (SELECT * FROM my_table);
DROP TABLE my_table;
ALTER TABLE my_table_tmp RENAME TO my_table;
COMMIT;
4-Call ANALYZE After Vacuuming
This is basic, but it gets left out. Call ANALYZE to update the query planner after you vacuum. The vacuum may have significantly reorganized the table, and you should update the planner stats. This can create a performance increase for reads, and the analyze process itself is typically quite fast.
5- VACUUM to 99% on Large Tables
Push the vacuum to 99% if you have daily insert volume less than 5% of the existing table. The syntax for doing so is,
VACUUM FULL my_table TO 99;
You must specify a table in order to use the TO clause. Therefore, you probably have to write code like this:
for table in tables:
cursor.execute('VACUUM FULL {} TO 99;'.format(table))
Why?
This one may seem counterintuitive. Many teams might clean up their redshift cluster by calling VACUUM FULL. This conveniently vacuums every table in the cluster. But, if a table’s unsorted percentage is less than 5%, Redshift skips the vacuum on that table. This process continues for every vacuum call until the table finally tops 5% unsorted, at which point the sorting will take place.
This is fine if the table is small, and resorting 5% of the table is a modest job. But if the table is very large, resorting and merging 5% of the table may be a significant time cost (it was for us).
Vacuuming more thoroughly on each call spreads the vacuum cost evenly across the events, instead of saving up unsorted rows, then running long vacuums to catch up.
You may wonder if this causes more total vacuum time. The answer is no, if you are following step 1, and inserting in sortkey order. The vacuum call amounts to a sorting of the unsorted section and a quick merge step. Sorting 5% of the table will take 5x the time that sorting 1% of the table does, and the merge step will always be fast if you are inserting new data in sortkey order.
Note: the Most important thing is when etl works kind of insert delete etch, Redshift stored data if you didn’t vacuum etc. So on performance and prices is going up. Keep clean your tables after every execution or with daily crons.