如何使用C#API将BigQuery行转换为JSON? [英] How do I convert a BigQuery row to JSON using the C# API?

查看:101
本文介绍了如何使用C#API将BigQuery行转换为JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的C#代码从BigQuery表中提取一些数据

I am pulling some data from a BigQuery table using the code below in C#

        BigQueryClient client = BigQueryClient.Create("<Project Name>");

        BigQueryTable table = client.GetTable("<Database>", "Students");

        string sql = $"select * FROM {table} where Marks='50'";
        BigQueryResults results = client.ExecuteQuery(sql);

        foreach (BigQueryRow row in results.GetRows())
        {

        }

我希望能够将整个结果变量读取到JSON中,或者能够从每一行中获取JSON.

I want to be able to either read the entire results variable into JSON or be able to get the JSON out of each row.

当然,我可以创建一个对表格进行建模的类.在foreach循环中,我可以将每一行读入类对象.我可以尝试使用"newton soft"之类的第三方将类对象序列化为JSON.

Of course, I could create a class that models the table. And inside the foreach loop, I could just read each row into the class object. The class object I can try to serialize into JSON using a third party like "newton soft".

类似的东西:

class Student{
  int id;  // assume these are columns in the db
  string name;

}

我的foreach现在看起来像:

    foreach (BigQueryRow row in results.GetRows())
    {
        Student s=new Student();
        s.id = Convert.ToString(row["id"]);
        s.name=  Convert.ToString(row["name"]);


        // something like  string x=x+ s.toJSON();  //using newton soft
    }

这样,字符串x将为每行生成并附加JSON. 还是有一种方法可以将每个学生添加到集合或列表中,然后从整个列表中获取JSON?

This way string x will have the JSON generated and appended for each row. Or is there a way I can just add each student to a collection or List and then get the JSON from the whole list?

整个阅读过程逐行逐字段对我来说似乎很乏味,而且我必须有一种更简单的感觉.没有看到Google BigQuery对C#的任何支持以直接转换为JSON.他们在Python中确实有一些东西.

This whole reading row by row and field by field seems tedious to me and there must be a simpler way I feel. Did not see any support from Google BigQuery for C# to directly convert to JSON. They did have something in Python.

如果不是,那么将列表添加到JSON会更好,但是我不确定是否支持.

If not then the list to JSON would be better but I am not sure if it supported.

更新:

https://github.com/GoogleCloudPlatform/google-cloud-dotnet/blob/master/apis/Google.Cloud.BigQuery.V2/Google.Cloud.BigQuery.V2/BigQueryRow.cs

看起来像Big Query Row类具有一个RawRow字段,该字段的类型为TableRow.并且该类使用JSON引用,因此,我确定它们具有JSON格式的行数据.我如何向我展示它?

Looks like the Big Query Row class has a RawRow field which is of Type TableRow. And the class uses JSON references so , I am sure they have the data of the row in JSON format . How can I expose it to me ?

推荐答案

这可能会有点晚,但是您可以使用:

This might be a little late but you can use:

var latestResult = _bigQueryClient.ExecuteQuery($"SELECT TO_JSON_STRING(t) FROM `{ProjectId}.{DatasetId}.{TableName}` as t", null

所有列将被序列化为json并放置在每一行的第一列中.然后,您可以使用类似Newtonsoft之类的方法轻松解析每一行.

All columns will be serialized as json and placed in the first column on each row. You can then use something like Newtonsoft to parse each row easily.

这篇关于如何使用C#API将BigQuery行转换为JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆