如何将json请求中的过滤器参数发送到hbase rest api?

oaxa6hgo  于 2021-06-02  发布在  Hadoop
关注(0)|答案(2)|浏览(411)

我正在尝试开发一个php客户端来与hbase restapi交互。但是我找不到使用json请求创建scanner的方法。我能找到的所有例子都使用xml格式。只是想知道是否可以使用json格式发送。
下面的代码返回了错误消息:“http/1.1 500不能反序列化java.lang.string示例的起始\u数组令牌”

<?php

$headers = array(
    'Accept: application/json',
    'Content-Type: application/json',
);

if (!file_exists(__DIR__ . '/tmp')) {
    mkdir(__DIR__ . '/tmp', 0777);
}

$scannerFile = __DIR__ . '/tmp/scanner';

if (!file_exists($scannerFile)) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, "http://hbase_uri:20550/news/scanner");
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_GSSNEGOTIATE);
    curl_setopt($ch, CURLOPT_USERPWD, ":");

    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
        'batch' => 10,
        'filter' => array(
            array(
                'type' => 'PrefixFilter',
                'value' => 'u123',
            ),
        ),
    )));

    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $page = curl_exec($ch);

    file_put_contents($scannerFile, $page);

    curl_close($ch);
} else {
    $s = file_get_contents($scannerFile);
    $parts = explode('Location: http://hbase_uri:20550/news/scanner/', $s);
    $parts = explode("\n", $parts[1]);
    $parts[0] = trim($parts[0]);
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, "http://hbase_uri:20550/news/scanner/" . $parts[0]);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_GSSNEGOTIATE);
    curl_setopt($ch, CURLOPT_USERPWD, ":");

    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $page = curl_exec($ch);

    file_put_contents(__DIR__ . '/tmp/result', $page);

    curl_close($ch);

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, "http://hbase_uri:20550/news/scanner/" . $parts[0]);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_GSSNEGOTIATE);
    curl_setopt($ch, CURLOPT_USERPWD, ":");

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');

    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $page = curl_exec($ch);

    file_put_contents(__DIR__ . '/tmp/result_delete', $page);

    curl_close($ch);
}
elcex8rz

elcex8rz1#

为了未来的游客。

使用@kiang answer,我成功地制作了一个创建扫描仪的 curl 贴子:

curl \
  --verbose \
  --request POST \
  --header 'Accept: application/json' \
  --header 'Content-Type: application/json' \
  --data '{"batch":10,"filter":"{\"type\":\"PrefixFilter\",\"value\":\"u123\"}"}' \
  'https://hbase:20550/namespace:table/scanner/'

他回来了

Location -> Buffer(https://hbase:20550/namespace:table/scanner/151302168977413777789)
...
AhcWSResponse(StandaloneAhcWSResponse(201, Created))

什么可能有用:

使用这些例子(你需要 " (标志)
查找scannermodel的文档。

pexxcrt2

pexxcrt22#

我从别人的暗示中得到了答案:

<?php

$headers = array(
    'Accept: application/json',
    'Content-Type: application/json',
);

if (!file_exists(__DIR__ . '/tmp')) {
    mkdir(__DIR__ . '/tmp', 0777);
}

$scannerFile = __DIR__ . '/tmp/scanner';

if (!file_exists($scannerFile)) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, "http://hbase_uri:20550/news/scanner");
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_GSSNEGOTIATE);
    curl_setopt($ch, CURLOPT_USERPWD, ":");

    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
        'batch' => 10,
        'filter' => json_encode(array(
            'type' => 'PrefixFilter',
            'value' => 'u123',
        )),
    )));

    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $page = curl_exec($ch);

    file_put_contents($scannerFile, $page);

    curl_close($ch);
} else {
    $s = file_get_contents($scannerFile);
    $parts = explode('Location: http://hbase_uri:20550/news/scanner/', $s);
    $parts = explode("\n", $parts[1]);
    $parts[0] = trim($parts[0]);
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, "http://hbase_uri:20550/news/scanner/" . $parts[0]);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_GSSNEGOTIATE);
    curl_setopt($ch, CURLOPT_USERPWD, ":");

    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $page = curl_exec($ch);

    file_put_contents(__DIR__ . '/tmp/result', $page);

    curl_close($ch);

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, "http://hbase_uri:20550/news/scanner/" . $parts[0]);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_GSSNEGOTIATE);
    curl_setopt($ch, CURLOPT_USERPWD, ":");

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');

    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $page = curl_exec($ch);

    file_put_contents(__DIR__ . '/tmp/result_delete', $page);

    curl_close($ch);
}

相关问题