Can I import a stream table into a partitioned table in dolphindb database? - database-partitioning

Can I import a stream table into a partitioned table in dolphindb database?
I create a partitioned table as below:
login("admin", "123456")
dbPath="dfs://thermalDB"
stream = streamTable(10000:0,`id`ts`tem,[SYMBOL,TIMESTAMP,DOUBLE])
dbDate = database(, VALUE, 2021.03.17..2031.03.17)
dbID = database(, HASH, [SYMBOL, 50])
db=database(dbPath, COMPO, [dbDate, dbID])
db.createPartitionedTable(stream, "pt", `ts`id)
But it returns me an error:
A stream table does not support direct access. Please use sql query to retrieve data
How does the error occur?

The table passed into the function createPartitionedTable cannot be a stream table. You can write the code as follows:
login("admin", "123456")
dbPath="dfs://thermalDB"
dbDate = database(, VALUE, 2021.03.17..2031.03.17)
dbID = database(, HASH, [SYMBOL, 50])
db=database(dbPath, COMPO, [dbDate, dbID])
t = table(1:0,`id`ts`tem,[SYMBOL,TIMESTAMP,DOUBLE])
db.createPartitionedTable(t, "pt", `ts`id)

Related

In R with mongolite, how to return _id and extract timestamp from _id

We have the following R code which fetches an entire collection from our mongodb cluster
mongo_uri <- "mongodb+srv://username:password#our-cluster.dwxnd.gcp.mongodb.net/dbname?retryWrites=true&w=majority"
con <- mongolite::mongo('users', db = 'dbname', url = mongo_uri)
users <- con$find('{}')
In the following code, the _id is not returned. However, we want the _id returned, and then we want to use the _id along with the ObjectId.getTimestamp function to extract the date that the document was added to the collection.
Is this possible to do in R with mongolite?
Solved this one on my own.
users <- con$find('{}', fields = '{}')
oid_to_timestamp(users$`_id`[1])

boto3 resource for querying dynamodb : Query condition missed key schema element

I have a table as : AdvgCountries which has two columns
a. CountryId (String) (Parition Key)
b. CountryName(String) Sort Key
While creating the table , I created with only Partition Key and then later added a Global Secondary Index with Index name as:
CountryName-index
Type : GSI
Partition key : CountryId
Sort Key : CountryName
I am able to retrieve CountryName based upon CountryId but unable to retrieve CountryId based upon CountryName. Based upon my reading I found that there are options to do this by providing indexname but I get the following error:
botocore.exceptions.ClientError: An error occurred
(ValidationException) when calling the Query operation: Query
condition missed key schema element: CountryId
import boto3
import json
import os
from boto3.dynamodb.conditions import Key, Attr
def query_bycountryname(pCountryname, dynamodb=None):
if not dynamodb:
dynamodb = boto3.resource('dynamodb', endpoint_url="https://dynamodb.us-east-1.amazonaws.com")
table = dynamodb.Table('AdvgCountires')
print(f"table")
attributes = table.query(
IndexName="CountryName-index",
KeyConditionExpression=Key('CountryName').eq(pCountryname),
)
if 'Items' in attributes and len(attributes['Items']) == 1:
attributes = attributes['Items'][0]
print(f"before return")
return attributes
if __name__ == '__main__':
CountryName = "India"
print(f"Data for {CountryName}")
countries = query_bycountryname(CountryName)
for country in countries:
print(country['CountryId'], ":", country['CountryName'])
Any help is appreciated.
You can't be able to fetch primary key value based on sort key. DynamoDB does not work like this.
In Dynamodb, each item’s location is determined by the hash value of
its partition key.
The Query operation in Amazon DynamoDB finds items based on primary
key values.
KeyConditionExpression are used to write conditional statements by
using comparison operators that evaluate against a key and limit the
items returned. In other words, you can use special operators to
include, exclude, and match items by their sort key values.

Appending new data to sqlite db in R

I have created a table in a sqlite3 database from R using the following code:-
con <- DBI::dbConnect(drv = RSQLite::SQLite(),
dbname="data/compfleet.db")
s<- sprintf("create table %s(%s, primary key(%s))", "PositionList",
paste(names(FinalTable), collapse = ", "),
names(FinalTable)[2])
dbGetQuery(con, s)
dbDisconnect(con)
The second column of the table is UID which is the primary key. I then run a script to update the data in the table. The updated data could contain the same UID which already exists in the table. I don't want these existing records to be updated and just want the new records(with new UID values) to be appended to this database. The code I am using is:-
DBI::dbWriteTable(con, "PositionList", FinalTable, append=TRUE, row.names=FALSE, overwite=FALSE)
Which returns an error:
Error in result_bind(res#ptr, params) :
UNIQUE constraint failed: PositionList.UID
How can I achieve the task of appending only the new UID values without changing the existing UID values even if they appear when I run my updation script?
You can query the existing UIDs (as a one-column data frame) and remove corresponding rows from the table you want to insert.
uid_df <- dbGetQuery(con, "SELECT UID FROM PositionList")
dbWriteTable(con, "PositionList", FinalTable[!(FinalTable$UID %in% uid_df[[1]]), ], ...)
When you are going to insert data,first get the data from database by using UID.If data is exist nothing to do else insert new data with new UID.Duplicate Primary Key (UID) recard is not exist ,so it show the error.

dynamoDB query: SELECT * FROM mytable WHERE userId IN myList

I'm migrating mySQL to DynamoDB. In mySQL, I have
SELECT * FROM mytable WHERE userId IN myList
How can I achieve it in DynamoDB?
Thanks
You can use a filter expression on a Scan operation to achieve the same result as the SQL query above. For example, if you use the Document SDK in Java, you could write the following:
final Table table = new Table(AmazonDynamoDBClient.builder().withRegion(Regions.US_EAST_1).build(), "mytable");
//convert the Iterable<Item> returned by table.scan() to a stream of Items
StreamSupport.stream(
// list however many items you need to test after IN
table.scan(new ScanSpec().withFilterExpression("userId IN :u1, :u2")
//define the values of the set of usernames you are testing in the list avove
.withValueMap(ImmutableMap.of(":u1", "robert", ":u2", "daniel"))).spliterator(), false)
//do useful stuff with the result set
.map(Item::toJSONPretty)
.forEach(System.out::println);

Query DynamoDB with a hash key and a range key with Boto3

I am having trouble using AWS Boto3 to query DynamoDB with a hash key and a range key at the same time using the recommend KeyConditionExpression. I have attached an example query:
import boto3
from boto3 import dynamodb
from boto3.session import Session
dynamodb_session = Session(aws_access_key_id=AWS_KEY,
aws_secret_access_key=AWS_PASS,
region_name=DYNAMODB_REGION)
dynamodb = dynamodb_session.resource('dynamodb')
table=dynamodb.Table(TABLE_NAME)
request = {
'ExpressionAttributeNames': {
'#n0': 'hash_key',
'#n1': 'range_key'
},
'ExpressionAttributeValues': {
':v0': {'S': MY_HASH_KEY},
':v1': {'N': GT_RANGE_KEY}
},
'KeyConditionExpression': '(#n0 = :v0) AND (#n1 > :v1)',
'TableName': TABLE_NAME
}
response = table.query(**request)
When I run this against a table with the following scheme:
Table Name: TABLE_NAME
Primary Hash Key: hash_key (String)
Primary Range Key: range_key (Number)
I get the following error and I cannot understand why:
ClientError: An error occurred (ValidationException) when calling the Query operation: Invalid KeyConditionExpression: Incorrect operand type for operator or function; operator or function: >, operand type: M
From my understanding the type M would be a map or dictionary type and I am using a type N which is a number type and matches my table scheme for the range key. If someone could explain why this error is happening or I am also open to a different way of accomplishing the same query even if you cannot explain why this error exists.
The Boto 3 SDK constructs a Condition Expression for you when you use the Key and Attr functions imported from boto3.dynamodb.conditions:
response = table.query(
KeyConditionExpression=Key('hash_key').eq(hash_value) & Key('range_key').eq(range_key_value)
)
Reference: Step 4: Query and Scan the Data
Hope it helps
Adding this solution as the accepted answer did not address why the query used did not work.
TLDR: Using query on a Table resource in boto3 has subtle differences as opposed to using client.query(...) and requires a different syntax.
The syntax is valid for a query on a client, but not on a Table. The ExpressionAttributeValues on a table do not require you to specify the data type. Also if you are executing a query on a Table resource you do not have to specify the TableName again.
Working solution:
from boto3.session import Session
dynamodb_session = Session(aws_access_key_id=AWS_KEY,aws_secret_access_key=AWS_PASS,region_name=DYNAMODB_REGION)
dynamodb = dynamodb_session.resource('dynamodb')
table = dynamodb.Table(TABLE_NAME)
request = {
'ExpressionAttributeNames': {
'#n0': 'hash_key',
'#n1': 'range_key'
},
'ExpressionAttributeValues': {
':v0': MY_HASH_KEY,
':v1': GT_RANGE_KEY
},
'KeyConditionExpression': '(#n0 = :v0) AND (#n1 > :v1)',
}
response = table.query(**request)
I am the author of a package called botoful which might be useful to avoid dealing with these complexities. The code using botoful will be as follows:
import boto3
from botoful import Query
client = boto3.Session(
aws_access_key_id=AWS_KEY,
aws_secret_access_key=AWS_PASS,
region_name=DYNAMODB_REGION
).client('dynamodb')
results = (
Query(TABLE_NAME)
.key(hash_key=MY_HASH_KEY, range_key__gt=GT_RANGE_KEY)
.execute(client)
)
print(results.items)

Resources