ClickHouse / clickhouse-js

Official JS client for ClickHouse DB
https://clickhouse.com
Apache License 2.0
205 stars 25 forks source link

Error querying clickhouse table using clickhouse/client-web #222

Closed timongll closed 7 months ago

timongll commented 8 months ago

I am attempting to use clickhouse/client-web

i am sure my username/password and host are correct because i can login through curl and clickhouse-client. But when i try to make a simple query i get the error shown in the image

    "@clickhouse/client-web": "^0.2.7",
    const tryClickhouse = async () => {
    console.log("Initialising clickhouse client");
    const client = createClient({
      host: URL,
      username: USERNAME,
      password: PASSWORD,
      database: "default",
      application: "pong"
    });

    console.log("ClickHouse ping");
    if (!(await client.ping())) {
      throw new Error("failed to ping clickhouse!");
    }
    console.log("ClickHouse pong!");

    const row = await client.query({
      query: "SELECT 1",
    });
image
slvrtrn commented 8 months ago

I checked it with a simple React app:

import { useEffect, useState } from 'react'
import { createClient } from '@clickhouse/client-web'

export function App() {
  const client = createClient({
    host: 'https://...aws.clickhouse.cloud:8443',
    password: '...'
  })
  const [rows, setRows] = useState<string>()

  useEffect(() => {
    async function ping() {
      console.log('ClickHouse ping')
      const result = await client.ping()
      if (!result.success) {
        console.error(result.error)
      } else {
        console.log('ClickHouse pong!')
      }
    }

    async function query() {
      const rs = await client.query({
        query: 'SELECT 42 AS count',
        format: 'JSONEachRow',
      })
      return rs.text()
    }

    ping().then(() => query().then(setRows))
  }, [setRows])

  return <span>Result: {rows}</span>
}

export default App

image

How does your URL look like? How is your ClickHouse deployed?

timongll commented 8 months ago

thanks i think it kind of worked using clickhouse/client

i am now querying a table but getting this error:

i am using a readonly user

"Cannot modify 'enable_http_compression' setting in readonly mode. "

    const row = await client.query({
      query,
    });

how do i modify this query to only read and not modify

slvrtrn commented 8 months ago

If you are using Node.js, then it is @clickhouse/client, indeed (see the docs).

Regarding a read-only user, should work like this:

const client = createClient({
  compression: {
    response: false, // cannot enable HTTP compression for a read-only user
  },
})

I just realized that it is not pointed out in the docs as it seems, nor does it have an example. Will add.

slvrtrn commented 8 months ago

@timongll, I added read-only user examples. The output should be something like this:

Created user clickhouse_js_examples_readonly_user_6a9549fb1b984e4880965834f57f1e35 with restricted access to the system database
------------------------------------------------------------------------
[Expected error] Readonly user cannot insert the data into the table. Cause:
 ClickHouseError: clickhouse_js_examples_readonly_user_6a9549fb1b984e4880965834f57f1e35: Not enough privileges. To execute this query, it's necessary to have the grant INSERT(id, name) ON default.clickhouse_js_examples_readonly_user_test_data. 
    at parseError (/home/serge/work/clickhouse-js/examples/node_modules/packages/client-common/src/error/parse_error.ts:29:12)
    at ClientRequest.onResponse (/home/serge/work/clickhouse-js/examples/node_modules/packages/client-node/src/connection/node_base_connection.ts:155:28)
    at processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: '497',
  type: 'ACCESS_DENIED'
}
------------------------------------------------------------------------
[Expected error] Cannot query system.users cause it was not granted. Cause:
 ClickHouseError: clickhouse_js_examples_readonly_user_6a9549fb1b984e4880965834f57f1e35: Not enough privileges. To execute this query, it's necessary to have the grant SHOW USERS ON *.*. 
    at parseError (/home/serge/work/clickhouse-js/examples/node_modules/packages/client-common/src/error/parse_error.ts:29:12)
    at ClientRequest.onResponse (/home/serge/work/clickhouse-js/examples/node_modules/packages/client-node/src/connection/node_base_connection.ts:155:28)
    at processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: '497',
  type: 'ACCESS_DENIED'
}
------------------------------------------------------------------------
Select result: [ { id: '12', name: 'foo' }, { id: '42', name: 'bar' } ]
------------------------------------------------------------------------
[Expected error] Cannot use compression with a read-only user. Cause:
 ClickHouseError: Cannot modify 'enable_http_compression' setting in readonly mode. 
    at parseError (/home/serge/work/clickhouse-js/examples/node_modules/packages/client-common/src/error/parse_error.ts:29:12)
    at ClientRequest.onResponse (/home/serge/work/clickhouse-js/examples/node_modules/packages/client-node/src/connection/node_base_connection.ts:155:28)
    at processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: '164',
  type: 'READONLY'
}
------------------------------------------------------------------------
All done!

Hope this helps.

slvrtrn commented 7 months ago

@timongll, closing this for now. If you have further questions, feel free to re-open or just DM me in the community Slack.