GithubHelp home page GithubHelp logo

Comments (6)

nitinsridar avatar nitinsridar commented on August 23, 2024

Using instanceof operator? instanceof operator is used to find whether the object is an instance of the specified type. Therefore, you can check whether the value is an instance of int type. If so, you can store the value into an int variable. For example: if ( obj instanceof int ) { int myObject = obj; }

But I have to check all columns in a row and it takes lot of operation.

from java-spanner.

codyoss avatar codyoss commented on August 23, 2024

Hey @nitinsridar I am curious to hear more about what your use case is here. Mutations are usually for modifying Spanner rows. So usually you would be putting data into them, and if so would have the Java type information. For any given key I would suspect you would know the type to cast it to, correct me if I am wrong though.

from java-spanner.

nitinsridar avatar nitinsridar commented on August 23, 2024

When mutation is gone through the transaction we are publishing the mutation to pub/sub as json format, this is the use case, it's difficult to consume the mutation object at the other end so we are converting to json object and publishing to pub/sub and here I cannot convert to proper data type. Please help on this...

from java-spanner.

olavloite avatar olavloite commented on August 23, 2024

There's no standard method in the Java client library that does this, as Mutation objects are primarily intended for sending mutations to the backend, and not for reading out or sending to other services. The best way to do what you want, would be to add a method like this to your own code, and use that to read the actual underlying value from a mutation:

  Object getUnderlyingValue(Value v) {
    switch(v.getType().getCode()) {
      case ARRAY:
        switch(v.getType().getArrayElementType().getCode()) {
          case BOOL:
            return v.getBoolArray();
          case BYTES:
            return v.getBytesArray();
          case DATE:
            return v.getDateArray();
          case FLOAT64:
            return v.getFloat64Array();
          case INT64:
            return v.getInt64Array();
          case STRING:
            return v.getStringArray();
          case TIMESTAMP:
            return v.getTimestampArray();
          case STRUCT:
            // Array of struct is not supported.
          case ARRAY:
            // Array of array is not supported.
          default:
            break;
        }
        break;
      case BOOL:
        return v.getBool();
      case BYTES:
        return v.getBytes();
      case DATE:
        return v.getDate();
      case FLOAT64:
        return v.getFloat64();
      case INT64:
        return v.getInt64();
      case STRING:
        return v.getString();
      case STRUCT:
        return v.getStruct();
      case TIMESTAMP:
        return v.getTimestamp();
      default:
        break;
    }
    throw new IllegalStateException("Value with unknown type: " + v.toString());
  }

from java-spanner.

olavloite avatar olavloite commented on August 23, 2024

@nitinsridar Could you indicate whether the above example solves this for you? I don't think this is a very common use case, so it's not something that I think we want to add to the client library. If you add the above code snippet to your own code base, you should however be able to achieve what you wanted.

from java-spanner.

olavloite avatar olavloite commented on August 23, 2024

Closing this as we have not heard back in a while. Please re-open if additional information is needed.

from java-spanner.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.