How to save json data in bigtable? [duplicate] - google-cloud-platform

I would like to insert a json object into a Hbase cellusing scala, presently i'm able to insert values using the below code but would like to know how i may be able to insert the entire Json object into a Hbase cell.
import org.apache.hadoop.hbase.util.Bytes.toBytes
val hTable:HTable = new HTable(configuration, "tablename")
val p = new Put(Bytes.toBytes("row1"))
p.add(Bytes.toBytes("info"),Bytes.toBytes("firstname)",Bytes.toBytes("Jim"))
hTable.put(p)
hTable.close()

You can encode your json object as a string. then encode this string as byte array. then put this byte array in Hbase. pseudo code will be like this:
json = createYourJson()
jsonString = json.toString
jsonBytyes = Bytes.toBytes(jsonString)
put.add(yourColumnFamily, yourQualifier, jsonBytes)
and when loading the value from hbase you have to reverse this order. Pseudo code will be like this:
jsonBytes = hbase.get(table, columnFamily, qualifier)
jsonString = Bytes.toString(jsonBytes)
json = Json.parse(jsonString)

Related

Flutter: Create map from list using a delimiter

I am trying to store a list of activities with a specific color locally and trying to convert the list into either a map or a list of lists.
Using shared preferences to save the data locally I have the following list:
List<String> value = ['Sleep: Blue', 'Meditation: Green', 'Running: Red'];
prefs.setStringList('ActivityList', value); //save data locally
But I want to be able to retrieve an object of the form:
values = [ {'Sleep', 'Blue'}, {'Meditation', 'Green'}, {'Running', 'Red'} ];
What would be the best way to do this and how would I use the delimiter ':' to split the data accordingly?
Thanks in advance!
I am not sure what you mean by array of objects. If you simply want an array of pairs, then the following should work for you
value.map((item) => item.split(": "))
Or if you want a key value map from your data, then you can do something like this:
Map.fromEntries(value.map((item) {
List<String> pair = item.split(": ");
return MapEntry(pair[0], pair[1]);
}));

Parse or get multiple key dictionary data from GET request

Datatables is sending to Django the following query string parameters:
action:remove
data[1][DT_RowId]:1
data[1][volume]:5.0
data[1][coeff]:35
data[2][DT_RowId]:2
data[2][volume]:4.0
data[2][coeff]:50
I can access the values like this:
print request.GET['data[1][volume]']
5.0
How can I access the key itself as a dictionary and its keys?
For example, I would like to access the value as data[1]['volume']. In addition, I need to access the keys; e.g. get 1 from data[1].
I think you will need to parse the keys, yourselves and convert them to dictionary. This could be done quickly using the regular expression module in python.
import re
pattern = re.compile("data\[(?P<key_one>.*?)\]\[(?P<key_two>.*?)\]")
match = pattern.match('data[1][volume]')
key_one = match.group('key_one')
key_two = match.group('key_two')
print(key_one) # Should print 1
print(key_two) # Should print volume
See Python documentation of its regular expression library to learn more.

RDD of List String convert to Row

I'm trying to convert an RDD that has a fixed size lists of strings (a result of parsing CSV file) into and RDD of Rows. This is so I can turn it into a dataframe, because I need it into a dataframe to write to parquet. Anyway the only part I need help with is the converting of Rdd from list of strings to Row.
The RDD variable name is RDD
I used:
import org.apache.spark.sql._
val RowRDD = RDD.map(r => Row.fromSeq(r))

Python Make dict of lists from string

I have the dict of lists that stored in database as a text
a = {u'1': [u'12'], u'2': [u'7', u'8', u'9']}
I want manipulating with this structure as a dict of lists.
a["2"][3] = 9
but haven't any idea how to convert this string to dict of lists back.
You should store that data as JSON rather than just the repr of a dict. Then you can easily convert to and from that format using the json library.
Tnx. I can do what I want using JSON
import json
a = {u'1': [u'12'], u'2': [u'7', u'8', u'9']}
Make JSON
y = json.dumps(a)
b = json.loads(y)
And finally we have
>>> b["2"][1]
8

<Binary> in sql

I want to select all the binary data from a column of a SQL database (SQL Server Enterprise) using C++ query. I'm not sure what is in the binary data, and all it says is .
I tried this (it's been passed onto me to study off from) and I honestly don't 100% understand the code at some parts, as I commented):
SqlConnection^ cn = gcnew SqlConnection();
SqlCommand^ cmd;
SqlDataAdapter^ da;
DataTable^ dt;
cn->ConnectionString = "Server = localhost; Database=portable; User ID = glitch; Pwd = 1234";
cn->Open();
cmd=gcnew SqlCommand("SELECT BinaryColumn FROM RawData", cn);
da = gcnew SqlDataAdapter(cmd);
dt = gcnew DataTable("BinaryTemp"); //I'm confused about this piece of code, is it supposed to create a new table in the database or a temp one in the code?
da->Fill(dt);
for(int i = 0; i < dt->Rows->Count-1; i++)
{
String^ value_string;
value_string=dt->Rows[i]->ToString();
Console::WriteLine(value_string);
}
cn->Close();
Console::ReadLine();
but it only returns a lot of "System.Data.DataRow".
Can someone help me?
(I need to put it into a matrix form after I extract the binary data, so if anyone could provide help for that part as well, it'd be highly appreciated!)
dt->Rows[i] is indeed a DataRow ^. To extract a specific field from it, use its indexer:
array<char> ^blob=dt->Rows[i][0];
This extracts the first column (since you have only one) and returns an array representation of it.
To answer the question in your code, the way SqlDataAdapter works is like this:
you build a DataTable to hold the data to retrieve. You can fill in its columns, but you're not required to. Neither are you required to give it a name.
you build the adapter object, giving it a query and a connection object
you call the Fill method on the adapter, giving it the previously created DataTable to fill with whatever your query returns.
and you're done with the adapter. At this point you can dispose of it (for example inside a using statement if you're using C#).