LEC 25-26
XQuery--Extension of XPath by SQL-like features
>> Every XPath expression is an XQuery expression
>> More general XQueries: FLWR expressions
Let clauses:
>> Format : let variable := XQuery expression
>> 将XQuery表达式的结果赋给变量
Example :
◦let $doc := doc("students.xml")
◦let $student_names := $doc/students/student/name
>> Variable names : start with $ ($doc, $x, $student....)
For clauses:
>> Format : for variable in XQuery expression
>> 依次考虑XQuery表达式结果中的每个项(顺序与结果相同)
Example :
◦for $s in $doc/students/student
◦for $name in $doc//student[module="COMP207"]/name
Where clauses : 和sql里的差不多,都是接condition
Other types of conditions :
例子:
下面那个答案错了 应该是
<pair>
<name>Anna</name>,<id>123456</id>
</pair>
这里修改也错了,应该是:
for $author in $b/author
return<pair>{$b/title},{$author}</pair>
output是:
<pair><title>...</title>,<author>Anna</author></pair>
<pair><title>...</title>,<author>Ben</author></pair>
Order by clauses : like in sql
order by 的位置在for&let,where 下面,return 上面
Group by clauses : similar to sql
Distinct-values
In SQL it was a key word (DISTINCT) 就是只要不同的值,相同的就不要
In XQuery, it is a function, called distinct-values Converts elements to strings
XML in SQL
Three general approaches to storing XML documents in a relational database :
◦Store XML documents as entries of a table
◦Store XML documents in schema-independent form
◦Store XML documents in shredded form across a number of attributes and relations
>> Creating Table using XML Type
Introduction to NoSQL
NoSQL : not only mysql
满足典型web应用程序的需求 :
◦Very fast access to data, with millions of users in parallel
◦Fault-tolerance 容错率
◦Flexibility in the type of data stored (semi-structured)
◦Full ACID compliance can sometimes be relaxed
The CAP Theorem
NoSQL Database Classification
Key-Value stores
Simple access mechanism :
1. find(k): returns value for key k
2. write(k, v): inserts value v under key k
Distributed Storage
>> Three properties :
1. Scalability Via Horizontal Fragmentation
New node can be added easily :
◦Add node to free range(s) and move key-value pairs appropriately
◦Automatic horizontal fragmentation
2. Availablility via Replication:
Replication is used to ensure availability
Replicas (copies of key-value pairs) are stored on consecutive nodes on the ring in clock-wise order
3. High performance :
◦Retrieving the value for a key is fast: apply the hash function to determine the node, then ask the node
◦Writing is fast, too.
>> Eventual Consistency
Document stores
Document = semistructured data associated with an object ID
XML vs JSON
>> XML :
>> JSON :
Use case :
Case Study: MongoDB
Create collections / insert documents / finding documents
Indexes in MongoDB
MongoDB Techniques
Column Stores
Uses two levels of fragmentation:
◦Top level: rows are divided into regions (i.e., ranges of rows)
◦Bottom level: regions store different column families in different nodes (i.e., computers)