コード例 #1
0
ファイル: output.go プロジェクト: chentao/heka-flume
func (t *ThriftAppender) Connect() error {
	if t.client != nil && t.client.Transport != nil {
		if t.client.Transport.IsOpen() {
			return nil
		}
	}

	tSock, err := thrift.NewTSocketTimeout(t.addr, time.Duration(t.timeout)*time.Millisecond)
	if err != nil {
		return err
	}
	trans := thrift.NewTFramedTransport(tSock)
	protoFactory := thrift.NewTCompactProtocolFactory()
	c := flume.NewThriftSourceProtocolClientFactory(trans, protoFactory)
	t.client = c

	return t.client.Transport.Open()
}
コード例 #2
0
func main() {
	flag.Usage = Usage
	var host string
	var port int
	var protocol string
	var urlString string
	var framed bool
	var useHttp bool
	var parsedUrl url.URL
	var trans thrift.TTransport
	_ = strconv.Atoi
	_ = math.Abs
	flag.Usage = Usage
	flag.StringVar(&host, "h", "localhost", "Specify host and port")
	flag.IntVar(&port, "p", 9090, "Specify port")
	flag.StringVar(&protocol, "P", "binary", "Specify the protocol (binary, compact, simplejson, json)")
	flag.StringVar(&urlString, "u", "", "Specify the url")
	flag.BoolVar(&framed, "framed", false, "Use framed transport")
	flag.BoolVar(&useHttp, "http", false, "Use http")
	flag.Parse()

	if len(urlString) > 0 {
		parsedUrl, err := url.Parse(urlString)
		if err != nil {
			fmt.Fprintln(os.Stderr, "Error parsing URL: ", err)
			flag.Usage()
		}
		host = parsedUrl.Host
		useHttp = len(parsedUrl.Scheme) <= 0 || parsedUrl.Scheme == "http"
	} else if useHttp {
		_, err := url.Parse(fmt.Sprint("http://", host, ":", port))
		if err != nil {
			fmt.Fprintln(os.Stderr, "Error parsing URL: ", err)
			flag.Usage()
		}
	}

	cmd := flag.Arg(0)
	var err error
	if useHttp {
		trans, err = thrift.NewTHttpClient(parsedUrl.String())
	} else {
		portStr := fmt.Sprint(port)
		if strings.Contains(host, ":") {
			host, portStr, err = net.SplitHostPort(host)
			if err != nil {
				fmt.Fprintln(os.Stderr, "error with host:", err)
				os.Exit(1)
			}
		}
		trans, err = thrift.NewTSocket(net.JoinHostPort(host, portStr))
		if err != nil {
			fmt.Fprintln(os.Stderr, "error resolving address:", err)
			os.Exit(1)
		}
		if framed {
			trans = thrift.NewTFramedTransport(trans)
		}
	}
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error creating transport", err)
		os.Exit(1)
	}
	defer trans.Close()
	var protocolFactory thrift.TProtocolFactory
	switch protocol {
	case "compact":
		protocolFactory = thrift.NewTCompactProtocolFactory()
		break
	case "simplejson":
		protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
		break
	case "json":
		protocolFactory = thrift.NewTJSONProtocolFactory()
		break
	case "binary", "":
		protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
		break
	default:
		fmt.Fprintln(os.Stderr, "Invalid protocol specified: ", protocol)
		Usage()
		os.Exit(1)
	}
	client := flume.NewThriftSourceProtocolClientFactory(trans, protocolFactory)
	if err := trans.Open(); err != nil {
		fmt.Fprintln(os.Stderr, "Error opening socket to ", host, ":", port, " ", err)
		os.Exit(1)
	}

	switch cmd {
	case "append":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "Append requires 1 args")
			flag.Usage()
		}
		arg9 := flag.Arg(1)
		mbTrans10 := thrift.NewTMemoryBufferLen(len(arg9))
		defer mbTrans10.Close()
		_, err11 := mbTrans10.WriteString(arg9)
		if err11 != nil {
			Usage()
			return
		}
		factory12 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt13 := factory12.GetProtocol(mbTrans10)
		argvalue0 := flume.NewThriftFlumeEvent()
		err14 := argvalue0.Read(jsProt13)
		if err14 != nil {
			Usage()
			return
		}
		value0 := argvalue0
		fmt.Print(client.Append(value0))
		fmt.Print("\n")
		break
	case "appendBatch":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "AppendBatch requires 1 args")
			flag.Usage()
		}
		arg15 := flag.Arg(1)
		mbTrans16 := thrift.NewTMemoryBufferLen(len(arg15))
		defer mbTrans16.Close()
		_, err17 := mbTrans16.WriteString(arg15)
		if err17 != nil {
			Usage()
			return
		}
		factory18 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt19 := factory18.GetProtocol(mbTrans16)
		containerStruct0 := flume.NewAppendBatchArgs()
		err20 := containerStruct0.ReadField1(jsProt19)
		if err20 != nil {
			Usage()
			return
		}
		argvalue0 := containerStruct0.Events
		value0 := argvalue0
		fmt.Print(client.AppendBatch(value0))
		fmt.Print("\n")
		break
	case "":
		Usage()
		break
	default:
		fmt.Fprintln(os.Stderr, "Invalid function ", cmd)
	}
}