How to get database name from a connection? - rethinkdb

Using the driver rethinkdbdash...
Given:
const rConnection = r.db('someDb').table('someTable')
How do I get the database name or the table name this connection is using from the variable rConnection?

Similar to my answer here, here's an ugly solution that works (tested on rethinkdb, not rethinkdbdash):
nesh> let mom = (q, fn) => q.toString().match(new RegExp(fn + '\\(\\"(.*?)\\"\\)'))[1]
undefined
nesh> rql = r.db('foo').table('bar')
nesh> mom(rql, 'db')
'foo'
nesh> mom(rql, 'table')
'bar'

Related

Very slow connection to Snowflake from Databricks

I am trying to connect to Snowflake using R in databricks, my connection works and I can make queries and retrieve data successfully, however my problem is that it can take more than 25 minutes to simply connect, but once connected all my queries are quick thereafter.
I am using the sparklyr function 'spark_read_source', which looks like this:
query<- spark_read_source(
sc = sc,
name = "query_tbl",
memory = FALSE,
overwrite = TRUE,
source = "snowflake",
options = append(sf_options, client_Q)
)
where 'sf_options' are a list of connection parameters which look similar to this;
sf_options <- list(
sfUrl = "https://<my_account>.snowflakecomputing.com",
sfUser = "<my_user>",
sfPassword = "<my_pass>",
sfDatabase = "<my_database>",
sfSchema = "<my_schema>",
sfWarehouse = "<my_warehouse>",
sfRole = "<my_role>"
)
and my query is a string appended to the 'options' arguement e.g.
client_Q <- 'SELECT * FROM <my_database>.<my_schema>.<my_table>'
I can't understand why it is taking so long, if I run the same query from RStudio using a local spark instance and 'dbGetQuery', it is instant.
Is spark_read_source the problem? Is it an issue between Snowflake and Databricks? Or something else? Any help would be great. Thanks.

Mongo db won't connect with my code or write to the database

This is my code and I'm trying to get it so when a user does the add command, it stores their id and number they have used the command, but this isn't working, please can someone help.
num = 0
#client.command()
async def add(ctx):
global num
num += 1
await ctx.send('non')
mongo_url = "mongodb+=true&w=majority"
cluster = MongoClient(mongo_url)
db = cluster["mongo_url "]
collection = db["mongo_url "]
ping_cm = {"bank":num}
collection.insert_one(ping_cm)
I assume your mongo_url token is incorrect, it should have your name and password and db that you are storing it to, but you are accessing your token currently instead of your bank name, whatever that is called,
for example,
db = cluster["mongo_url "] #This has been set to your token, your mongo_url which won't do anything
You have used "bank" in other parts of your code, which is really confusing but I assume thats what you want to do and access, this will then store it in different rows for each user id who uses the command
num = 0
#client.command()
async def add(ctx):
global num
num += 1
await ctx.send('non')
mongo_url = "YOUR_MONGO_DATABASE_URL"
cluster = MongoClient(mongo_url)
db = cluster["bank"]
collection = db["bank"]
ping_cm = {"bank":num}
collection.insert_one(ping_cm)
await ctx.channel.send("Bank Updated!")
Make sure you are providing your mongo url "properly" otherwise the code won't be working at all they should look like this: eg.
EXAMPLE ONLY
mongo_url = "mongodb+srv://name:password#bank.9999000.mongodb.net/bank?retryWrites=true&w=majority" #EXAMPLE
You can get the URL when you go to the database you want to connect to, the click manage > db_url and copy that where I have included “YOUR_MONGO_DATABASE_URL" that should work if it is correct

How to access a single field of the logstash metadata event?

I am using logastash 7.6 with the output-jdbc plugin, but I get an error and I understand that it is because in the event it sends me all the fields to be indexed that are part of #metadata.
Probe just putting the event name without # and it works for me.
How can I get a single field within a #metada set?
ERROR:
ERROR logstash.outputs.jdbc - JDBC - Exception. Not retrying {:exception=>#, :statement=>"UPDATE table SET estate = 'P' WHERE codigo = ? ", :event=>"{\"properties\":{\"rangoAltura1\":null,\"rangoAltura2\":null,\"codigo\":\"DB_001\",\"rangoAltura3\":null,\"descrip\":\"CARLOS PEREZ\",\"codigo\":\"106\",\"rangoAltura5\":null,\"active\":true},\"id\":\"DB_001_555\"}"}
My .conf:
statement => ["UPDATE table SET estate = 'A' WHERE entidad = ? ","%{[#metadata][miEntidad]}"]
{[#metadata][miEntidad]} -----> map['entidad_temp'] = event.get('entidad')
According to the output jdbc plugin README you have it set correctly/
Maybe try the following as a work-around:
statement => ["UPDATE table SET estate = 'A' WHERE entidad = ? ","[#metadata][miEntidad]"]

ruby, postgres ,compare data from two tables coming from two databases

we have a database legacy from a SQL SERVER, and now the new database is a postgres, I would like to do some testing to see if the content is the same from both tables, sinnce I still don't have acess to the legacy database (SQL server), I said let play with the new postgres database by polling two table and try to compare the data, in the iteration loop I got confused, any idea will be helpful :
require 'pg'
pg_conn = PGconn.connect("localhost", 5432, '', '', "myDB", "userxx", "Zazzz")
all_children = pg_conn.exec("SELECT * from COMPANY;")
all_children2 = pg_conn.exec("SELECT * from COMPANY2;")
all_children.each do |child|
??????
end
Thanks,
The quick and dirty way would be to do a fast checking on one column, for example the name of the companies and compare the results to see the differences by substracting the resulting arrays :
require 'pg'
pg_conn = PGconn.connect("localhost", 5432, '', '', "myDB", "userxx", "Zazzz")
all_children = pg_conn.exec("SELECT name from COMPANY;")
all_children2 = pg_conn.exec("SELECT name from COMPANY2;")
# first check the count, if it's the same it's probably a good sign
puts all_children.length
puts all_children2.length
# check differences
results = all_children.uniq - all_children2.uniq
results2 = all_children2.uniq - all_children.uniq
For more complex tests you can maybe use the scientist gem from github : https://github.com/github/scientist
Edit:
sql version ?
select * from company left outer join company2 on company.name = company.name2 where company2.name is null;

DEPRECATION WARNING: implicit join references. Rails don't want to be a SQL parser for includes

I am upgrading an old app from rails3 to rails 4. Currently on rails 4.0
I have a lot of such deprecation warnings while running rspec:
Currently, Active Record recognizes the table in the string, and
knows to JOIN the comments table to the query, rather than loading
comments in a separate query. However, doing this without writing a
full-blown SQL parser is inherently flawed. Since we don't want to
write an SQL parser, we are removing this functionality. From now on,
you must explicitly tell Active Record when you are referencing a
table from a string:
Post.includes(:comments).where("comments.title =
'foo'").references(:comments)
If you don't rely on implicit join references you can disable the
feature entirely by setting
`config.active_record.disable_implicit_join_references = true`.
DEPRECATION WARNING: It looks like you are eager loading table(s)
(one of: product_masters, product_master_names) that are referenced
in a string SQL snippet. For example:
Post.includes(:comments).where("comments.title = 'foo'")
Here is the lines that causing error
def find_product_master
masters = ProductMaster.includes(:product_master_names).where("gcc = 1 and crossed_product_master_id is null and product_masters.supplier_id is null && (product_masters.slug = :name or product_masters.slug = :clean_name or product_master_names.slug = :name or product_master_names.slug = :clean_name) and product_masters.color = :color and (product_subfamily_id = :subfamily_id or second_product_subfamily_id = :subfamily_id)", name: line.file_name.parameterize, clean_name: line.file_name.parameterize.gsub("chateau","").parameterize, color: line.file_color, subfamily_id: line.product_subfamily_id)
masters.size == 1 ? line.update_column(:product_master_id, masters.first.id) : line.update_column(:status, "product_master_missing")
end
I tried like it was described in warning
def find_product_master
masters = ProductMaster.includes(:product_master_names).where("gcc = 1 and crossed_product_master_id is null and product_masters.supplier_id is null && (product_masters.slug = :name or product_masters.slug = :clean_name or product_master_names.slug = :name or product_master_names.slug = :clean_name) and product_masters.color = :color and (product_subfamily_id = :subfamily_id or second_product_subfamily_id = :subfamily_id)".references(:product_master_names), name: line.file_name.parameterize, clean_name: line.file_name.parameterize.gsub("chateau","").parameterize, color: line.file_color, subfamily_id: line.product_subfamily_id)
masters.size == 1 ? line.update_column(:product_master_id, masters.first.id) : line.update_column(:status, "product_master_missing")
end
But then i received an error
NoMethodError:
undefined method `references' for #<String:0x00000010f2c730>
Just chain references behind the include call instead of calling reference on a one of the parameter of where:
masters = ProductMaster
.includes(:product_master_names)
.references(:product_master_names)
.where(
# ...
)

Resources