我使用Go作为我的后端服务器和Javascript作为我的前端,我以前成功地使用http在Go中的IPC中本地托管一些简单的数据,然后在我的表中从Javascript中的本地主机URL获取和检索.但现在我遇到了麻烦,试图设置一种服务和检索数据的方式使用飞行.

所以,我的问题主要是如何在Go后端本地设置Apache箭头飞行服务器(我的箭头记录是发送的数据),然后在Javascript前端检索这些数据?

代码从我的main. go文件设置数据:

import (
    "fmt"
    "github.com/apache/arrow/go/v16/arrow"
    "github.com/apache/arrow/go/v16/arrow/array"
    "github.com/apache/arrow/go/v16/arrow/flight"
    "github.com/apache/arrow/go/v16/arrow/memory"
    "log"
    "net/http"
)

var metadata = arrow.NewMetadata(
    []string{"type", "round", "date"},
    []string{"int", "1dp", "2024/03/30"},
)

var schema = arrow.NewSchema([]arrow.Field{
    {Name: "X", Type: arrow.PrimitiveTypes.Int32},
    {Name: "X + 5", Type: arrow.PrimitiveTypes.Int32},
}, &metadata)

func GetPutData() arrow.Record {

    pool := memory.NewGoAllocator()

    recordBuilder := array.NewRecordBuilder(pool, schema)

    recordBuilder.Field(0).(*array.Int32Builder).AppendValues([]int32{1, 2, 3, 4, 5}, nil)
    recordBuilder.Field(1).(*array.Int32Builder).AppendValues([]int32{6, 7, 8, 9, 10}, nil)

    record := recordBuilder.NewRecord()

    return record
}

我想我的主要方法是这样的:

func main() {
    // Create a Flight server locally with data from GetPutData
    rec := GetPutData()
    
    // Start flight server

    fmt.Println("Flight serving on port...")
}

然后在前端,我想象它会是这样的代码:

import * as Arrow from 'apache-arrow';
import * as Flight from 'apache-arrow/flight';

async function runExample(url) {

    const table = // Fetch data from flight server

    console.table(table.toArray());
    console.log(table.schema)
    console.log(table.data)
}

runExample(url);

我试过使用之前的例子,但它们似乎过时了?但我也试着查看文档here,但我发现旧方法已经改变了什么是令人难以置信的混乱,等等,而且我目前不担心添加任何身份验证.

推荐答案

因此,我只能在Go服务器端讨论这个问题,因为您在JavaScript端遇到的最大问题是必须访问gRPC.ArrowJS中有issue个飞行跟踪示例,尚未填充.

所有这些都说了,对于服务器端有一个示例服务器在documentation.

对于您的特定示例,最低限度是为您的服务器实现一个方法:DoGet:

type server struct {
  flight.BaseFlightServer
}

func (s *server) DoGet(*flight.Ticket, svc flight.FlightService_DoGetServer) error {
  // if your server can return more than one stream of data,
  // the ticket is how you would determine which to send.
  // for your example, we're going to ignore the ticket.  
  rec := GetPutData()
  defer rec.Release()
  // create a record stream writer
  wr := flight.NewRecordWriter(svc, ipc.WithSchema(rec.Schema()))
  defer wr.Close()
  // write the record
  return wr.Write(rec)
}

你的主函数可能会像这样:

func main() {
  srv := flight.NewFlightServer()
  // replace "localhost:0" with the hosting addr and port
  // using 0 for the port tells it to pick any open port
  // rather than specifying one for it.
  srv.Init("localhost:0")

  // register the flight server object we defined above
  srv.RegisterFlightService(&server{})
  // you can tell it to automatically shutdown on SIGTERM if you like
  srv.SetShutdownOnSignals(syscall.SIGTERM, os.Interrupt)
  fmt.Printf("Server listening on %s...\n", srv.Addr())
  srv.Serve()
}

当您开始使服务器变得更复杂时,您可以考虑定义GetFlightInfo方法和其他方法.

希望这对你有帮助!你还有什么问题可以问.

Javascript相关问答推荐

react 路由加载程序行为

我不知道为什么我的JavaScript没有验证我的表单

是什么原因导致此Angular 16应用程序中类型错误时属性结果不存在?

使用useup时,React-Redux无法找到Redux上下文值

从PWA中的内部存储读取文件

过滤对象数组并动态将属性放入新数组

角色 map 集/spritebook动画,用户输入不停止在键上相位器3

S文本内容和值不必要的不同

更新动态数据中对象或数组中的所有值字符串

使用Nuxt Apollo在Piniastore 中获取产品细节

如何在.NET Core中将chtml文件链接到Java脚本文件?

将数组扩展到对象中

使用NextJS+MongoDB+Prisma ORM获取无效请求正文,无法发布错误

更新Redux存储中的对象数组

Next.js中的服务器端组件列表筛选

JavaScript将字符串数字转换为整数

如何在加载页面时使锚定标记成为焦点

将Windows XP转换为原始数据以在html前端中显示

在执行console.log(new X())时记录一个字符串

如何为两条动态路由创建一个页面?