1

I have string in which I have to get some string and store it in variable.

data source=local/SQL,1433;initial catalog=Employee;user id=user_first;password=abc@123;encrypt=true;connectretrycount=3;connection timeout=30;

I need to extract data source , initial catlog,user id and password . And store it in variable. I was trying to convert it into list and iterating over it and extracting string. But that is not feasible I guess.

I want to save that value into variable like

val source = "local/SQL"

val catlog = "Employee"

Is there any other method which I can follow?

0

1 Answer 1

1

What do you mean by "it's not feasible"? It works just fine:

// s is the same string you provided
s.split(";").map { keyVal => 
  val Array(key, value, _*) = keyVal.split("=", 2)
  key -> value
} 

// result -> 
res2: Array[(String, String)] = Array(
  ("data source", "local/SQL,1433"),
  ("initial catalog", "Employee"),
  ("user id", "user_first"),
  ("password", "abc@123"),
  ("encrypt", "true"),
  ("connectretrycount", "3"),
  ("connection timeout", "30")
)

And easily use this as a udf. Or you might prefer using regular expressions:

val extractorPattern = "data source=([^;]*);initial catalog=([^;]*);.*".r
// expand this for other fields as well


val extractorPattern(dataSource, initialCatalog) = s
// The result in ammonite:
// dataSource: String = "local/SQL,1433"
// initialCatalog: String = "Employee"

There are also some other approaches, you can also take the regex and use it in spark API functions.

Update


If you want to access it as a variable inside the function, you can do it in a safe way:

val dataSource = res2.collectFirst { case ("data source", dataSource) => dataSource } 

// dataSource: Option[String] = Some(value = "local/SQL,1433")

Or if you're sure that data source or basically any key always exists in the original string, then:

val Some(dataSource) = res2.collectFirst { case ("data source", dataSource) => dataSource } 
// dataSource: String = "local/SQL,1433"
Sign up to request clarification or add additional context in comments.

2 Comments

@clark I think this question is unclear a little bit, like where's the variable? do you mean a variable inside the function? Or some random global variable outside the function scope? In all the cases, what matters is the value and not variable, when you have the value, you can easily access it to a variable like myVariable = value. I'll provide some examples in the next comment.
@clark there are thousands of approaches you can take, personally I would've used dataSource.takeWhile(_ != ','). You can also use split, or regexes, or dropping, etc,. This is something that you should find out that's the best solution to your problem. (Try learning the type of approaches that conform to the programming language you're working with)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.