Skip to content

Instantly share code, notes, and snippets.

package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
z := x / 2
epsilon := 0.001
@Megaprog
Megaprog / Main.kt
Created July 12, 2023 11:18
Spark local read from JDBC and write to parquet
import org.apache.parquet.hadoop.ParquetOutputFormat
import org.apache.spark.sql.SaveMode
import org.apache.spark.sql.SparkSession
fun main() {
val session = createSparkSession("test", 1)
val df = session.read().format("jdbc")
.option("url", "jdbc:postgresql://localhost:6432/pg-atlas-v2-stg-other?user=postgres")
.option("query", """select * from usdt_trx_udm_v2_transactions
@Megaprog
Megaprog / build.gradle.kts
Created January 26, 2022 13:55
Kotlin integrationTest for Gradle 6 or 7
sourceSets {
create("integrationTest") {
compileClasspath += main.get().output + configurations.testRuntimeClasspath
runtimeClasspath += output + compileClasspath
}
}
val integrationTestImplementation: Configuration by configurations.getting {
extendsFrom(configurations.implementation.get())
}
SELECT schema_name,
pg_size_pretty(sum(table_size)::bigint),
(sum(table_size) / pg_database_size(current_database())) * 100
FROM (
SELECT pg_catalog.pg_namespace.nspname as schema_name,
pg_relation_size(pg_catalog.pg_class.oid) as table_size
FROM pg_catalog.pg_class
JOIN pg_catalog.pg_namespace ON relnamespace = pg_catalog.pg_namespace.oid
) t
GROUP BY schema_name
@Megaprog
Megaprog / pg_locks.sql
Last active September 13, 2022 04:23
How to see PG locks with advisory bigint hash
select l.pid, relation::regclass, mode, a.application_name, a.query_start, locktype,
(classid::bigint << 32) | objid::bigint as hash
from pg_locks l join pg_stat_activity a on l.pid = a.pid where granted;
@Megaprog
Megaprog / supervisorScope_vs_SupervisorJob
Created December 1, 2021 10:47
CoroutineScope(coroutineContext + SupervisorJob()) and supervisorScope() look simular but behaviur is completly different. If you add SupervisorJob() then all jobs will be out of scope and when nobody will wait for them after scope finishes
fun main() {
runBlocking {
repeat(10) {
try {
// CoroutineScope(coroutineContext + SupervisorJob()).run {
supervisorScope {
listOf(
async {
throw IllegalStateException("ERROR OCCURRED.")
},
import psycopg2.extras
# del os.environ["PGTZ"]
# os.environ["PGTZ"]="UTC"
# os.environ["PGTZ"]="Europe/Moscow"
conn = psycopg2.connect(
# host="postgres-corecms-slave.spb.play.dc",
host="postgres-context-slave.spb.play.dc",
port=5433,
dbname="context",
from sqlalchemy import create_engine
engine = create_engine("postgresql://<name>:<password>@postgres-context-slave.spb.play.dc:5433/context", echo=True, connect_args={"options": "-c TimeZone=UTC"})
@Megaprog
Megaprog / SimpleMessageListenerContainerLifeCycle.java
Last active July 13, 2021 02:22
How to start and stop SimpleMessageListenerContainer and wait a result
val events = CopyOnWriteArrayList<UserSubscriptionExternalActivationEvent>()
val listenerContainer = rabbitConfig.bind<UserSubscriptionExternalActivationEvent>(
routingKey.externalActivation,
TopicExchange(properties.template.exchange),
) {
println("result is $it")
events.add(it)
Mono.empty()
}
listenerContainer.start()
use std::sync::{Arc, Mutex, MutexGuard};
use std::collections::HashMap;
use std::ops::Deref;
pub struct Inner<'a>(MutexGuard<'a, HashMap<i32, Vec<i32>>>, i32);
impl<'a> Deref for Inner<'a> {
type Target = Vec<i32>;
fn deref(&self) -> &Self::Target {
self.0.get(&self.1).unwrap()