Last active
August 29, 2015 14:15
-
-
Save ghopson/9fae7d207ab7a7581649 to your computer and use it in GitHub Desktop.
Spock specification that calls HBase via REST api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import grails.converters.JSON | |
import grails.plugins.rest.client.RestBuilder | |
import org.apache.http.HttpStatus | |
import org.codehaus.groovy.grails.web.json.JSONArray | |
import spock.lang.Specification | |
/** | |
* Install hadoop, hbase using brew or something | |
* start up both | |
* ensure you can do hbase shell etc. | |
* start up hbase rest on port 8888 | |
* now run spec - note the first test deletes the schema! | |
*/ | |
class HBaseSpec extends Specification { | |
RestBuilder rest = new RestBuilder() | |
static final String table = 'test' | |
static final String rowKey = '1234567890abcdef' | |
static final String objectName = "Person" | |
static final String columnName = 'firstName' | |
static final String columnName2 = 'lastName' | |
static final String columnValue = 'Geoff' | |
static final String columnValue2 = 'Hopson' | |
String firstNameQualifier = String.format("%s:%s", objectName, columnName) | |
String lastNameQualifier = String.format("%s:%s", objectName, columnName2) | |
String encodedRowKey | |
String encodedColumnName | |
String encodedColumnName2 | |
String encodedValue | |
String encodedValue2 | |
def setup() { | |
encodedRowKey = rowKey.bytes.encodeBase64().toString() | |
encodedColumnName = firstNameQualifier.bytes.encodeBase64().toString() | |
encodedColumnName2 = lastNameQualifier.bytes.encodeBase64().toString() | |
encodedValue = columnValue.bytes.encodeBase64().toString() | |
encodedValue2 = columnValue2.bytes.encodeBase64().toString() | |
} | |
def 'REST delete schema'() { | |
when: | |
def response = rest.delete("http://localhost:8888/$table/schema") | |
then: | |
println("REST delete schema - response status = ${response.statusCode}") | |
println("REST delete schema - response JSON = ${response.json}") | |
assert response.status == HttpStatus.SC_OK | |
} | |
def "REST create table"() { | |
given: | |
def schema = [name: table, ColumnSchema:[[name:objectName]]] | |
when: | |
def response = rest.put("http://localhost:8888/$table/schema") { | |
accept("application/json") | |
contentType("application/json") | |
json schema | |
} | |
then: | |
println("REST create table - response status = ${response.statusCode}") | |
assert response.status == HttpStatus.SC_CREATED | |
} | |
def 'REST get schema'() { | |
when: | |
def response = rest.get("http://localhost:8888/$table/schema") | |
then: | |
println("REST get schema - response status = ${response.statusCode}") | |
println("REST get schema - response JSON = ${response.json}") | |
assert response.status == HttpStatus.SC_OK | |
} | |
def 'REST get tables'() { | |
when: | |
def response = rest.get("http://localhost:8888/") { | |
accept("application/json") | |
contentType("application/json") | |
} | |
then: | |
println("REST get tables - response status = ${response.statusCode}") | |
println("REST get tables - response JSON = ${response.json}") | |
assert response.status == HttpStatus.SC_OK | |
assert response.json.table instanceof JSONArray | |
assert response.json.table*.name.contains(table) | |
} | |
def "REST add single column to row"() { | |
given: | |
def rowData = [Row: [[ | |
key: encodedRowKey, | |
Cell:[ | |
[ | |
column:encodedColumnName, | |
$: encodedValue | |
] | |
] | |
]] | |
] | |
println "rowData = ${rowData as JSON}" | |
when: | |
def response = rest.put("http://localhost:8888/$table/$rowKey/$firstNameQualifier") { | |
accept("application/json") | |
contentType("application/json") | |
json rowData | |
} | |
then: | |
println("REST add single row - response status = ${response.statusCode}") | |
assert response.status == HttpStatus.SC_OK | |
} | |
def "REST add second column to row"() { | |
given: | |
def rowData = [Row: [[ | |
key: encodedRowKey, | |
Cell:[ | |
[ | |
column:encodedColumnName2, | |
$: encodedValue2 | |
] | |
] | |
]] | |
] | |
println "rowData = ${rowData as JSON}" | |
when: | |
def response = rest.put("http://localhost:8888/$table/$rowKey/$lastNameQualifier") { | |
accept("application/json") | |
contentType("application/json") | |
json rowData | |
} | |
then: | |
println("REST add single row - response status = ${response.statusCode}") | |
assert response.status == HttpStatus.SC_OK | |
} | |
def "REST get single row from table"() { | |
when: | |
def response = rest.get("http://localhost:8888/$table/$rowKey") { | |
accept("application/json") | |
contentType("application/json") | |
} | |
then: | |
println("REST get single row - response status = ${response.statusCode}") | |
println("REST get single row - response JSON = ${response.json}") | |
assert response.status == HttpStatus.SC_OK | |
assert response.json.Row[0].key == encodedRowKey | |
def data1 = response.json.Row[0].Cell[0].$ | |
def data2 = response.json.Row[0].Cell[1].$ | |
assert data1 == encodedValue | |
assert data2 == encodedValue2 | |
assert new String(data1.decodeBase64()) == columnValue | |
assert new String(data2.decodeBase64()) == columnValue2 | |
} | |
def "REST insert multiple columns"() { | |
given: | |
def rowData = [Row: [[ | |
key: encodedRowKey, | |
Cell:[ | |
[ | |
column:encodedColumnName, | |
$: encodedValue | |
], | |
[ | |
column:encodedColumnName2, | |
$: encodedValue2 | |
] | |
] | |
]] | |
] | |
println "rowData = ${rowData as JSON}" | |
when: | |
def response = rest.put("http://localhost:8888/$table/thisRowKeyDoesNotExistPostMultipleCols") { | |
accept("application/json") | |
contentType("application/json") | |
json rowData | |
} | |
then: | |
println("REST insert multiple columns - response status = ${response.statusCode}") | |
assert response.status == HttpStatus.SC_OK | |
} | |
/** | |
* Check the time taken for this test in the IDE | |
*/ | |
def "REST insert multiple rows"() { | |
given: | |
def rowKey = "${rowKey}_${index}".bytes.encodeBase64().toString() | |
def columnValue1 = "${columnValue}_${index}".bytes.encodeBase64().toString() | |
def columnValue2 = "${columnValue2}_${index}".bytes.encodeBase64().toString() | |
and: | |
def rowData = [Row: [[ | |
key: rowKey, | |
Cell:[ | |
[ | |
column:encodedColumnName, | |
$: columnValue1 | |
], | |
[ | |
column:encodedColumnName2, | |
$: columnValue2 | |
] | |
] | |
]] | |
] | |
when: | |
def response = rest.put("http://localhost:8888/$table/thisRowKeyDoesNotExistPostMultipleCols") { | |
accept("application/json") | |
contentType("application/json") | |
json rowData | |
} | |
then: | |
assert response.status == HttpStatus.SC_OK | |
where: | |
index << (1..10000) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment