How do I convert a BigQuery row to JSON using the C# API?

烈酒焚心 提交于 2020-12-11 04:42:33

问题


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())
        {

        }

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.

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".

Something like :

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

}

My foreach would now look like:

    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
    }

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?

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.

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

Update :

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

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 ?


回答1:


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

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.




回答2:


I ran into the same issue. I am posting this solution which is not optimized for performance but very simple for multiple data types. This allows you to deserialize anything (almost)

public class BQ
{
    private string projectId = "YOUR_PROJECT_ID";
 
    public BQ()
    {

    }       
    public List<T> Execute<T>(string sql)
    {
        var client = BigQueryClient.Create(projectId);

        List<T> result = new List<T>();
        try
        {
            string query = sql;
            BigQueryResults results = client.ExecuteQuery(query, parameters: null);
            List<string> fields = new List<string>();

            foreach (var col in results.Schema.Fields)
            {
                fields.Add(col.Name);
            }

            Dictionary<string, object> rowoDict;

            foreach (var row in results)
            {
                rowoDict = new Dictionary<string, object>();
                foreach (var col in fields)
                {
                    rowoDict.Add(col, row[col]);
                }
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(rowoDict);
                T o = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
                result.Add(o);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        finally
        {
            client.Dispose();
            Console.WriteLine("Done.");
        }

        return result;

    }

}



回答3:


I would do somting like this Var res = Result. Getrows. Select(x=> new student(){id=x[`ID']}). And then var js = json. Conver(res); This way is much faster and clear.
Hop it help u




回答4:


You can use Newtonsoft.Json. First download by PackageManager Console the Nuget Package, here you can get the command to do that.

After download you can use it as the following code:

List<Student> list = new List<Student>();
foreach (BigQueryRow row in results.GetRows())
{
    Student s=new Student();
    s.id = Convert.ToString(row["id"]);
    s.name=  Convert.ToString(row["name"]);
    list.Add(s);
}
var jsonResult = Newtonsoft.Json.JsonConvert.SerializeObject(list);

I hope this can help you.



来源:https://stackoverflow.com/questions/42566229/how-do-i-convert-a-bigquery-row-to-json-using-the-c-sharp-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!