Get an existing listing from a data exchange.

## Code sample

### Java


Before trying this sample, follow the Java setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery Java API
reference documentation](https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/overview).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication.md.txt#client-libs).

```java
import com.google.cloud.bigquery.analyticshub.v1.AnalyticsHubServiceClient;
import com.google.cloud.bigquery.analyticshub.v1.Listing;
import com.google.cloud.bigquery.analyticshub.v1.ListingName;
import java.io.IOException;

public class GetListing {

  public static void main(String[] args) throws IOException {
    String projectId = "example-project-id";
    String locationId = "example-location-id";
    String dataExchangeId = "example-data-exchange-id";
    String listingId = "example-listing-id";
    getListing(projectId, locationId, dataExchangeId, listingId);
  }

  public static void getListing(
      String projectId, String locationId, String dataExchangeId, String listingId)
      throws IOException {
    // Initialize the client with Application Default Credentials.
    try (AnalyticsHubServiceClient client = AnalyticsHubServiceClient.create()) {
      String name = ListingName.of(projectId, locationId, dataExchangeId, listingId).toString();

      Listing listing = client.getListing(name);

      System.out.println("Listing retrieved successfully: " + listing.getName());
      System.out.println("Display name: " + listing.getDisplayName());
      System.out.println("State: " + listing.getState());
    }
  }
}
```

### Node.js


Before trying this sample, follow the Node.js setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery Node.js API
reference documentation](https://googleapis.dev/nodejs/bigquery/latest/index.html).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication.md.txt#client-libs).

```javascript
'use strict';

const {AnalyticsHubServiceClient} = require('@google-cloud/bigquery-analyticshub');
const {status} = require('@grpc/grpc-js');

const client = new AnalyticsHubServiceClient();

/**
 * Retrieves a listing from an Analytics Hub data exchange.
 *
 * @param {string} projectId - The Google Cloud project ID containing the data exchange and listing.
 * @param {string} locationId - The Google Cloud location or region of the data exchange (for example, 'us' or 'us-central1').
 * @param {string} dataExchangeId - The caller-chosen identifier of the data exchange containing the listing.
 * @param {string} listingId - The caller-chosen identifier of the listing to retrieve.
 */
async function getListing(projectId, locationId, dataExchangeId, listingId) {
  try {
    const name = `projects/${projectId}/locations/${locationId}/dataExchanges/${dataExchangeId}/listings/${listingId}`;

    const request = {
      name,
    };

    const [listing] = await client.getListing(request);

    console.log(`Listing name: ${listing.name}`);
    console.log(`Display name: ${listing.displayName}`);
    console.log(`State: ${listing.state}`);
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Listing not found: ${err.message}`);
    } else {
      console.error(`API error: ${err.message}`);
    }
  }
}

module.exports = { getListing };
```

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples.md.txt?product=bigquery).