2

I am trying to retrieve the project details such as projectName and projectNumber in bigquery, but anywhere I can see only the projectId but not other details. I have explored the audit logs in my project when I ran a query and seen all the types of audit logs whether anywhere I can be able to find those details, but I was able to see the projectId only. I also explored the API's like is there any API serving my purpose, I have found one API which gives the list of projects but there also they are not providing the projectName and projectNumber details. So is there any other way I can get those details, please let me know.

EDIT: Looking for the JAVA API client

Wytrzymały Wiktor
  • 11,492
  • 5
  • 29
  • 37
saivasanth528
  • 171
  • 12

2 Answers2

0

From bigquery resources you can use its client library (which makes use of the API) to get that information.

UPDATED 12/07/2021: JAVA API

Unfortunately this was take out of JAVA API BIGQUERY Client and there is no news if it will make a return. So, you wont be able to get such information from BigQuery latest client. You can see one discussion about it here.

Regardless of that you will be able to get that info using resource manager. ( although you will need to have the information from the hierarchy above or at least the project name )

libraries used

import com.google.cloud.resourcemanager.v3.Project;
import com.google.cloud.resourcemanager.v3.ProjectName;
import com.google.cloud.resourcemanager.v3.ProjectsClient;

code

try (ProjectsClient projectsClient = ProjectsClient.create()) { 
     ProjectName name = ProjectName.of("[PROJECT]");
     Project prjct = projectsClient.getProject(name); 
     System.out.println(prjct.getProjectId());
     System.out.println(prjct.getName()); # returns `projects/415104041262`
}

To setup DOM and dependencies, you can check the official documentation for resource manager. Also, here is the link where can check the client used to get projects, project client

Workarounds:

Alternatively you can use other libraries and cmds to get that information, it makes use of bigquery python client library to get the information you are looking for:

from google.cloud import bigquery 
from google.oauth2 import service_account

# key_path = "path/to/service_account.json"
key_path = "my-service-account-key.json"

credentials = service_account.Credentials.from_service_account_file(
    key_path, scopes=["https://www.googleapis.com/auth/cloud-platform"],
)

client = bigquery.Client(credentials=credentials, project=credentials.project_id,)

print(client.get_service_account_email())

projects = client.list_projects(max_results=10)

for project in list(projects):
    print(project.project_id)
    print(project.numeric_id)

Note: follow this link for guidance on how to get a service account json.

output

bq-service-account-project-number@bigquery-account.iam.gserviceaccount.com
project id: my-project-id
project number: 7788990000xxx

Another options outside bigquery resources to get info about projects. You can get that information you will have to open google cloud shell and perform the following command:

gcloud projects describe <my-project-id>

output:

createTime: '2015-10-20T10:12:09.452Z'
lifecycleState: ACTIVE
name: my-project-id
parent:
  id: '111222333xxx'
  type: folder
projectId: my-project-id
projectNumber: '777555000xxx'

Also, I'm leaving these links where you can get project related info:

Finally, If you are looking about why its difficult to get info from bigquery about projects I'm providing this link that discuss about resource-hierarchy.

Betjens
  • 1,353
  • 2
  • 4
  • 13
  • Hi Betjens, thanks for your detailed response, but I am particularly looking at JAVA API, I have searched for that and found one repo . https://github.com/googleapis/java-resourcemanager Through that i was able to get the project details, but the methods are deprecated . That's the only issue, trying to find out the alternative for that – saivasanth528 Dec 06 '21 at 07:09
  • Hello saivasanth, I have updated my response. – Betjens Dec 07 '21 at 11:05
  • Indeed, As you mention, java-resourcemanager its the only way at the moment. From bigquery client you will not be able to get that info. – Betjens Dec 07 '21 at 11:13
  • Also, java-resourcemanager is still alive, its on v3 at the moment. – Betjens Dec 07 '21 at 11:16
  • thanks for your effort, I had posted my answer which fitted to my use case, please go through it – saivasanth528 Dec 20 '21 at 16:59
0

I have implemented it in this way, here the methods are deprecated, finding out the alternatives for this like moving of this artifact to our own artifactory like that which is out of the context currently, but I got the info that I want through the java client.

     <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-resourcemanager</artifactId>
        <version>1.1.4</version>
    </dependency>

This is the dependency that I have added in my pom.xml file

import com.google.cloud.resourcemanager.ResourceManager;
import com.google.cloud.resourcemanager.ResourceManagerOptions;
import com.google.cloud.resourcemanager.Project;
String credentialsPath = "tmp/credentials.json";
ResourceManager resourceManager = ResourceManagerOptions.newBuilder().setProjectId("my-project-id").
            setCredentials(GoogleCredentials.fromStream(new FileInputStream(credentialsPath))).build().getService();
  

 Project project = resourceManager.get(projectId);
// You got the project object which contains the details you needed
    

You need two permissions to get the details of the project

  1. projects.get
  2. Enable this API for your project

If these permissions are correct , you will get the details otherwise null object will be returned from the google side

saivasanth528
  • 171
  • 12