1

我想使用 java 代码添加一个新的存储插件。目前我正在创建一个json文件并将其上传到drill web ui上。但它失败了。这是我的代码

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())
  }

在这种情况下,响应代码为 400,请求错误。

任何建议,怎么了?有没有其他方法可以使用java代码而不是rest api动态添加插件?

谢谢

4

2 回答 2

1

问题是吉姆指出的多部分实体。这是工作代码

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())
  }
于 2015-07-10T13:37:36.243 回答
0

我认为这可能是多部分条目。我只是将 json 数据作为帖子消息正文的一部分发布。这是一个有效的示例 curl。请改用 StringEntity。

curl -X POST -H "授权:基本 bWFwcjpyb290NG1hcHI=" -H "内容类型:应用程序/json" -d '{"name":"nfl","config":{"type":"file","已启用":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}},"格式":{"csv":{"type":"text","extensions":["csv"],"delimiter":","},"tsv":{"type" :"text","extensions":["tsv","txt"],"delimiter":"\t"},"json":{"type":"json"}}}}'text","extensions":["csv"],"delimiter":","},"tsv":{"type":"text","extensions":["tsv","txt"],"分隔符":"\t"},"json":{"type":"json"}}}}'text","extensions":["csv"],"delimiter":","},"tsv":{"type":"text","extensions":["tsv","txt"],"分隔符":"\t"},"json":{"type":"json"}}}}'http://maprdemo:8047/storage/nfl.json

于 2015-07-10T12:51:57.200 回答