Hafiz Mujadid
Hafiz Mujadid

Reputation: 1595

java api to add storage plugin in apache drill

I want to add a new storage plugin using java code. Currently I am creating a json file and uploading it on drill web ui. But it fails. here is my code

def creatplugin() {
   val httpclient = new DefaultHttpClient()
    val httpPost = new HttpPost("http://ip:port/storage/hdfs1.json")
    val uploadFilePart = new FileBody(new File("D:/plugin.json"))
    val reqEntity = new MultipartEntity()
    reqEntity.addPart("hdfs1.json", uploadFilePart)
    httpPost.setEntity(reqEntity)
    httpPost.setHeader("Content-type", "application/json")
    val response = httpclient.execute(httpPost)
    println(response.getStatusLine().getStatusCode())
  }

In this case response code is 400 with bad request.

Any suggestion, what's going wrong? is there any other way to add plugin dynamically using java code instead of rest api?

Thanks

Upvotes: 1

Views: 469

Answers (2)

JimBoTron
JimBoTron

Reputation: 16

I would think it may be the multipart entry. I would just post the json data as part of the body of the post message. Here is an example curl that works.Use a StringEntity instead.

curl -X POST -H "Authorization: Basic bWFwcjpyb290NG1hcHI=" -H "Content-Type: application/json" -d '{"name":"nfl","config":{"type":"file","enabled":true,"connection":"maprfs:///","workspaces":{"views":{"location":"/mapr/demo.mapr.com/data/views","writable":true,"defaultInputFormat":null},"json":{"location":"/mapr/demo.mapr.com/data/nfl/json","writable":false,"defaultInputFormat":"json"},"csv":{"location":"/mapr/demo.mapr.com/data/nfl/csv","writable":false,"defaultInputFormat":"csv"},"tab":{"location":"/mapr/demo.mapr.com/data/nfl/txt","writable":false,"defaultInputFormat":"tsv"},"xml":{"location":"/mapr/demo.mapr.com/data/nfl/xml","writable":false,"defaultInputFormat":null}},"formats":{"csv":{"type":"text","extensions":["csv"],"delimiter":","},"tsv":{"type":"text","extensions":["tsv","txt"],"delimiter":"\t"},"json":{"type":"json"}}}}' http://maprdemo:8047/storage/nfl.json

Upvotes: 0

Hafiz Mujadid
Hafiz Mujadid

Reputation: 1595

The problem was multipart entity as pointed by Jim. Here is working code

def creatplugin() {
    val source = scala.io.Source.fromFile("D:/plugin.json").mkString
    val httpclient = new DefaultHttpClient()
    val httpPost = new HttpPost("http://ip:port/storage/hdfs1.json")
    val reqEntity = new StringEntity(source)
    httpPost.setEntity(reqEntity)
    httpPost.setHeader("content-type", "application/json")
    httpPost.setHeader("Accept", "application/json")
    val response = httpclient.execute(httpPost)
    println(response.getStatusLine().getStatusCode())
  }

Upvotes: 1

Related Questions