I created a column family Users using following command:
create column family Users with comparator=TimeUUIDType and default_validation_class=UTF8Type;
Then I insert a column into Users. It shows as follows.
RowKey: jsmith
=>(column=66829930-515b-11e0-8443-0f82b246fa40, value=hello, timestamp=1300451382)
I want to access it using SimpleCassie. The command is:
$data = $cassie->keyspace('Keyspace1')->cf('Users')->key('jsmith')->column('66829930-515b-11e0-8443-0f82b246fa40')->value();
(I also tried: $data = $cassie->keyspace('Keyspace1')->cf('Users')->key('jsmith')->column($cassie->uuid('66829930-515b-11e0-8443-0f82b246fa40')->__toString())->value();)
However, They do not work. It always return NULL.
How can I get the column value (hello) I want?
Try:
$cassie->keyspace('Keyspace1')->cf('Users')->key('jsmith')->column($cassie->uuid('66829930-515b-11e0-8443-0f82b246fa40')->uuid)->value();)
Cassandra expects the binary representation of a UUID, not a hex/string representation.
Related
Is there a way to retrieve data from MySQL to Prolog knowledge base?
I am trying to retrieve, for example, the fields: Name and price from a table chair in a MySQL database to a Prolog knowledge base rather than declaring them in Prolog.
The comment by #Boris is interesting. Assuming available a builtin that issues a shell command and get the output stream (in SWI-Prolog we can use library(process)), here is a simple interface to query a Wordpress table from MySQL
query(USER, PWD, DB, QUERY, Columns, Rows) :-
atom_concat('-p', PWD, PPWD),
process_create(path(mysql), ['-u', USER, PPWD, '-D', DB, '-e', QUERY], [stdout(pipe(Out)),stderr(std)]),
read_record(Out, Columns),
read_records(Out, Rows).
read_record(Out, Fields) :-
read_line_to_codes(Out, Codes),
Codes \= end_of_file,
atom_codes(Line, Codes),
atomic_list_concat(Fields, '\t', Line).
read_records(Out, [Record|Rs]) :-
read_record(Out, Record),
!, read_records(Out, Rs).
read_records(Out, []) :-
close(Out).
test run:
test_query :-
query('------','-----',a_blog,"select * from wp_options limit 10", Cols,Rows),
writeln(columns:Cols),
maplist(writeln, Rows).
yields
?- test_query.
columns:[option_id,option_name,option_value,autoload]
[1,siteurl,http://localhost/a_blog,yes]
[2,home,http://localhost/a_blog,yes]
[3,blogname,a blog,yes]
[4,blogdescription,Just another WordPress site,yes]
[5,users_can_register,0,yes]
...
true.
so, instead of display, just assert the records:
capture_table(USER, PWD, DB, QUERY, Functor) :-
query(USER, PWD, DB, QUERY, _Columns, Rows),
maplist(capture_table(Functor), Rows).
capture_table(Functor, Row) :-
Clause =.. [Functor|Row],
assertz(Clause).
I'm working with a table in which information is stored in a table in JSON format. The JSON value field looks like:
select * from k2_extra_fields where id = 2 and published = 1;
id | value
2,[{"name":"Apples","value":1,"target":null,"alias":"","required":0,"showNull":1},{"name":"Pears","value":2,"target":null,"alias":"","required":0,"showNull":1},{"name":"Mangos","value":3,"target":null,"alias":"","required":0,"showNull":1},{"name":"Guava","value":4,"target":null,"alias":"Fruit","required":0,"showNull":1},{"name":"Pineapple","value":5,"target":null,"alias":"Fruit","required":0,"showNull":1}]
Or values in a simple line by line view (minus the ID):
[
{"name":"Apples","value":1,"target":null,"alias":"","required":0,"showNull":1},
{"name":"Pears","value":2,"target":null,"alias":"","required":0,"showNull":1},
{"name":"Mangos","value":3,"target":null,"alias":"","required":0,"showNull":1},
{"name":"Guava","value":4,"target":null,"alias":"Fruit","required":0,"showNull":1},
{"name":"Pineapple","value":5,"target":null,"alias":"Fruit","required":0,"showNull":1}
]
The query that leads me here returns the value of 3. 3 = Mangos. How do I take the '3' value and match it up with the stored names/values so that I end up with the output, Mangos?
It should be possible with build in mysql functionality, but very hard and 'not clever' idea to do. If you really need to compute this problem within mysql, you would need to actually add new funtionality to your mysql. Look up on UDF plugins: http://dev.mysql.com/doc/refman/5.1/en/udf-compiling.html
I have a table with a following columns (more than 2) in my database:
1. id
2. name
3. email
I am trying to fetch key value pairs using PDO by fetching the fetchMode to PDO::FETCH_KEY_PAIR and specifying columns id and name which works just fine
But when i try to specify name and email as the desired columns i get the result set but the key is not name but usual numeric zero based indexes
0 => some#email.com
Why?
BTW i am using laravel, here is my code:
$users = new User;
$users->getConnection()->setFetchMode(PDO::FETCH_KEY_PAIR);
return $users::get(array('name','email'));
I was able to get the desired result by using:
User::lists('email', 'name');
I'm new in cassandra and I wanna get the values from a column family, where the key is a TimeUUIDType.
I'm using PHP with PHPCassa, and I can insert(set) correctly in the column family, generating uuid with the function:
$key = CassandraUtil::uuid1();
The problem happen when I try to do a get in the column family, because i have and uuid in String format( something like that):
$uuidString= "e2658820-69f2-11e1-af9a-95dd4f324d9";
I would like to know if is possible cast or transform an String form to a valid uuid for cassandra in php or phpcassa, because my purpose is in another page create the correct uuid from the $uuuidString.
Thanks.
CassandraUtil::import($uuidString) will handle that.
Edit
As of phpcassa 1.0+, \phpcassa\UUID::import() is the proper method.
I am trying to match a md5 has (generated through php) to its original value in a SQLExpress database.
I am using the following function in my SQL query
master.sys.fn_varbintohexsubstring(0, HASHBYTES('MD5', 'ID'), 1, 0)
Where 'ID' is the field in the database.
However they both seem to return different values for the md5 hash. I have been using '12290' as a static value to test this.
php md5() returns: 0bd81786a8ec6ae9b22cbb3cb4d88179
The following SQL Statement returns the same output:
DECLARE #password VARCHAR(255)
SET #password = master.sys.fn_varbintohexsubstring(0, HASHBYTES('MD5', '12290'), 1, 0)
SELECT #password
Yet when I run the following statement from the table:
SELECT ID, master.sys.fn_varbintohexsubstring(0, HASHBYTES('MD5', CONVERT(NVARCHAR(255), ID)), 1, 0) AS temp
FROM Clients
ORDER BY ID ASC
The 'temp' value matching to the 'ID' value of 12290 returns: 1867dce5f1ee1ddb46ff0ccd1fc58e03
Any help on the matter would be much appreciated!
Thanks
Python helped me to help you.
>>> from hashlib import md5
>>> md5('1\x002\x002\x009\x000\x00').digest().encode('hex')
'1867dce5f1ee1ddb46ff0ccd1fc58e03'
NVARCHAR is Unicode type and it seems from the above experiment that '12990' is stored as UTF-16LE in your database: '1\02\09\09\00\0'.
Assuming that the data encoding in the PHP is UTF-8 data and you don't want to change the existing data in the database, this is how you can fix your PHP script:
<?php
$password = '12290';
$hash = md5(mb_convert_encoding($password, 'UTF-16LE', 'UTF-8')) . "\n";
echo $hash;
?>
Output:
susam#swift:~$ php utf16le-hash.php
1867dce5f1ee1ddb46ff0ccd1fc58e03
In case the data in PHP is in some other encoding such as ASCII, ISO-8859-1, etc. you can change the third argument to mb_convert_encoding accordingly. The list of all supported encodings is available at: http://www.php.net/manual/en/mbstring.supported-encodings.php
Also, see http://www.php.net/manual/en/function.mb-convert-encoding.php
I don't have SQL server to test this on, but the CONVERT command might be creating the NVARCHAR with 240-odd trailing blanks (as you have specified NVARCHAR(255))
Try setting the NVARCHAR to the length of the ID to test:
ARE #password VARCHAR(255)
SET #password = master.sys.fn_varbintohexsubstring(0, HASHBYTES('MD5', CONVERT(NVARCHAR(5), '12290')), 1, 0)
SELECT #password
Try with different lengths in the CONVERT - is there any difference?
One of two things is most likely the problem:
Either the ID column in that row has a value not exactly equal to '12290' (e.g. extra whitespace)
Or the CONVERT function produces such a value
In any case, a standard debugging approach would be to use an SQL query to SELECT the string lengths of that ID field and the return value of CONVERT; if either is not equal to 5, you found the error.
Alternatively you can perform a dump of the table in question including data, and look at the generated INSERT statement to see what the database says the value in that column is.