Skip to content

Instantly share code, notes, and snippets.

@rajkrrsingh
Last active September 27, 2020 07:44
Show Gist options
  • Save rajkrrsingh/ecde1281f7afc1c8566bca5a4c14d20f to your computer and use it in GitHub Desktop.
Save rajkrrsingh/ecde1281f7afc1c8566bca5a4c14d20f to your computer and use it in GitHub Desktop.
sample Hive Client implemented in GoLang to connect to hiveserver2

for more information on golang hive driver, please refer https://github.com/beltran/gohive

Sample Table
create table test (id int);
insert into table test values (1),(2),(3),(4),(5);
select * from test;
1
2
3
4
5

hiveclient.go

package main

import (
	"context"
	"fmt"
	"github.com/beltran/gohive"
	"log"
)

func main() {
	conf := gohive.NewConnectConfiguration()

	conn, err := gohive.Connect("hdp31ab",10500,"NONE",conf)
	if err != nil {
		log.Fatal("Error occured while getting the connection %v ",err)
	}
	defer conn.Close()

	cur := conn.Cursor()
	defer cur.Close()

	cur.Exec(context.Background(),"select * from test")

	if cur.Err != nil {
		log.Fatal("unable to get the result set from hive server : %v",cur.Err)
	}
	ctx := context.Background()
	var rowcell1 int32
	for cur.HasMore(ctx){
		cur.FetchOne(ctx, &rowcell1)
		fmt.Println(rowcell1)
	}

}

Run it!!!

go run hiveclient.go 
1
2
3
4
5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment