Skip to content

Commit 261005f

Browse files
authored
Enhance hive writer (#1438)
* enhancement hive writer * update * update
1 parent 3c19ae5 commit 261005f

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

pkg/sqlfs/hive_writer.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ func uploadCSVFile(csv *os.File, db *sql.DB, hivePath, table, user, passwd strin
6565
if _, e := cmd.CombinedOutput(); e != nil {
6666
return fmt.Errorf("failed %s: %v", cmd, e)
6767
}
68+
defer func() {
69+
cmd = exec.Command("hdfs", "dfs", "-rm", "-r", "-f", hdfsPath)
70+
cmd.Env = hdfsEnv
71+
cmd.CombinedOutput()
72+
}()
6873

6974
cmd = exec.Command("hdfs", "dfs", "-copyFromLocal", csv.Name(), hdfsPath)
7075
cmd.Env = hdfsEnv

pkg/sqlfs/hive_writer_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,23 @@ import (
1717
"fmt"
1818
"io"
1919
"math/rand"
20+
"os/exec"
21+
"path"
2022
"strings"
2123
"testing"
2224

2325
"github.com/stretchr/testify/assert"
2426
)
2527

28+
func hasHDFSDir(hdfsPath string) bool {
29+
cmd := exec.Command("hdfs", "dfs", "-ls", hdfsPath)
30+
out, _ := cmd.CombinedOutput()
31+
if strings.Contains(string(out), "No such file or directory") {
32+
return false
33+
}
34+
return true
35+
}
36+
2637
func TestNewHiveWriter(t *testing.T) {
2738
testDriver = getEnv("SQLFLOW_TEST_DB", "hive")
2839
if testDriver != "hive" {
@@ -36,12 +47,13 @@ func TestNewHiveWriter(t *testing.T) {
3647
w, e := newHiveWriter(testDB, "/hivepath", tbl, "", "", bufSize)
3748
a.NoError(e)
3849
a.NotNil(w)
39-
defer w.Close()
4050

4151
has, e1 := hasTable(testDB, tbl)
4252
a.NoError(e1)
4353
a.True(has)
4454

55+
a.NoError(w.Close())
56+
a.False(hasHDFSDir(path.Join("/hivepath", tbl)))
4557
a.NoError(dropTable(testDB, tbl))
4658
}
4759

0 commit comments

Comments
 (0)