-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
block in batch can be replaced by data generated outside.
- Loading branch information
1 parent
86e77b8
commit 14e8c2f
Showing
5 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/timeplus-io/proton-go-driver/v2" | ||
"github.com/timeplus-io/proton-go-driver/v2/lib/column" | ||
"log" | ||
"time" | ||
) | ||
|
||
func BatchReplaceExample() { | ||
var ( | ||
ctx = context.Background() | ||
conn, err = proton.Open(&proton.Options{ | ||
Addr: []string{"127.0.0.1:8463"}, | ||
Auth: proton.Auth{ | ||
Database: "default", | ||
Username: "default", | ||
Password: "", | ||
}, | ||
//Debug: true, | ||
DialTimeout: time.Second, | ||
MaxOpenConns: 10, | ||
MaxIdleConns: 5, | ||
ConnMaxLifetime: time.Hour, | ||
}) | ||
) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
if err := conn.Exec(ctx, `DROP STREAM IF EXISTS example`); err != nil { | ||
log.Fatal(err) | ||
} | ||
err = conn.Exec(ctx, ` | ||
CREATE STREAM IF NOT EXISTS example ( | ||
Col1 uint8 | ||
, Col2 string | ||
) | ||
`) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
const rows = 20 | ||
var ( | ||
col1 column.UInt8 = make([]uint8, rows) | ||
col2 column.String = make([]string, rows) | ||
) | ||
for i := 0; i < rows; i++ { | ||
col1[i] = uint8(i) | ||
col2[i] = fmt.Sprintf("num%03d", i) | ||
} | ||
batch, err := conn.PrepareBatch(ctx, "INSERT INTO example (* except _tp_time)") | ||
err = batch.ReplaceBy( | ||
&col1, | ||
&col2, | ||
) | ||
if err != nil { | ||
return | ||
} | ||
err = batch.Send() | ||
if err != nil { | ||
return | ||
} | ||
} | ||
|
||
func main() { | ||
BatchReplaceExample() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/timeplus-io/proton-go-driver/v2" | ||
) | ||
|
||
func example() error { | ||
var ( | ||
ctx = context.Background() | ||
conn, err = proton.Open(&proton.Options{ | ||
Addr: []string{"127.0.0.1:8463"}, | ||
Auth: proton.Auth{ | ||
Database: "default", | ||
Username: "default", | ||
Password: "", | ||
}, | ||
//Debug: true, | ||
DialTimeout: time.Second, | ||
MaxOpenConns: 10, | ||
MaxIdleConns: 5, | ||
ConnMaxLifetime: time.Hour, | ||
}) | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
if err := conn.Exec(ctx, `DROP STREAM IF EXISTS example`); err != nil { | ||
return err | ||
} | ||
err = conn.Exec(ctx, ` | ||
CREATE STREAM IF NOT EXISTS example ( | ||
Col1 uint8 | ||
, Col2 string | ||
) | ||
`) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
batch, err := conn.PrepareBatch(ctx, "INSERT INTO example (* except _tp_time)") | ||
if err != nil { | ||
return err | ||
} | ||
for i := 0; i < 10; i++ { | ||
for j := 0; j < 10; j++ { | ||
err := batch.Append( | ||
uint8(i*10+j), | ||
fmt.Sprintf("num_%d_%d", j, i), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
if err := batch.StreamingSend(); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return batch.Close() | ||
} | ||
|
||
func main() { | ||
start := time.Now() | ||
if err := example(); err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println(time.Since(start)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters