Skip to content

Instantly share code, notes, and snippets.

View piyusht007's full-sized avatar
🏠
Working from home

Piyush Tiwari piyusht007

🏠
Working from home
View GitHub Profile
@piyusht007
piyusht007 / BaseQueryValidator.java
Last active July 9, 2024 10:58
SQL parsing in java using Apache-Calcite SQL parser.
public class BaseQueryValidator {
private static List<String> extractTableAliases(SqlNode node) {
final List<String> tables = new ArrayList<>();
// If order by comes in the query.
if (node.getKind().equals(SqlKind.ORDER_BY)) {
// Retrieve exact select.
node = ((SqlSelect) ((SqlOrderBy) node).query).getFrom();
} else {
node = ((SqlSelect) node).getFrom();
@piyusht007
piyusht007 / AppConfig.java
Last active May 13, 2022 16:54
Scheduling a periodic task using ScheduledExecutorService.
@Configuration
public class AppConfig {
@Bean
public ScheduledExecutorService getScheduledExecutorService() {
return Executors.newSingleThreadScheduledExecutor();
}
}
@piyusht007
piyusht007 / PresignedURLGenerator.java
Created March 28, 2018 11:41
Generating Amazon S3 Pre-signed URLs with Server Side Encryption - KMS
public class PresignedLinkGenerator {
private static final int EXPIRATION_TIME_IN_MILLISECONDS = 1000 * 60 * 60; // 1 hour
private static final String SIGNER_TYPE = "AWSS3V4SignerType";
private String KmsKeyARN = "<EncryptionKeyARN>";
public URL generatePresignedLink(final String bucketName, final String objectKey, final HttpMethod httpMethod) throws Exception {
final Region currentRegion = Regions.getCurrentRegion() == null ? Region.getRegion(Regions.US_WEST_2) : Regions.getCurrentRegion();
final AmazonS3 s3client = AmazonS3ClientBuilder.standard()
.withClientConfiguration(new ClientConfiguration().withSignerOverride(SIGNER_TYPE))
.withRegion(currentRegion.getName())
@piyusht007
piyusht007 / S3FileWriter.java
Created March 28, 2018 12:01
Writing a file to S3 bucket using pre-signed URL
public S3FileWriter {
public String write(final ResultSet resultSet) {
HttpURLConnection connection = null;
try {
StringBuilder csvContent = new StringBuilder();
connection = httpURLConnectionHelper.newHttpURLConnection("<PRESIGNED_PUT_URL_FOR_FILE>");
try (OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
BufferedWriter writer = new BufferedWriter(out)
@piyusht007
piyusht007 / generate-dates-for-last-month.sh
Created May 3, 2018 08:52
Shell Scripting - Generate date string values
# This script generate comma separated dates for last month in yyyy-mm-dd format.
# Ex:
# "2018-04-01,2018-04-02,2018-04-03,2018-04-04,2018-04-05,2018-04-06,2018-04-07,
# 2018-04-08,2018-04-09,2018-04-10,2018-04-11,2018-04-12,2018-04-13,2018-04-14,
# 2018-04-15,2018-04-16,2018-04-17,2018-04-18,2018-04-19,2018-04-20,2018-04-21,
# 2018-04-22,2018-04-23,2018-04-24,2018-04-25,2018-04-26,2018-04-27,2018-04-28,
# 2018-04-29,2018-04-30"
set `date +%m" "%Y`
CURMTH=$1
@piyusht007
piyusht007 / TaskRunner.java
Created December 30, 2019 09:03
Running tasks in parallel in JAVA
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.http.MediaType;
import org.springframework.web.util.UriComponents;
@piyusht007
piyusht007 / findIAMUsers.py
Created January 9, 2020 07:21
Python script to find AWS IAM users that has LastActivity/Access Key Age >= 90 days
import boto3
from datetime import datetime, timedelta
client = boto3.client('iam')
users = client.list_users()
flaggedUsers = []
flaggedUserThreshold = 90
for key in users['Users']:
@piyusht007
piyusht007 / findIAMUsers.sh
Created January 9, 2020 09:11
Shell script to find AWS IAM users that has LastActivity/Access Key Age >= 90 days
#!/bin/sh
set +x;
SHOW_THRESHOLD_BREACHED_ONLY=$1;
SEPARATOR=",";
LAST_ACTIVITY="LastActivity:";
ACCESS_KEY_AGE="AccessKeyAge:";
flagged="true";
notFlagg
@piyusht007
piyusht007 / test.yaml
Last active January 26, 2020 13:32
AWS Cloud Formation Template - VPC with one public subnet and one private subnet
AWSTemplateFormatVersion: '2010-09-09'
Description: 'A sample template for VPC with one public and one private subnet'
Parameters:
petClinicPublicSSHKey:
Default: pc-public
Description: 'Name of an existing EC2 KeyPair to enable SSH access into ec2 instance on public subnet'
Type: 'AWS::EC2::KeyPair::KeyName'
petClinicPrivateSSHKey:
Default: pc-private
Description: 'Name of an existing EC2 KeyPair to enable SSH access into ec2 instance on private subnet'
@piyusht007
piyusht007 / cheatsheet.txt
Last active March 23, 2020 13:48
AWS Cheatsheet
Using AWS Route 53 DNS:
1. As active and passive failover setup: Make 2 record sets with the same domain name and set one as primary and the other as secondary under Routing Policy Type "Failover".
2. As round-robin DNS setup: Make 2 record sets with same domain name and set weight to 50 percent on both under Routing Policy Type "Weighted".