ClickHouse-Encoder

 view release on metacpan or  search on metacpan

eg/etl_dbi.pl  view on Meta::CPAN

# 3) Stream batches into ClickHouse, reusing the encoder.
my $http = HTTP::Tiny->new(timeout => 60);
my $url  = "$ch_http/?query="
         . uri_escape("insert into $target_table format native");
my $total = 0;

my $writer = sub {
    my $body = shift;
    my $resp = $http->post($url, {
        content => $body,
        headers => { 'content-type' => 'application/octet-stream' },
    });
    die "insert failed (status $resp->{status}): $resp->{content}\n"
        unless $resp->{success};
};

my $stream = $enc->streamer($writer, batch_size => $batch);
while (my $row = $sth->fetchrow_arrayref) {
    # DBI returns arrayref; matches encoder's expected row shape directly.
    $stream->push_row([@$row]);   # copy because fetchrow_arrayref reuses the same arrayref
    $total++;

eg/from_csv.pl  view on Meta::CPAN

close $fh;
print "Loaded ", scalar @rows, " rows from $csv_path.\n";

my $body = $enc->encode(\@rows);
printf "Encoded to Native: %.2f MB\n", length($body) / 1024 / 1024;

my $http = HTTP::Tiny->new(timeout => 30);
my $url  = "$http_base/?query=" . uri_escape("insert into $table format native");
my $resp = $http->post($url, {
    content => $body,
    headers => { 'content-type' => 'application/octet-stream' },
});

if ($resp->{success}) {
    print "Inserted ", scalar @rows, " rows.\n";
} else {
    die "insert failed (status $resp->{status}): $resp->{content}\n";
}

eg/insert_compressed.pl  view on Meta::CPAN

            or die "gzip failed: $IO::Compress::Gzip::GzipError";
        return $out;
    };
}
print "Using compression: $enc_name\n";

my $http = HTTP::Tiny->new(timeout => 30);
sub run {
    my ($sql, $body) = @_;
    my $url = "$base/?query=" . uri_escape($sql);
    my %hdr = ('content-type' => 'application/octet-stream');
    if (defined $body && length $body) {
        $body = $compress->($body);
        $hdr{'content-encoding'} = $enc_name;
    }
    my $resp = $http->post($url, { content => $body // '', headers => \%hdr });
    die "ClickHouse error (status $resp->{status}): $resp->{content}\n"
        unless $resp->{success};
    return $resp->{content};
}

eg/insert_http.pl  view on Meta::CPAN


my $base = $ENV{CH_HTTP} // 'http://localhost:8123';
my $http = HTTP::Tiny->new(timeout => 10);

sub run_query {
    my ($sql, %opt) = @_;
    # POST for everything: ClickHouse rejects modifying queries via GET.
    my $url  = "$base/?query=" . uri_escape($sql);
    my $resp = $http->post($url, {
        content => $opt{body} // '',
        headers => { 'content-type' => 'application/octet-stream' },
    });
    die "ClickHouse query failed (status $resp->{status}): $resp->{content}\n"
        unless $resp->{success};
    return $resp->{content};
}

run_query('drop table if exists demo_events');
run_query(<<'SQL');
create table demo_events (
    id        UInt64,



( run in 0.949 second using v1.01-cache-2.11-cpan-524268b4103 )